Oracle提供了这样的Oracle.ManagedDataAccess.dll,让用可以需要安装Oracle客户端即可连接数据库,并且不区分32位和64位。
1.引用
选择引用,找到对应文件目录下的DLL引用即可。
修改连接字符串
string connString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=" + hostaddress + ")(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=" + servername + ")));Persist Security Info=True;User ID=" + uid + ";Password=" + pwd + ";";
3.连接数库的代码
public class ConnForOracle
{
protected OracleConnection Connection;
private string connectionString;
public ConnForOracle()
{
string connStr;
connStr = “改成你自己的连接字符串即可”
connectionString = connStr;
Connection = new OracleConnection(connectionString);
}
#region 带参数的构造函数
/// 带参数的构造函数
/// 数据库联接字符串
public ConnForOracle(string ConnString)
{
string connStr;
connStr = System.Configuration.ConfigurationSettings.AppSettings[ConnString].ToString();
Connection = new OracleConnection(connStr);
}
#endregion
#region 打开数据库
/// 打开数据库
public void OpenConn()
{
if (this.Connection.State != ConnectionState.Open)
this.Connection.Open();
}
#endregion
#region 关闭数据库联接
/// 关闭数据库联接
public void CloseConn()
{
if (Connection.State == ConnectionState.Open)
Connection.Close();
}
#endregion
#region 执行SQL语句,返回数据到DataSet中
/// 执行SQL语句,返回数据到DataSet中
/// sql语句
/// 自定义返回的DataSet表名
/// 返回DataSet
public DataSet ReturnDataSet(string sql, string DataSetName)
{
DataSet dataSet = new DataSet();
OpenConn();
OracleDataAdapter OraDA = new OracleDataAdapter(sql, Connection);
OraDA.Fill(dataSet, DataSetName);
// CloseConn();
return dataSet;
}
}
4.显示查询结果