程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#連Oracle數據庫

C#連Oracle數據庫

編輯:C#入門知識

using System;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
namespace Game.DBUtility
{

    /// <summary>
    /// A helper class used to execute queries against an Oracle database
    /// 一個用來幫助執行Oracle數據庫操作的類
    /// </summary>
    public abstract class OracleHelper
    {

        // Read the connection strings from the configuration file
        //從配置文件中讀取數據庫連接字串
        public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.AppSettings["OraConnString1"];
        public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.AppSettings["OraConnString2"];
        public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.AppSettings["OraConnString3"];
        public static readonly string ConnectionStringProfile = ConfigurationManager.AppSettings["OraProfileConnString"];
        public static readonly string ConnectionStringMembership = ConfigurationManager.AppSettings["OraMembershipConnString"];

        //Create a hashtable for the parameter cached
        //為Parameter創建一個哈稀表的緩存
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        /// <summary>
        /// Execute a database query which does not include a select
        /// 執行一次數據庫操作但是不包括查詢
        /// </summary>
        /// 下面為所傳遞的參數
        /// <param name="connString">Connection string to database </param>//數據庫的連接字串
        /// <param name="cmdType">Command type either stored procedure or SQL </param>//Command 類型為存儲過程或者SQL語句
        /// <param name="cmdText">Acutall SQL Command</param>
        /// <param name="commandParameters">Parameters to bind to the command</param>Parameter 參數
        /// <returns></returns>
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
        {
            // Create a new Oracle command
            //創建一個Oracle command對象
            OracleCommand cmd = new OracleCommand();

            //Create a connection
            //創建一個connection對象
            using (OracleConnection connection = new OracleConnection(connectionString))
            {

                //Prepare the command
                //為command做准備,下面有此方法定義
                PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);

                //Execute the command
                //執行 command
                int val = cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return val;
            }
        }

        /// <summary>
        /// Execute an OracleCommand (that returns no resultset) against an existing database transaction
        /// using the provided parameters.
        /// 用parameters執行現有的數據庫事務(沒有返回值??)---估計不對
        /// </summary>
        /// <remarks>
        /// e.g.:
        /// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
        /// 例子
        /// </remarks>
        /// <param name="trans">an existing database transaction</param>//一個現有的數據庫事務
        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>//command對象類型 (存儲過程???,文本,其他)
        /// <param name="commandText">the stored procedure name or PL/SQL command</param>//存儲過程名或者是SQL語句
        /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>//用於執行的Paramter數組
        /// <returns>an int representing the number of rows affected by the command</returns>//返回的是命令影響的行數
        public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
        {
            OracleCommand cmd = new OracleCommand();

            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
        

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved