一:通过System.Data.OracleClient(需要安装Oracle客户端并配置tnsnames.ora)
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OracleClient;
3.
string connString = "User ID=IFSAPP;Password=IFSAPP;Data Source=RACE;";
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
二:通过System.Data.OracleClient(需要安装Oracle客户端不需配置tnsnames.ora)
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OracleClient;
3.
string connString = "User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
三:通过System.Data.OleDb和Oracle公司的驱动
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OleDb;
3.
string connString = "Provider=OraOLEDB.Oracle.1;User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";
OleDbConnection conn = new OleDbConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
四:通过System.Data.OleDb和微软公司的Oracle驱动
1. 添加命名空间System.Data.OracleClient引用
2. using System.Data.OleDb;
3.
string connString = "Provider=MSDAORA.1;User ID=IFSAPP;Password=IFSAPP;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = RACE)))";
OleDbConnection cnn = new OleDbConnection(connString);
try
{
conn.Open();
MessageBox.Show(conn.State.ToString());
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
备注:
a.XP操作系统已经安装了微软公司的Oracle驱动C:Program FilesCommon FilesSystemOle DBmsdaora.dll
b.该驱动需要Oracle客户端的三个文件(oraocixe10.dll、oci.dll、ociw32.dll)放在System32下即可
五:使用ODP连接
1. 下载安装ODP.NET(http://www.oracle.com/technetwork/developer-tools/visual-studio/downloads/index.html)
2. 安装完全成后会产生一序列文件。
3. 找到这个安装目录,打开文件夹%ORACLE_HOME%NetworkAdmin在这个下面建立一个tnsnames.ora的文件,其内容可以参考其下的Sample目录下面的配置
Oracle.RACE =
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=127.0.0.1)
(PORT=1521)
)
)
(CONNECT_DATA=
(SID=RACE)
(SERVER=DEDICATED)
)
)
Oracle.RACE为连接字符串名称,可以随便取。等号后面的字符串可以在Enterprise Manager Console工具中连接数据库后的TNS描述符中拷过来
4. 引用Oracle.DataAccess命名空间
5. using Oracle.DataAccess.Client;
6. 示例代码:
string connString = "DATA SOURCE=Oracle.RACE;PERSIST SECURITY INFO=True;USER ID=IFSAPP;password=IFSAPP";
OracleConnection conn = new OracleConnection(connString);
try
{
conn.Open();
OracleCommand cmd = new OracleCommand(cmdText,conn);
OracleDataReader reader = cmd.ExecuteReader();
this.DataGridView1.DataSource = reader;
this.DataGridView1.DataBind();
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
六:使用第三方驱动
第三方驱动有 Devart,下载驱动 http://www.devart.com/dotconnect/oracle/,但是是商业版,需要购买许可或破解
连接格式 User ID=myUsername;Password=myPassword;Host=ora;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;
1. 引用Devart.Data.Oracle命名空间
2. using Devart.Data.Oracle;
3.
OracleConnection conn = new OracleConnection();
conn.ConnectionString = "";
conn.Unicode = true;
conn.UserId = "IFSAPP";
conn.Password = "IFSAPP";
conn.Port = 1521;
conn.Server = "127.0.0.1";
conn.Sid = "RACE";
try
{
conn.Open();
//execute queries, etc
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message.ToString());
}
finally
{
conn.Close();
}
七、另外的东东
在vs2010中使用c# 连接oracle 11g:
一. 在VS2010中连接 Oracle数据库有两种方法:
第一种:微软提供的连接方法 : using System.Data.OracleClient;
第二种:Oracle自己提供的方法:using Oracle.DataAccess.Client;
连接字符串:
connectionString="Password=czh;User ID=czh;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXX.XXX.XXX.XXX)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=skydream)));"
1. 微软提供的连接方法 : using System.Data.OracleClient;
测试例程:
··1.在VS2010新建控制台应用程序(C#);
··2.右键、引用,在.NET中选择System.Data.OracleClient;
··3.在程序中 using System.Data.OracleClient;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OracleClient; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string connectionString; string queryString; connectionString = "Data Source=202.200.136.125/orcl;User ID=openlab;PassWord=open123"; queryString = "SELECT * FROM T_USER"; OracleConnection myConnection = new OracleConnection(connectionString); OracleCommand myORACCommand = myConnection.CreateCommand(); myORACCommand.CommandText = queryString; myConnection.Open(); OracleDataReader myDataReader = myORACCommand.ExecuteReader(); myDataReader.Read(); Console.WriteLine("email: " + myDataReader["EMAIL"]); myDataReader.Close(); myConnection.Close(); } } }
2.Oracle自己提供的方法:using Oracle.DataAccess.Client;
前提条件:安装oracle或者oracle client以及安装 Oracle Client 。
··1.在VS2010新建控制台应用程序(C#);
··2.右键、引用,在.NET/组件中选择Oracle.DataAccess.Client;如果找不到则选择 浏览,进入到oracleclient的安装目录寻找 Oracle.Data.Access.dll (典型目录为:E:appAdministratorproduct11.2.0client_1ODP.NETin2.xOracle.Data>Access.dll)
··3.程序中添加引用:using Oracle.DataAccess.Client;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Oracle.DataAccess.Client; namespace testConnectionOracle { class Program { static void Main(string[] args) { string connectionString; string queryString; connectionString = "Data Source=202.200.155.123/orcl;User ID=openlab;PassWord=open123"; queryString = "SELECT * FROM T_USER"; OracleConnection myConnection = new OracleConnection(connectionString); OracleCommand myORACCommand = myConnection.CreateCommand(); myORACCommand.CommandText = queryString; myConnection.Open(); OracleDataReader myDataReader = myORACCommand.ExecuteReader(); myDataReader.Read(); Console.WriteLine("email: " + myDataReader["EMAIL"]); myDataReader.Close(); myConnection.Close(); } } }