C#使用ODP.NET(Oracle.ManagedDataAccess.dll)操做Oracle數據庫

在剛接觸C#的時候因爲公司使用的就是Oracle數據庫,那麼C#怎麼鏈接Oracle數據庫就成了首要去掌握的知識點了。在那時沒有ODP.NET,但visual studio卻對Oralce數據庫的調用進行了集成,就是下圖中的這個,儘管如今使用這個的時候visual studio提醒過期的,但在那時倒是很是好用的。html

爲何如今visual studio中這個程序集依然能使用,有ODP.NET,單單要拿出Oracle.ManagedDataAccess.dll進行本文的重點呢?web

一、visual studio中提醒過期,由於微軟和甲骨文存在數據庫的競爭關係,而且從.NET的更新來看這個程序集沒有再更新過,對於數據庫一直在更新而調用的程序集一直不更新敢用下去麼?還有一個我認爲很是重要的緣由就是使用這個程序集,擺脫不了每個運行程序的客戶機上都要去安裝Oracle客戶端的事實,試想一下,若是用戶數量巨大的時候,安裝客戶端的工做量就至關的大,很耗費資源;sql

二、甲骨文後來出了ODP.NET,各類資料以及官網上都說須要安裝ODP.NET,而後引用Oracle.ManagedDataAccess.dll,他和visual studio中的程序集最大的區別在於會隨着數據庫更新,是oracle官方出的驅動;數據庫

三、其實通過測試,ODP.NET的安裝徹底是沒有必要的,直接在網上下載一個Oracle.ManagedDataAccess.dll,按以下方式引用便可,使用此種方式減小了ODP.NET的安裝,無視操做系統的位數,最重要的是減小了Oracle客戶端的安裝;編程

 

那在哪裏下載Oracle.ManagedDataAccess.dll呢?若是Oracle.ManagedDataAccess.dll有更新怎麼知道呢?這個時候全宇宙最強大開發工具登場了!c#

安裝完成就能夠擼代碼了,而且若是有更新能夠隨時能看到。而且在編程方面和之前使用System.Data.OracleClient程序集沒有多大的不一樣,基本上拿之前使用System.Data.OracleClient程序集的代碼作少量的改動便可。windows

一、使用了鏈接字符串,而不是用oracle客戶端的服務名了;服務器

二、幾個命名空間的變化;oracle

下面是對數據庫經常使用操做的封裝app

複製代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.IO;
using System.Collections;
using System.Diagnostics;
using Oracle.ManagedDataAccess.Types;

namespace OracleDemo
{
    public class OracleHelper
    {
        private static string connStr = "User Id=admin;Password=123;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=test)))";

        #region 執行SQL語句,返回受影響行數
        public static int ExecuteNonQuery(string sql, params OracleParameter[] parameters)
        {
            using (OracleConnection conn = new OracleConnection(connStr))
            {
                conn.Open();
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion
        #region 執行SQL語句,返回DataTable;只用來執行查詢結果比較少的狀況
        public static DataTable ExecuteDataTable(string sql, params OracleParameter[] parameters)
        {
            using (OracleConnection conn = new OracleConnection(connStr))
            {
                conn.Open();
                using (OracleCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    OracleDataAdapter adapter = new OracleDataAdapter(cmd);
                    DataTable datatable = new DataTable();
                    adapter.Fill(datatable);
                    return datatable;
                }
            }
        }
        #endregion
    }
}
複製代碼

如下代碼是調用部分

                string sqlString = "Select a.col1,a.col2 From test a Where a.id=:id";
                DataTable dt = OracleHelper.ExecuteDataTable(sqlString,new OracleParameter(":id",1));

 

程序編寫完成,生成完成後,目錄大概以下:

這時候拷貝Debug文件夾到目標客戶電腦上便可直接運行了(前提是目標客戶電腦上有.NET Framework),徹底擺脫安裝Oracle客戶端。

出處:http://www.javashuo.com/article/p-gvkghhih-dr.html

===========================================================

1、學習背景:

1)微軟自vs2010後放棄了自家的system.data.oracleClient驅動,推薦使用oracle提供的驅動。

2)微軟提供的system.data.oracleClient驅動存在oracle32位與64位鏈接兼容性的問題,解決起來費時費力。

2、Oracle推薦的驅動:oracle.DataAccess.dll與oracle.ManagedDataAccess.dll

