c#不安装Oracle客户端连接Oracle
本文所讲的连结oracle是用的.net自带的System.Data.OracleClient (.net 3.5 2.0 添加引用后有, 4.0不再有了) 就是复制dll, 连结串写完整.
View Code
using System.Data;
using System.Data.OracleClient;
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string connectionString, string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (OracleException ex)
{
throw new Exception(ex.Message);
}
finally
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
return ds;
}
using System.Data.OracleClient;
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string connectionString, string SQLString)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OracleDataAdapter command = new OracleDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (OracleException ex)
{
throw new Exception(ex.Message);
}
finally
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
return ds;
}
1.到oracle官网下载oracle client instant解压后的dll来复制到你的exe文件夹中.( instantclient-basic-nt-11.2.0.2.0.zip 支持多国语言, instantclient-basiclite-nt-11.2.0.2.0.zip 只支持英文, 高版本的oracle client可连结低版本的oracle)
(或者如果你本机安装个oracle客户端, 可以在oracle客户端文件夹搜索下*oci*.dll(oci是oracle call interface)文件, 搜索的全部dll复制到你的exe文件夹中, 网上常的复制dll就是这几个dll吧)
2.连结字符串要写成(可以写到配置文件中的, 就是要写直实的地址, 端口,实例,用户名,密码)
private static string CONNECTION_STRING =
"User Id=myUserID;Password=myPassword;Data Source=(DESCRIPTION=" +
"(ADDRESS=(PROTOCOL=TCP)(HOST=myserver.server.com)(PORT=yourPort#))" +
"(CONNECT_DATA=(SID=yourSID)));";
这样就不用去读取安装客户端的tnsnames.ora中配置了
注意: 在编程, 要把刚上面的dll放到你的debug或release目录下, 就是与exe文件同目录, 不然找不到文件, 各种出错.
参见: