English 中文(简体)
将C#连接到Oracle数据库所需的最小客户端占用空间是多少?
原标题:
  • 时间:2008-09-16 09:11:44
  •  标签:

我已通过在笔记本电脑上下载并安装客户端管理工具和Visual Studio 2008,成功地从C#(Visual Studio 2008)连接到Oracle数据库(10g)。

Oracle客户端工具的安装占地面积超过2亿字节,而且相当冗长。

有人知道最小可行足迹是多少吗?我希望它是一个单独的DLL和一个寄存器命令,但我觉得我需要安装一个oracle home,并设置各种环境变量。

我在代码中使用Oracle.DataAccess。

最佳回答

您需要一个Oracle客户端来连接到Oracle数据库。最简单的方法是安装Oracle数据访问组件

为了最大限度地减少占地面积,我建议如下:

  • Use the Microsoft provider for Oracle (System.Data.OracleClient), which ships with the framework.
  • Download the Oracle Instant Client Package - Basic Lite : this is a zip file with (almost) the bare minimum. I recommend version 10.2.0.4, which is much smaller than version 11.1.0.6.0.
  • Unzip the following files in a specific folder :
    • v10 :
      • oci.dll
      • orannzsbb10.dll
      • oraociicus10.dll
    • v11 :
      • oci.dll
      • orannzsbb11.dll
      • oraociei11.dll
  • On a x86 platform, add the CRT DLL for Visual Studio 2003 (msvcr71.dll) to this folder, as Oracle guys forgot to read this...
  • Add this folder to the PATH environment variable.
  • Use the Easy Connect Naming method in your application to get rid of the infamous TNSNAMES.ORA configuration file. It looks like this : sales-server:1521/sales.us.acme.com.

这相当于大约19Mb(v10)。

如果您不关心在多个应用程序之间共享此文件夹,则可以将上述DLL与应用程序二进制文件一起发送,并跳过PATH设置步骤。

如果您绝对需要使用Oracle提供程序(Oracle.DataAccess),则需要:

  • ODP .NET 11.1.0.6.20 (the first version which allegedly works with Instant Client).
  • Instant Client 11.1.0.6.0, obviously.

请注意,我还没有测试过这个最新的配置。。。

问题回答

我在Windows XP上使用Pandicus建议的方法,使用ODAC 11.2.0.2.1。步骤如下:

  1. Download the "ODAC 11.2 Release 3 (11.2.0.2.1) with Xcopy Deployment" package from oracle.com (53 MB), and extract the ZIP.
  2. Collect the following DLLs: oci.dll (1 MB), oraociei11.dll (130 MB!), OraOps11w.dll (0.4 MB), Oracle.DataAccess.dll (1 MB). The remaining stuff can be deleted, and nothing have to be installed.
  3. Add a reference to Oracle.DataAccess.dll, add using Oracle.DataAccess.Client; to your code and now you can use types like OracleConnection, OracleCommand and OracleDataReader to access an Oracle database. See the class documentation for details. There is no need to use the tnsnames.ora configuration file, only the connection string must be set properly.
  4. The above 4 DLLs have to be deployed along with your executable.

截至2014年,OPD.NET托管驱动程序的占地面积最小。

Here is a code usage comparison to the non-managed versions that previous (outdated) answers suggested: http://docs.oracle.com/cd/E51173_01/win.122/e17732/intro005.htm#ODPNT148

You will need to download these dlls and reference Oracle.ManagedDataAccess.dll in your project: Download the ODP.NET, Managed Driver Xcopy version only

以下是您需要在发布时打包的典型足迹:

  1. Oracle.ManagedDataAccess.dll
  2. Oracle.ManagedDataAccessDTC.dll

所有这些加在一起,.Net 4.0的高达6.4 MB。

通过这种方式,您可以使用oracle中的5个可重新分发的文件连接ODP.net:

Chris的博客条目:使用新的ODP.Net通过简单的部署从C#访问Oracle

编辑:如果博客每次都宕机,这里有一个简短的摘要。。。

  • oci.dll
  • Oracle.DataAccess.dll
  • oraociicus11.dll
  • OraOps11w.dll
  • orannzsbb11.dll
  • oraocci11.dll
  • ociw32.dll

