使用的DLL:Oracle.ManagedDataAccess
Bug:OracleInternal.Common.ProviderConfig的类型初始值设定项引发异常
App.config的更改才是关键
关键应该是这个:
oracle.manageddataaccess默认情况下只支持direct connection,
如果要支持tns name,即ez connection,需要在app.config或者web.config里添加如下设置
安装后的App.config:
更改后的App.config:
去掉的节点 如下:
代码:
OracleHelper
public class OracleHelper1 { public static OracleCommand cmd = null; public static OracleConnection conn = null; public static string connstr = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.2.98)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=JJJT)));User Id=CMSP;Password=Oracle1"; public static OracleConnection init() { try { conn = new OracleConnection(connstr); if (conn.State != ConnectionState.Open) { conn.Open(); } } catch (Exception e) { throw new Exception(e.Message.ToString()); } return conn; } public static DataSet GetDataSet(string sqlstr) { DataSet set = new DataSet(); try { init(); OracleDataAdapter adp = new OracleDataAdapter(sqlstr, conn); adp.Fill(set); conn.Close(); } catch (Exception e) { throw new Exception(e.Message.ToString()); } return set; } public static int ExecuteNonQuery(string cmdText) { int count; try { init(); cmd = new OracleCommand(cmdText, conn); count = cmd.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { throw new Exception(ex.Message.ToString()); } return count; } }
CS代码
DataTable dt1 = OracleHelper1.GetDataSet("select * from TB_PROJECTS").Tables[0];