C#连接Oracle数据库解决报错(需要安装Oracle客户端软件8.1.7)的问题

时间:2022-01-28 05:51:18

1.通过nuget安装  ManagedDataAccess

2.引用

using Oracle.ManagedDataAccess.Client;

注:最低framework4.0

 

测试连接:

/// <summary>
/// 测试数据库连通
/// </summary>
public static DataSet db_connection_test(string str_uid, string str_pwd, string str_serveraddr, string str_port, string str_dbname)
{
string str_conn = string.Format("user id={0};password={1};data source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST={2})(PORT={3}))(CONNECT_DATA=(SERVICE_NAME={4})))", str_uid, str_pwd, str_serveraddr, str_port, str_dbname);//连接字符串格式化
OracleConnection Conn = new OracleConnection(str_conn);//定义连接实例
try
{
Conn.Open();//通过自带的方式打开连接,测试连接实例
using (OracleDataAdapter _da = new OracleDataAdapter("select * from SYS_T_SELECT", Conn))
{
DataSet ds = new DataSet();
_da.Fill(ds);
return ds;
}
Conn.Close();
}
catch (Exception ex)
{
return new DataSet();
}
}

 

 

参数传入:

DataTable dt = DBHelper.db_connection_test("用户名", "密码", "ip", "端口", "ORCL").Tables[0];