.net鏈接Oracle

經過網上了解到.net鏈接Oracle主要有3種方法。
(1)System.Data.OracleClient
微軟的System.Data.OracleClient能夠直接引用,可是VS會提示「System.Data.OracleClient.OracleConnection」已過期。
(2)Oracle.DataAccess.Client
也叫ODP.net,是Oracle提供的數據庫訪問類庫,不用安裝Oracle客戶端,只須要在oracle安裝目錄下找到Oracle.DataAccess.dll添加引用,可是缺點是要區分x86/x64版本。
(3)Oracle.ManagedDataAccess.Client
也是Oracle提供的數據庫訪問類庫,無需安裝oracle客戶端,也不須要區分x86/x64版本,直接引用Oracle.ManagedDataAccess.dll 便可。 sql

下面是用使用Oracle.ManagedDataAccess.Client鏈接Oracle例子:
一、配置Oracle鏈接字符串
本次例子是WinForm,app.config的Oracle鏈接字符串配置以下,關鍵信息:IP地址、服務名、用戶名、密碼。數據庫

<add key="OracleConn" value="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.2)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)));Persist Security Info=True;User ID=user;Password=123456"/>

二、簡單寫了個數據庫訪問類OracelHelper.csoracle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using System.Data;

namespace demo
{
    public class OracelHelper
    {
        private static readonly string connectionString = System.Configuration.ConfigurationManager.AppSettings["OracleConn"].ToString();

        public static OracleConnection GetConn()
        {
            var conn = new OracleConnection(connectionString);
            conn.Open();
            return conn;
        }

        public static int ExecuteNonQuery(string sql)
        {
            using (var conn = GetConn())
            {
                var cmd = new OracleCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();
                return result;
            }
        }

        public static int ExecuteScalar(string sql)
        {
            using (var conn = GetConn())
            {
                var cmd = new OracleCommand(sql, conn);
                object o = cmd.ExecuteScalar();
                return Convert.ToInt32(o.ToString());
            }
        }


        public static OracleDataReader ExecuteReader(string sql)
        {
            var conn = GetConn();
            var cmd = new OracleCommand(sql, conn);
            var myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return myReader;
        }

        public static DataSet ExecDataSet(string sql)
        {
            using (var conn = GetConn())
            {
                var cmd = new OracleCommand(sql, conn);
                OracleDataAdapter da = new OracleDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
        }
    }
}

三、若是是使用Dapper,可參考上篇文章「.net4.0使用Dapper操做MySql」,部分代碼稍微修改以下app

(1)Dapper封裝spa

 public class DapperHelper
    {
        public static OracleConnection OracleConnection()
        {
            string oracleConnectionStr = System.Configuration.ConfigurationManager.AppSettings["OracleConn"].ToString();
            var connection = new OracleConnection(oracleConnectionStr);
            connection.Open();
            return connection;
        }       
    }

(2)使用Dapper進行select查詢.net

using (IDbConnection conn = DapperHelper.OracleConnection())
            {               
                string sqlCommandStr = @"select * from user";
                List<User> userList = conn.Query<User>(sqlCommandStr).ToList();
               //todo
            }
相關文章
相關標籤/搜索