1)oracle.DataAccess.dll的缺陷。oracle.DataAccess.dll須要幾個dll文件,在實際使用中發現,oracle.DataAccess.dll無客戶端鏈接方式一樣存在所使用的dll文件的32位與64位和oracle數據庫或系統自己存在的dll存在鏈接兼容性問題。因此不推薦使用該驅動。

2)推薦使用oracle.ManagedDataAccess.dll(oracle.ManagedDataAccess.Client)全託管驅動。實際使用發現,oracle全託管驅動對32位和64位oracle數據庫具備很好的鏈接兼容性。可採用無客戶端遠程鏈接oracle,或在本機使用鏈接。可經過visual studio 2010 nuget安裝,或直接下載dll文件,而後引用oracle.ManagedDataAccess.dll文件。在項目數據訪問層中引用命名空間using oracle.ManagedDataAccess.Client,如圖2-1

2-1引用oracle.ManagedDataAccess.Client命名空間

 

3、Web.config鏈接字符串

NET提供的連接字符串connectionString="DataSource=192.168.1.2:1521/orcl;Persist Security Info=True;User ID=scott;Password=tiger";

 

4、關於command.BindByName = true;(重點敲黑板)

1)在使用全託管驅動oracle.ManagedDataAccess.dll後,ado.net的增刪查語句都執行正常,可是在執行修改語句(update語句)時,會引起異常。

2)默認狀況下ODP.Net 綁定變量時,sql語句中的變量順序必須和變量綁定順序一致,不然Fill查不到數據,cmd.ExecuteNonQuery()返回0沒法執行,將BindByName 設爲true後,sql變量順序和綁定順序便可不一致

 

出處:https://blog.csdn.net/windows_xp_guule/article/details/80565243

===========================================================

基於C#語言Oracle.ManagedDataAccess操做Oracle數據庫鏈接語句

  Oracle.ManagedDataAccess下載地址: https://download.csdn.net/download/u012949335/10643367 

 

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using Oracle.ManagedDataAccess;
using Oracle.ManagedDataAccess.Types;
using Oracle.ManagedDataAccess.Client;
 
namespace DB
{
    /// <summary>
    /// 數據訪問基礎類(基於Oracle) Copyright (C) Maticsoft
    /// 能夠用戶能夠修改知足本身項目的須要。
    /// </summary>
    public abstract class DbHelperOra
    {
        //數據庫鏈接字符串(web.config來配置),能夠動態更改connectionString支持多數據庫.        
        public static string connectionString = PubConstant.ConnectionString;     
        public DbHelperOra()
        {            
        }
 
        #region 公用方法
        
