C#连接Oracle数据库

C#连接Oracle数据库

连接字符串:
1.OleDB连接驱动OraOLEDB.Oracle
    Oracle公司提供的OleDB驱动,Provider需要根据实际情况修改,支持32bit与64bit,需要安装Oracle Data Provider for OLE DB.
    连接字符串:
    connectionString="provider=OraOLEDB.Oracle;Persist Security Info=False;User ID=UserName;Data Source=DataBase;Extended Properties='';Password=Password"
    或
    connectionString="provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=UserName;Data Source=DataBase;Extended Properties='';Password=Password"

2.OleDB连接驱动MSDAORA
    Microsoft公司提供的OleDB驱动,Provider需要根据实际情况修改,只支持32bit.
    连接字符串:
    connectionString="Provider=MSDAORA;Data Source=dataSource;User Id=userid;Password=password;"
    或
    connectionString="Provider=MSDAORA.1;Data Source=dataSource;User Id=userid;Password=password;"

3.OracleClient连接
    Microsoft公司提供,支持32bit与64bit,已建议作废.
    连接字符串:
    connectionString="Server=tnsnames;User Id=userid;Password=password;" providerName="System.Data.OracleClient"


对于各种数据库的连接方式,可参考www.connectionstrings.com

 

连接示例1:

       public static bool TestDB()
        {
            string connString = "Provider=MSDAORA;Data Source=dataSource;User Id=userid;Password=password;";
            try
            {
                DbConnection conn = new System.Data.OleDb.OleDbConnection(connString);
                try
                {
                    conn.Open();
                    conn.Close();
                    MessageBox.Show("连接正常", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, e.Source + e.TargetSite, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                finally
                {
                    conn.Dispose();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, e.Source + e.TargetSite, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
        }


 

连接示例2:

       public static bool TestDB()
        {
            string connString = "Provider=MSDAORA;Data Source=dataSource;User Id=userid;Password=password;";
            try
            {
                using (DbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
                {
                    conn.Open();
                    conn.Close();
                    MessageBox.Show("连接正常", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, e.Source + e.TargetSite, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
        }


 

你可能感兴趣的:(C#连接Oracle数据库)