使用Oracle.ManagedDataAccess连接到Oracle

时间:2015-07-22 13:16:40

标签: c# .net oracle odp.net

我在我的C#(.NET 4.0)项目中使用Oracle.ManagedDataAccess Nuget Package Version 12.1.022。该程序包会自动在app.config文件中创建条目。如何从此文件中读取数据源字符串以便能够连接到数据库?

  

System.Configuration.ConfigurationManager.AppSettings.Get("数据源&#34);

对我不起作用

for /f "delims= " %%a in ('date /t') do @set arg1=%%a
plink.exe admin@xxxx -pw *** configupload -p ftp \xxx,eccadmin,config.txt,xxx
ren config.txt %arg1%.config.txt

4 个答案:

答案 0 :(得分:14)

通常,您将在标准连接字符串中引用别名:

  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=MyDataSource;User Id=scott;Password=tiger;"/>
  </connectionStrings>

然后你将使用标准方法来检索字符串:

ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

另请注意,数据源部分是可选的。您可以将描述符直接嵌入连接字符串中:

  <connectionStrings>
    <add name="MyConnection" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)));User Id=scott;Password=tiger;"/>
  </connectionStrings>

您还可以在tnsnames.ora文件中引用别名。默认情况下,驱动程序在exe文件夹中查找tnsnames.ora,在TNS_ADMIN环境变量中指定目录,或者在TNS_ADMIN配置变量中查找:

http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId6 http://docs.oracle.com/cd/E48297_01/doc/win.121/e41125/featConfig.htm#autoId7

答案 1 :(得分:5)

您可以参考连接字符串中已定义的数据源

<oracle.manageddataaccess.client>
    <version number="*">
        <dataSources>
            <dataSource alias="MyDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=100.100.100.100)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myservice.com)))" />
        </dataSources>
    </version>
</oracle.manageddataaccess.client>
例如,在这种特殊情况下:

public class OracleDBManager
{
    private OracleConnection _con;
    private const string connectionString = "User Id={0};Password={1};Data Source=MyDataSource;";
    private const string OracleDBUser = "exampleUser";
    private const string OracleDBPassword = "examplePassword";

    public OracleDBManager()
    {
        InitializeDBConnection();
    }

    ~OracleDBManager()
    {
        if (_con != null)
        {
            _con.Close();
            _con.Dispose();
            _con = null;
        }
    }

    private void InitializeDBConnection()
    {
            _con = new OracleConnection();
            _con.ConnectionString = string.Format(connectionString, OracleDBUser, OracleDBPassword);
            _con.Open();
    }
}

答案 2 :(得分:0)

使用包Formo,我创建了以下方法来从MyDataSource文件中获取app.config字符串

using Configuration = Formo.Configuration;
.......
/// <summary>
/// Gets the data source from app.config file
/// </summary>
/// <returns></returns>
public string GetMyDataSource()
{
    dynamic config = new Configuration();
    return config.MyDataSource;
}

答案 3 :(得分:0)

你可以创建一个单独的类文件,像这样返回连接字符串 -

public static OracleConnection GetConnection()
{
  const string connectionString = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=PROD))); User Id=userId;Password=password;";
  var connection=new OracleConnection(connectionString);
  return connection;
}

然后你可以像这样调用它来访问oracle db -

var oracleConnection = OracleDbConnection.GetConnection();
oracon.Open();