        public static int GetMaxID(string FieldName, string TableName)
        {
            string strsql = "select max(" + FieldName + ")+1 from " + TableName;
            object obj = GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        public static bool Exists(string strSql)
        {
            object obj = GetSingle(strSql);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
 
        public static bool Exists(string strSql, params OracleParameter[] cmdParms)
        {
            object obj = GetSingle(strSql, cmdParms);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
 
       
        #endregion
 
        
        #region  執行簡單SQL語句
 
        /// <summary>
        /// 執行SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        public static int ExecuteSql(string SQLString)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {                
                using (OracleCommand cmd = new OracleCommand(SQLString,connection))
                {
                    try
                    {        
                        connection.Open();
                        int rows=cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch(OracleException E)
                    {                    
                        connection.Close();
                        throw new Exception(E.Message);
                    }
                }                
            }
        }
        
        /// <summary>
        /// 執行多條SQL語句,實現數據庫事務。
        /// </summary>
        /// <param name="SQLStringList">多條SQL語句</param>        
        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
            using (OracleConnection conn = new OracleConnection(connectionString))
            {
                conn.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.Connection=conn;                
                OracleTransaction tx=conn.BeginTransaction();            
                cmd.Transaction=tx;                
                try
                {           
                    for(int n=0;n<SQLStringList.Count;n++)
                    {
                        string strsql=SQLStringList[n].ToString();
                        if (strsql.Trim().Length>1)
                        {
                            cmd.CommandText=strsql;
                            cmd.ExecuteNonQuery();
                        }
                    }                                        
                    tx.Commit();                    
                }
                catch(OracleException E)
                {        
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
        }
        /// <summary>
        /// 執行帶一個存儲過程參數的的SQL語句。
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <param name="content">參數內容,好比一個字段是格式複雜的文章,有特殊符號,能夠經過這個方式添加</param>
        /// <returns>影響的記錄數</returns>
        public static int ExecuteSql(string SQLString,string content)
        {                
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                OracleCommand cmd = new OracleCommand(SQLString,connection);
                OracleParameter myParameter = new OracleParameter("@content", OracleDbType.Varchar2);
                myParameter.Value = content ;
                cmd.Parameters.Add(myParameter);
                try
                {
                    connection.Open();
                    int rows=cmd.ExecuteNonQuery();
                    return rows;
                }
                catch(OracleException E)
                {                
                    throw new Exception(E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }    
            }
        }        
        /// <summary>
        /// 向數據庫裏插入圖像格式的字段(和上面狀況相似的另外一種實例)
        /// </summary>
        /// <param name="strSQL">SQL語句</param>
        /// <param name="fs">圖像字節,數據庫的字段類型爲image的狀況</param>
        /// <returns>影響的記錄數</returns>
        public static int ExecuteSqlInsertImg(string strSQL,byte[] fs)
        {        
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                OracleCommand cmd = new OracleCommand(strSQL,connection);
                OracleParameter myParameter = new OracleParameter("@fs", OracleDbType.LongRaw);
                myParameter.Value = fs ;
                cmd.Parameters.Add(myParameter);
                try
                {
                    connection.Open();
                    int rows=cmd.ExecuteNonQuery();
                    return rows;
                }
                catch(OracleException E)
                {                
                    throw new Exception(E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }                
            }
        }
        
        /// <summary>
        /// 執行一條計算查詢結果語句,返回查詢結果(object)。
        /// </summary>
        /// <param name="SQLString">計算查詢結果語句</param>
        /// <returns>查詢結果(object)</returns>
        public static object GetSingle(string SQLString)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                using(OracleCommand cmd = new OracleCommand(SQLString,connection))
                {
                    try
                    {
                        connection.Open();
                        object obj = cmd.ExecuteScalar();
                        if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
                        {                    
                            return null;
                        }
                        else
                        {
                            return obj;
                        }                
                    }
                    catch(OracleException e)
                    {                        
                        connection.Close();
                        throw new Exception(e.Message);
                    }    
                }
            }
        }
        /// <summary>
        /// 執行查詢語句,返回OracleDataReader ( 注意:調用該方法後,必定要對SqlDataReader進行Close )
        /// </summary>
        /// <param name="strSQL">查詢語句</param>
        /// <returns>OracleDataReader</returns>
        public static OracleDataReader ExecuteReader(string strSQL)
        {
            OracleConnection connection = new OracleConnection(connectionString);            
            OracleCommand cmd = new OracleCommand(strSQL,connection);                
            try
            {
                connection.Open();
                OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch(OracleException e)
            {                                
                throw new Exception(e.Message);
            }            
            
        }        
        /// <summary>
        /// 執行查詢語句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    OracleDataAdapter command = new OracleDataAdapter(SQLString,connection);                
                    command.Fill(ds,"ds");
                }
                catch(OracleException ex)
                {                
                    throw new Exception(ex.Message);
                }            
                return ds;
            }            
        }
 
 
        #endregion
 
        #region 執行帶參數的SQL語句
 
