c# - 如何连接到远程 Oracle 数据库

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

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

  • 是否要求客户端安装Oracle?为什么?
  • 当您必须使用 ODP.NET 时

谢谢

最佳答案

我建议使用 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 中的大多数都是 native 的(并且特定于 32 位/64 位),因此您将无法为“任何 CPU”平台构建(还2)。

.NET 代码与您在“胖”Oracle 客户端下使用的代码相同(并且与任何其他 ADO.NET 提供程序非常相似),除了您可能应该考虑使用“tnsnames.ora 独立”connection string 这样的作为:

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 native DLL)。

2 要么等待 Fully Managed Oracle Provider ,要么编辑您的项目文件(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>

关于c# - 如何连接到远程 Oracle 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15834561/

相关文章:

c# - 读取字节数组与 int 数组和位移位 - 哪个更快?

javascript - 如何从 C# 代码中的本地存储中检索值?

c# - 为什么 int.TryParse 不能初始化多个变量

c# - 无法成功启动或连接到子 MSBuild.exe 进程。验证 MSBuild.exe

unit-testing - 使用Visual Studio 2010进行本地构建后运行单元测试

c# - 使用具有代码访问安全性的 C# 迭代器方法时出现问题

c# - 在四核上使用线程可将代码速度提高 65%?

c# - 我可以更改默认配置文件吗?

c# - SSH.NET - 未找到合适的身份验证方法

visual-studio - 构建前强制重新加载文件