确保所有这些DLL都来自同一ODP.Net/ODAC发行版,以避免版本号冲突,并将它们与EXE放在同一文件夹中

开发艺术http://www.devart.com/,以前的CoreLab(crlab.com)提供一个pure-C#Oracle客户端。这是一个单独的dll,它运行良好。

这是Oracle 11.2.0.4.0的更新。我在Windows 7上使用System.Data.OracleClient成功完成了以下过程。

1.下载即时客户端软件包-基本精简版Windows 32位64位

2.将以下文件复制到系统路径中的某个位置:

32-Bit

 1,036,288  2013-10-11  oci.dll
   348,160  2013-10-11  ociw32.dll
 1,290,240  2013-09-21  orannzsbb11.dll
   562,688  2013-10-11  oraocci11.dll
36,286,464  2013-10-11  oraociicus11.dll

64位

   691,712  2013-10-09  oci.dll
   482,304  2013-10-09  ociw32.dll
 1,603,072  2013-09-10  orannzsbb11.dll
 1,235,456  2013-10-09  oraocci11.dll
45,935,104  2013-10-09  oraociicus11.dll

3.构造一个省略了tnsnames.ora的需要。

(请参阅下面测试程序中的示例。)

4.运行这个最低限度的C#程序来测试您的安装:

using System;
using System.Data;
using System.Data.OracleClient;

class TestOracleInstantClient
{
    static public void Main(string[] args)
    {
        const string host = "yourhost.yourdomain.com";
        const string serviceName = "yourservice.yourdomain.com";
        const string userId = "foo";
        const string password = "bar";

        var conn = new OracleConnection();

        // Construct a connection string using Method 1 or 2.
        conn.ConnectionString =
            GetConnectionStringMethod1(host, serviceName, userId, password);

        try
        {
            conn.Open();
            Console.WriteLine("Connection succeeded.");
            // Do something with the connection.
            conn.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Connection failed: " + e.Message);
        }
    }

    static private string GetConnectionStringMethod1(
        string host,
        string serviceName,
        string userId,
        string password
        )
    {
        string format =
            "SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)" +
            "(HOST={0})(PORT=1521))" +
            "(CONNECT_DATA=(SERVER=DEDICATED)" +
            "(SERVICE_NAME={1})));" +
            "uid={2};" +
            "pwd={3};"; // assumes port is 1521 (the default)

        return String.Format(format, host, serviceName, userId, password);
    }

    static private string GetConnectionStringMethod2(
        string host,
        string serviceName,
        string userId,
        string password
        )
    {
        string format =
            "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)" +
            "(HOST={0})(PORT=1521))" +
            "(CONNECT_DATA=(SERVER=DEDICATED)" +
            "(SERVICE_NAME={1})));" +
            "User Id={2};" +
            "Password={3};"; // assumes port is 1521 (the default)

        return String.Format(format, host, serviceName, userId, password);
    }
}

最后提示:如果遇到错误“System.Data.OracleClient需要Oracle客户端软件版本8.1.7”,请参阅这个问题

ODAC xcopy will get you away with about 45MB. http://www.oracle.com/technology/software/tech/windows/odpnet/index.html

我发现甲骨文论坛上的这篇文章也很有用:

How to setup Oracle Instant Client with Visual Studio

备注:ADO.NET团队正在弃用System.Data.OracleClient,因此对于未来的项目,您应该使用ODP.NET

繁殖:

设置以下环境变量:

  1. make sure no other oracle directory is in your PATH
  2. set your PATH to point to your instant client
  3. set your TNS_ADMIN to point to where you tnsnames.ora file is located
  4. set your NLS_LANG
  5. set your ORACLE_HOME to your instant client

对我来说,我将NLS_LANG设置为

http://download-east.oracle.com/docs/html/A95493_01/gblsupp.htm#634282

我通过使用即时客户端的sqlplus插件验证了这是使用了正确的客户端软件。

For me, I set: SET NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252

注意:在进行任何更改之前,请备份Oracle注册表项(如果存在)并备份任何环境变量的字符串。

在此处阅读Oracle Instant Client常见问题解答





相关问题
热门标签