C# 连接 Oracle 的几种方式
一:通过System.Data.OracleClient(需要安装Oracle客户端并配置tnsnames.ora)
- 添加命名空间System.Data.OracleClient引用
using System.Data.OracleClient;
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)
添加命名空间System.Data.OracleClient引用
using System.Data.OracleClient;
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公司的驱动
添加命名空间System.Data.OracleClient引用
using System.Data.OleDb;
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驱动
添加命名空间System.Data.OracleClient引用
using System.Data.OleDb;
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 Files\Common Files\System\Ole DB\msdaora.dll
b.该驱动需要Oracle客户端的三个文件(oraocixe10.dll、oci.dll、ociw32.dll)放在System32下即可
五:使用ODP连接
下载安装ODP.NET
安装完全成后会产生一序列文件。
找到这个安装目录,打开文件夹%ORACLE_HOME%\Network\Admin在这个下面建立一个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描述符中拷过来
引用Oracle.DataAccess命名空间
using Oracle.DataAccess.Client;
示例代码:
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;
引用Devart.Data.Oracle命名空间
using Devart.Data.Oracle;
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();
}
方式1:直接利用.NET的oracle驱动连接
安装Oracle客户端驱动包
找到服务器端安装路径:E:oracleproduct10.2.0db_1NETWORKADMIN下tnsnames.ora,把该文件覆盖客户端驱动目下该文件
配置客户端驱动
1、打开数据源管理
2、选择系统DSN,点击[添加]
3、弹出的界面中选择Oracle in OraDB10g_1驱动,弹出Oracle ODBC Driver Configuration界面
4、填写四个数据项Data Source Name TNS Services Name User ID
其中TNS Servers Name指的是服务器端设置服务名称
这个部分可以省略
设置完成之后确定
引用System.data.oracleclient;
复制代码 代码如下:
using System.data.oracleclient;
string oradb = "Data Source=water;User Id=modis;Password=modis;Integrated Security=no";
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
(我的环境为VS2005,oracle数据源连接只支持到10g以下。)