报错
下载https://files-cdn.cnblogs.com/files/yjmyzz/Oracle.ManagedDataAccess.zip地址
解决办法:
配置系统环境变量,找到路径下文件 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
先备份machine.config一份,以免出现意外状况。
查看ManagedDataAccess的版本如图
将如下配置加入节点
<section name="oracle.unmanageddataaccess.client" type="OracleInternal.Common.CustomSectionHandler, Oracle.DataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
在这个配置中对ManagedDataAccess和DataAccess都进行了配置,注意!!!对于相应配置的版本号是有可能不同的,要和自己项目中使用的ManagedDataAccess和DataAccess版本一致(如我的ManagedDataAccess的版本是:version=4.122.1.0而你们可能是:version=4.121.1.0),不然会导致以上错误
连接
连接代码
#region 连接数据库demo
private void btnOracle_Click(object sender, EventArgs e)
{
OracleConnection conn = null;
try
{
conn = OpenConn();
//查询
OracleDataAdapter oda = new OracleDataAdapter("select * from IMES.M_EMP ", conn);
DataTable dt = new DataTable();
//数据绑定到DataTable里
oda.Fill(dt);
// DataTable绑定到dataGridView
this.dgvData.DataSource = dt;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
CloseConn(conn);
}
}
/// <summary>
/// 打开数据库
/// </summary>
/// <returns></returns>
public static OracleConnection OpenConn()
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=**.**.**.**)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=****)));Persist Security Info=True;User ID=****;Password=****;";
conn.Open();
return conn;
}
/// <summary>
/// 关闭数据库
/// </summary>
/// <param name="conn"></param>
static void CloseConn(OracleConnection conn)
{
if (conn == null) { return; }
try
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
conn.Dispose();
}
}
#endregion