如何连接到远程Oracle数据库

时间:2013-04-05 12:56:21

标签: c# .net visual-studio-2010 oracle odp.net

我必须将远程Oracle DBMS连接到我的.NET C#Web服务

  • 请求客户端安装Oracle吗?为什么?
  • 当您必须使用ODP.NET

由于

4 个答案:

答案 0 :(得分:4)

我建议使用ODP.NET,因为它是免费的,并且是用于连接Oracle的“官方”ADO.NET兼容提供程序。 1

为了让您的用户不必单独安装Oracle客户端,请下载Oracle Instant Client,从那里获取以下文件......

oci.dll
Oracle.DataAccess.dll (the managed ODP.NET assembly itself)
orannzsbb11.dll
oraociei11.dll
OraOps11w.dll

...并随申请一起分发。

不幸的是,这些DLL中的大多数都是本机的(并且特定于32位/ 64位),因此您将无法构建“任何CPU”平台(但 2 )。

.NET代码与您在“胖”Oracle客户端下使用的代码相同(并且与其他任何ADO.NET提供程序非常相似),除非您应该考虑使用“tnsnames.ora independent”{{ 3}}例如:

Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=MyHost)(PORT=MyPort)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=MyOracleSID)));User Id=myUsername;Password=myPassword;

1 有一些商业替代方案和旧的Microsoft提供商现已弃用(并且无论如何都不会使您免于安装Oracle本机DLL)。

2 要么等待connection string,要么编辑项目文件(MSBuild XML)以有条件地包含32位或64位DLL,具体取决于构建平台,类似于:

  <Choose>
    <When Condition="'$(Platform)' == 'x64'">
      <ItemGroup>
        <Reference Include="Oracle.DataAccess, processorArchitecture=x64">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>..\ThirdParty\ODP.NET\x64\Oracle.DataAccess.dll</HintPath>
        </Reference>
        <Content Include="..\ThirdParty\ODP.NET\x64\oci.dll">
          <Link>oci.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="..\ThirdParty\ODP.NET\x64\orannzsbb11.dll">
          <Link>orannzsbb11.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="..\ThirdParty\ODP.NET\x64\oraociei11.dll">
          <Link>oraociei11.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="..\ThirdParty\ODP.NET\x64\OraOps11w.dll">
          <Link>OraOps11w.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
    <When Condition="'$(Platform)' == 'x86'">
      <ItemGroup>
        <Reference Include="Oracle.DataAccess, processorArchitecture=x86">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>..\ThirdParty\ODP.NET\x86\Oracle.DataAccess.dll</HintPath>
        </Reference>
        <Content Include="..\ThirdParty\ODP.NET\x86\oci.dll">
          <Link>oci.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="..\ThirdParty\ODP.NET\x86\orannzsbb11.dll">
          <Link>orannzsbb11.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="..\ThirdParty\ODP.NET\x86\oraociei11.dll">
          <Link>oraociei11.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
        <Content Include="..\ThirdParty\ODP.NET\x86\OraOps11w.dll">
          <Link>OraOps11w.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
  </Choose>

答案 1 :(得分:2)

我认为你可以使用ODP.NET的Oracle.DataAccess命名空间

您可以这样使用:

var _testConx = new OracleConnection(_testConnectionString);
var rezList = new List<Type>();
string _GetSQL = @"SELECT STATEMENT";
var dbCommand = new OracleCommand(_GetSQL , _testConx);
dbCommand .CommandType = CommandType.Text;
var reader = dbCommand .ExecuteReader();
while (reader.Read())
{
   var rez = new Type();
   rez.Field1= TryGetInt(reader.GetOracleValue(0));
   rez.Field2= TryGetString(reader.GetOracleValue(1));

   rezList.Add(rez);
}
return rezList;

这将使用oracle客户端连接到远程数据库。

您可以在外部资源中指定连接字符串,例如配置文件

答案 2 :(得分:1)

我们正在使用Oracle提供的OLEDB驱动程序连接到.net桌面应用程序中的远程Oracle数据库。也应该为Web服务工作。

String conString = "Provider=OraOLEDB.Oracle.1;User ID=username;password=password;Data Source=your_tnsname;Persist Security Info=False";
String query = "Select 2 from dual";
OleDbConnection OleDbCon = new OleDbConnection(conString);
OleDbCon.Open();
OleDbCommand cmd = new OleDbCommand(query, OleDbCon);
OleDbDataReader reader = cmd.ExecuteReader();
reader.Read();
decimal dResult = reader.GetDecimal(0);
con.Close();
return Convert.ToInt32(dResult);

您应该添加适当的异常处理。

答案 3 :(得分:1)

我喜欢使用System.Data.OracleClient。我知道它已被弃用,但它内置的事实使它变得简单易用。

我也喜欢使用System.Web中的SqlDataSource对象,即使在非ASP.NET应用程序中也是如此。下面是一些示例代码。然后,获取数据就像调用GetDataView()并传入select语句一样简单。您需要自己实现GetDefaultConnectionString()和GetDefaultProviderName()。提供者名称是“System.Data.OracleClient”,these应该让您开始使用连接字符串。

请注意,由于它依赖于System.Web for SqlDataSource,因此应用程序将需要整个.NET Framework 4配置文件(而不仅仅是较小的客户端配置文件)。根据您的要求,这可能是也可能不是问题。你总是可以实现自己的SqlDataSource,但我不想重新发明轮子,除非它给我一个很好的优势。

    /// <summary>
    /// Creates a SqlDataSource object using the Default connectionstring in the web.config file and returns it.
    /// </summary>
    /// <returns>An SqlDataSource that has been initialized.</returns>
    public static SqlDataSource GetDBConnection()
        {
        SqlDataSource db = new SqlDataSource();
        db.ConnectionString = GetDefaultConnectionString();
        db.ProviderName = GetDefaultProviderName();
        return db;
        }
    /// <summary>
    /// Creates a DataView object using the provided query and an SqlDataSource object.
    /// </summary>
    /// <param name="query">The select command to perform.</param>
    /// <returns>A DataView with data results from executing the query.</returns>
    public static DataView GetDataView(string query)
        {
        SqlDataSource ds = GetDBConnection();
        ds.SelectCommand = query;
        DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty);
        return dv;
        }

进行更新/插入/删除同样容易...

SqlDataSource ds=GetDBConnection();
ds.InsertCommand="insert into my_table values ('5','6')";
ds.Insert();
相关问题