我正在尝试使用Oracle.ManagedDataAccess.Client作为SYSDBA从C#Desktop应用程序连接到Windows 7 64x版本上的ORACLE 10g数据库。
这是我在配置文件中的连接字符串:
<add name="default" connectionString="User Id=scott;Password=tiger;DBA Privilege=SYSDBA;Data Source=(DESCRIPTION =(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = sherif-PC)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME =orcl2)));" providerName="Oracle.ManagedDataAccess.Client"/>
这是代码:
ConnectionStringSettings ConnectString = ConfigurationManager.ConnectionStrings["default"];
_dbFact = DbProviderFactories.GetFactory(ConnectString.ProviderName);
_con = _dbFact.CreateConnection();
_con.ConnectionString = ConnectString.ConnectionString;
using (_con)
{
_con.Open();
using (_cmd = _con.CreateCommand())
{
_cmd.CommandText = "select * from EMP;";
using (IDataReader reader = _cmd.ExecuteReader())
{
res = reader.GetString(1);
}
}
_con.Close();
MessageBox.Show(res);
}
我必须使用DbProviderFactory,因为它必须是针对不同种类的dbs的通用解决方案