        /// <summary>
        /// 執行SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        public static int ExecuteSql(string SQLString,params OracleParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {                
                using (OracleCommand cmd = new OracleCommand())
                {
                    try
                    {        
                        PrepareCommand(cmd, connection, null,SQLString, cmdParms);
                        int rows=cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch(OracleException E)
                    {                
                        throw new Exception(E.Message);
                    }
                }                
            }
        }
        
            
        /// <summary>
        /// 執行多條SQL語句,實現數據庫事務。
        /// </summary>
        /// <param name="SQLStringList">SQL語句的哈希表(key爲sql語句,value是該語句的OracleParameter[])</param>
        public static void ExecuteSqlTran(Hashtable SQLStringList)
        {            
            using (OracleConnection conn = new OracleConnection(connectionString))
            {
                conn.Open();
                using (OracleTransaction trans = conn.BeginTransaction()) 
                {
                    OracleCommand cmd = new OracleCommand();
                    try 
                    {
                        //循環
                        foreach (DictionaryEntry myDE in SQLStringList)
                        {    
                            string     cmdText=myDE.Key.ToString();
                            OracleParameter[] cmdParms=(OracleParameter[])myDE.Value;
                            PrepareCommand(cmd,conn,trans,cmdText, cmdParms);
                            int val = cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
 
                            trans.Commit();
                        }                    
                    }
                    catch 
                    {
                        trans.Rollback();
                        throw;
                    }
                }                
            }
        }
    
                
        /// <summary>
        /// 執行一條計算查詢結果語句,返回查詢結果(object)。
        /// </summary>
        /// <param name="SQLString">計算查詢結果語句</param>
        /// <returns>查詢結果(object)</returns>
        public static object GetSingle(string SQLString,params OracleParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                using (OracleCommand cmd = new OracleCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null,SQLString, cmdParms);
                        object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
                        {                    
                            return null;
                        }
                        else
                        {
                            return obj;
                        }                
                    }
                    catch(OracleException e)
                    {                
                        throw new Exception(e.Message);
                    }                    
                }
            }
        }
        
        /// <summary>
        /// 執行查詢語句,返回OracleDataReader ( 注意:調用該方法後,必定要對SqlDataReader進行Close )
        /// </summary>
        /// <param name="strSQL">查詢語句</param>
        /// <returns>OracleDataReader</returns>
        public static OracleDataReader ExecuteReader(string SQLString,params OracleParameter[] cmdParms)
        {        
            OracleConnection connection = new OracleConnection(connectionString);
            OracleCommand cmd = new OracleCommand();                
            try
            {
                PrepareCommand(cmd, connection, null,SQLString, cmdParms);
                OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return myReader;
            }
            catch(OracleException e)
            {                                
                throw new Exception(e.Message);
            }                    
            
        }        
        
        /// <summary>
        /// 執行查詢語句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString,params OracleParameter[] cmdParms)
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                OracleCommand cmd = new OracleCommand();
                PrepareCommand(cmd, connection, null,SQLString, cmdParms);
                using( OracleDataAdapter da = new OracleDataAdapter(cmd) )
                {
                    DataSet ds = new DataSet();    
                    try
                    {                                                
                        da.Fill(ds,"ds");
                        cmd.Parameters.Clear();
                    }
                    catch(OracleException ex)
                    {                
                        throw new Exception(ex.Message);
                    }            
                    return ds;
                }                
            }            
        }
 
 
        private static void PrepareCommand(OracleCommand cmd,OracleConnection conn,OracleTransaction trans, string cmdText, OracleParameter[] cmdParms) 
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null) 
            {
                foreach (OracleParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }
 
        #endregion
 
        #region 存儲過程操做
 
        /// <summary>
        /// 執行存儲過程 返回SqlDataReader ( 注意:調用該方法後,必定要對SqlDataReader進行Close )
        /// </summary>
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <returns>OracleDataReader</returns>
        public static OracleDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
        {
            OracleConnection connection = new OracleConnection(connectionString);
            OracleDataReader returnReader;
            connection.Open();
            OracleCommand command = BuildQueryCommand( connection,storedProcName, parameters );
            command.CommandType = CommandType.StoredProcedure;
            returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);                
            return returnReader;            
        }
        
        
        /// <summary>
        /// 執行存儲過程
        /// </summary>
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <param name="tableName">DataSet結果中的表名</param>
        /// <returns>DataSet</returns>
        public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName )
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                DataSet dataSet = new DataSet();
                connection.Open();
                OracleDataAdapter sqlDA = new OracleDataAdapter();
                sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters );
                sqlDA.Fill( dataSet, tableName );
                connection.Close();
                return dataSet;
            }
        }
 
        
        /// <summary>
        /// 構建 OracleCommand 對象(用來返回一個結果集,而不是一個整數值)
        /// </summary>
        /// <param name="connection">數據庫鏈接</param>
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <returns>OracleCommand</returns>
        private static OracleCommand BuildQueryCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters)
        {            
            OracleCommand command = new OracleCommand( storedProcName, connection );
            command.CommandType = CommandType.StoredProcedure;
            foreach (OracleParameter parameter in parameters)
            {
                command.Parameters.Add( parameter );
            }
            return command;            
        }
        
        /// <summary>
        /// 執行存儲過程,返回影響的行數        
        /// </summary>
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <param name="rowsAffected">影響的行數</param>
        /// <returns></returns>
        public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected )
        {
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                int result;
                connection.Open();
                OracleCommand command = BuildIntCommand(connection,storedProcName, parameters );
                rowsAffected = command.ExecuteNonQuery();
                result = (int)command.Parameters["ReturnValue"].Value;
                //Connection.Close();
                return result;
            }
        }
        
        /// <summary>
        /// 建立 OracleCommand 對象實例(用來返回一個整數值)    
        /// </summary>
        /// <param name="storedProcName">存儲過程名</param>
        /// <param name="parameters">存儲過程參數</param>
        /// <returns>OracleCommand 對象實例</returns>
        private static OracleCommand BuildIntCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters)
        {
            OracleCommand command = BuildQueryCommand(connection,storedProcName, parameters );
            command.Parameters.Add( new OracleParameter ( "ReturnValue",
                 OracleDbType.Int32, 4, ParameterDirection.ReturnValue,
                false,0,0,string.Empty,DataRowVersion.Default,null ));
            return command;
        }
        #endregion    
 
    }
}

 

