• oracle数据库连接方式


    安装了Oracle Db Provider后Oracle.DataAccess.dll的路径是:D:\app\Administrator\product\11.2.0\client_1\ODP.NET\bin\2.x

    下面演示了使用OleDb, msdaora, System.Data.OracleClient.OracleConnection, Oracle.DataAccess.Client.OracleConnection 以及用DbProviderFactories.GetFactory方式来获取Connection

    另外如果是64位操作系统,需要安装64位的驱动

    代码如下:

    using System;
    using System.Data;
    using System.Data.Common;
    
    namespace WinTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                ShowAllDbProviderFactories();
    
                string connString;
    
                /* ole db provider */
                connString = "Provider=OraOledb.Oracle;Data Source=xe;User ID=sa;Password=jack2008;";
                using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
                {
                    TestConnection(conn);
                }
    
                connString = "Provider=OraOleDb.Oracle.1;Data Source=xe;User ID=sa;Password=jack2008;";
                using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
                {
                    TestConnection(conn);
                }
    
                /* ms oracle provider, it is x86 version */
                //connString = "Provider=msdaora;Data Source=xe;User ID=sa;Password=jack2008;";
                //using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
                //{
                //    TestConnection(conn);
                //}
    
                connString = "Data Source=xe;User ID=sa;Password=jack2008;";
                using (DbConnection conn = new System.Data.OracleClient.OracleConnection(connString))
                {
                    TestConnection(conn);
                }
    
                connString = "Data Source=xe;User ID=sa;Password=jack2008;";
                using (DbConnection conn = new Oracle.DataAccess.Client.OracleConnection(connString))
                {
                    TestConnection(conn);
                }
    
                string providerInvariantName;
                providerInvariantName = "Oracle.DataAccess.Client";
                connString = "Data Source=xe;User ID=sa;Password=jack2008;";
                UseDbProviderFactoryGetConnection(providerInvariantName,connString);
    
                providerInvariantName = "System.Data.OracleClient";
                connString = "Data Source=xe;User ID=sa;Password=jack2008;";
                UseDbProviderFactoryGetConnection(providerInvariantName, connString);
    
                //connString = "Data Source=xe;User ID=sa;Password=jack2008;";
                //DbProviderFactory factory = DbProviderFactories.GetFactory("");
                //using (DbConnection conn = factory.CreateConnection())
                //{
                //    conn.ConnectionString = connString;
                //    TestConnection(conn);
                //}
    
                Console.WriteLine("go");
                Console.ReadLine();
    
            }
    
            
    
            private static void ShowAllDbProviderFactories()
            {
                /* all Db Provider Factory */
                DataTable dtFactory = DbProviderFactories.GetFactoryClasses();
                Console.WriteLine("------------------------------------");
                foreach (DataRow row in dtFactory.Rows)
                {
                    //for (int i = 0; i < dtFactory.Columns.Count; i++)
                    //{
                    //    Console.Write("{0}\t", row[i]);
                    //}
                    Console.Write("{0}\t{1}", row[2], row[3]);
                    Console.WriteLine();
                }
            }
    
            private static void UseDbProviderFactoryGetConnection(string providerInvariantName, string connString)
            {
                DbProviderFactory factory = DbProviderFactories.GetFactory(providerInvariantName);
                using (DbConnection conn = factory.CreateConnection())
                {
                    conn.ConnectionString = connString;
                    TestConnection(conn);
                }
            }
    
            private static void TestConnection(DbConnection conn)
            {
                Console.WriteLine("------------------------------------");
                string sql = "SELECT * FROM dept";
                DbCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                using (IDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        Console.WriteLine("{0}\t{1}\t{2}", dr[0], dr[1], dr[2]);
                    }
                }
                conn.Close();
            }
    
            private static void AddParameter(DbCommand cmd, string paramName, object paramValue)
            {
                DbParameter p = cmd.CreateParameter();
                p.ParameterName = paramName;
                p.Value = paramValue ?? DBNull.Value;
    
                cmd.Parameters.Add(p);
            }
        }
    }
  • 相关阅读:
    Nginx负载均衡配置实例详解
    网络性能排查
    tensorflow、cuda、cudnn之间的版本对应关系
    PyPI可以使用的几个国内源
    Ceres配置(vs2013+Win10)
    vs2013+opencv3.2配置
    Ceres Solver 在win8+vs2013环境下的安装
    51Nod-1006 最长公共子序列Lcs
    Qt中OpenGL模块下将图片转化为纹理,并传入shader中
    harris角点检测
  • 原文地址:https://www.cnblogs.com/zq8024/p/2824115.html
Copyright © 2020-2023  润新知