出處:https://blog.csdn.net/u012949335/article/details/82378210

===========================================================

今天調用webservice的時候,運行程序後開始報錯如下的錯誤信息

「/」應用程序中的服務器錯誤。

未能加載文件或程序集「Oracle.DataAccess」或它的某一個依賴項。試圖加載格式不正確的程序。
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以瞭解有關該錯誤以及代碼中致使錯誤的出處的詳細信息。 

異常詳細信息: System.BadImageFormatException: 未能加載文件或程序集「Oracle.DataAccess」或它的某一個依賴項。試圖加載格式不正確的程序。

源錯誤: 
執行當前 Web 請求期間生成了未處理的異常。可使用下面的異常堆棧跟蹤信息肯定有關異常緣由和發生位置的信息。

程序集加載跟蹤: 下列信息有助於肯定程序集「Oracle.DataAccess」沒法加載的緣由......。

而後百度上搞了好久,各類方法試遍了,最後嘗試用了Oracle.ManagedDataAccess Oracle全託管驅動,添加到項目的引用中,再配置一下Web.config,大功告成了。
下面貼上代碼,以備之後須要。
複製代碼
  <configSections>
    <section name="oracle.manageddataaccess.client"
      type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
  </configSections>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
        type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <publisherPolicy apply="no"/>
        <assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
        <bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.121.2.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
      </dataSources>
    </version>
  </oracle.manageddataaccess.client>
複製代碼
 
 

ORACLE全託管驅動Oracle.ManagedDataAccess.dll ,支持EF 支持分佈式事務,鏈接數據庫不再須要安裝oracle 客戶端了,也不用區分32位,64位了。

Oracle.ManagedDataAccess.dll下載地址:http://download.csdn.net/detail/w3800/7360331

 

出處:https://www.cnblogs.com/handsomeso/p/5623146.html

===========================================================

ODP.NET Managed web.config 配置

  <connectionStrings>
    <add name="OracleConnectionString" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=scott;Password=tiger;Data Source=MyDataSource;PERSIST SECURITY INFO=True" />
  </connectionStrings>

 

 

  <oracle.manageddataaccess.client>

    <version number="*">
      <dataSources>
        <dataSource alias="SampleDataSource"
            descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
        <dataSource alias=" MyDataSource"
             descriptor="(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = tcp)(HOST = 192.168.0.1)(PORT = 1521))  (ADDRESS = (PROTOCOL = tcp)(HOST = 192.168.0.2)(PORT = 1521))  (LOAD_BALANCE = ON) (FAILOVER = ON) ) (CONNECT_DATA =(SERVICE_NAME = oradb)(GLOBAL_NAME = oradb)(FAILOVER_MODE = (TYPE = SELECT) (METHOD = BASIC)))) " />
      </dataSources>
      <settings>
        <setting name="TNS_ADMIN" value="C:\oracle\product\10.2.0\client_1\NETWORK\ADMIN\"/>
      </settings>


    </version>

  </oracle.manageddataaccess.client>

 

c#

        public static string connectionString = ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString;// "鏈接字符串"; 

 

出處:https://blog.csdn.net/deepblue1974/article/details/52639396

==========================================================

相關文章
相關標籤/搜索