尝试使用最小设置配置从 C# 程序连接到 Oracle 10g 数据库时出错

发布于 2024-12-22 11:57:18 字数 4132 浏览 2 评论 0原文

我在尝试从我正在开发的 C# 2008 Express Edition 应用程序连接到远程 Oracle 10g 数据库时遇到错误。我正在尝试使用极简、非侵入式的开发方法,以便将 ClickOnce 部署到用户工作站。

关于上述内容,我调查了以下文档(其中包括......) -

使用 Oracle 客户端 11 部署 .NET 应用程序所需的最低设置是什么?

http://jeremybranham.wordpress.com/2011 /04/25/oracle-instant-client-with-odp-net/

http://ora-00001.blogspot.com/ 2010/01/odpnet-minimal-non-intrusive-install.html

http://splinter.com.au/using- the-new-odpnet-to-access-oracle-from-c

使用 odp.net 和 OCI 连接到 Oracle来自 C#

鉴于我遇到的错误,我创建了一个简单的测试应用程序。由带有一个按钮的单个 (wpf) 页面组成。 在按钮的单击事件中,我尝试创建与 Oracle 数据库的连接 -

private void button1_Click( object sender, RoutedEventArgs e )
{
    OracleConnection oraConnect;

    // string previously used OK in other projects
    string connectionString = "Data Source=" +
           "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = myServer)(PORT = 1521)))" +
           "(CONNECT_DATA =(SERVICE_NAME = myOracleDb)))" +
           ";Password=myPw;User ID=myID;";

    using ( oraConnect = new OracleConnection( connectionString ) )
    {
        try
        {
            if ( oraConnect.State == ConnectionState.Closed )
            {
                oraConnect.Open();
                MessageBox.Show( "oraConnect is attempting to open.." );
            }
            else
                MessageBox.Show( "oraConnect open to DB: " + oraConnect.ServerVersion.ToString() );
        }
        catch ( NullReferenceException nullExcept )
        {
            MessageBox.Show( "Caught error: ." + nullExcept.ToString() );
        }
        catch ( OracleException dbEx )
        {
            MessageBox.Show( "OraException - " + dbEx.Message.ToString());
        }
        catch ( Exception ex )
        {
            Exception current;
            current = ex;

            while ( current != null )
            {
                current = current.InnerException;
            }

            MessageBox.Show( "Db base exception - " + ex.GetBaseException().ToString() );
        }
        finally
        {
            oraConnect.Close();
        }
    }
}

按照上述文章中的信息,我确保以下 Dll 位于我的“bin”文件夹中 -

• oci.dll
• ociw32.dll
• orannzsbb10.dll
• oraocci10.dll
• oraociicus.dll
• msvcr71.dll

(在绝望中最后命名的...)并引用了“Oracle.DataAccess.dll”。

错误消息(在“catch ( OracleException dbEx )”处)是 -

"Oracle.DataAccess.Client.OracleException was caught
  Message=""
  StackTrace:
       at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure)
       at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src)
       at Oracle.DataAccess.Client.OracleConnection.Open()
       at OracleConnectionTest.Window1.button1_Click(Object sender, RoutedEventArgs e) in C:\Documents\Visual Studio 2008\Projects\OracleConnectionTest\OracleConnectionTest\Window1.xaml.cs:line 69
  InnerException: "

Line 69 is 'oraConnect.Open();'.

此外,还报告了以下内容 -

"((Oracle.DataAccess.Client.OracleException)($exception)).DataSource' threw an exception of type 'System.NullReferenceException".

我从数据源中的 NullReferenceException 假设问题在于 dll 之一(?),因为我“新建” ' 在尝试引用上面的 OracleConnection 之前。

另外,代码执行会跳转‘catch(NullReferenceException nullExcept)’ 并直接进入 OracleException 捕获。

抱歉胡言乱语,但希望这是有道理的? 任何帮助/建议表示赞赏!

I'm experiencing an error when trying to connect to a remote Oracle 10g database from a C# 2008 Express Edition application I'm developing. I'm trying to use a minimalist, non-intrusive approach to the development with a view to ClickOnce deployment to user workstations.

In respect of the above I've investigated the following documents (amongst others..) -

What is the minimal setup required to deploy a .NET application with Oracle client 11?

http://jeremybranham.wordpress.com/2011/04/25/oracle-instant-client-with-odp-net/

http://ora-00001.blogspot.com/2010/01/odpnet-minimal-non-intrusive-install.html

http://splinter.com.au/using-the-new-odpnet-to-access-oracle-from-c

Connect to Oracle with odp.net and the OCI from C#

In view of the error I've experienced I've created a simple test app. consisting of a single (wpf) page with one button.
In the click-event of the button I attempt to create a connection to an Oracle database -

private void button1_Click( object sender, RoutedEventArgs e )
{
    OracleConnection oraConnect;

    // string previously used OK in other projects
    string connectionString = "Data Source=" +
           "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = myServer)(PORT = 1521)))" +
           "(CONNECT_DATA =(SERVICE_NAME = myOracleDb)))" +
           ";Password=myPw;User ID=myID;";

    using ( oraConnect = new OracleConnection( connectionString ) )
    {
        try
        {
            if ( oraConnect.State == ConnectionState.Closed )
            {
                oraConnect.Open();
                MessageBox.Show( "oraConnect is attempting to open.." );
            }
            else
                MessageBox.Show( "oraConnect open to DB: " + oraConnect.ServerVersion.ToString() );
        }
        catch ( NullReferenceException nullExcept )
        {
            MessageBox.Show( "Caught error: ." + nullExcept.ToString() );
        }
        catch ( OracleException dbEx )
        {
            MessageBox.Show( "OraException - " + dbEx.Message.ToString());
        }
        catch ( Exception ex )
        {
            Exception current;
            current = ex;

            while ( current != null )
            {
                current = current.InnerException;
            }

            MessageBox.Show( "Db base exception - " + ex.GetBaseException().ToString() );
        }
        finally
        {
            oraConnect.Close();
        }
    }
}

Following the information in the above articles I've ensured that the following Dll's are in my "bin" folder -

• oci.dll
• ociw32.dll
• orannzsbb10.dll
• oraocci10.dll
• oraociicus.dll
• msvcr71.dll

(the last named in desperation...) and have referenced 'Oracle.DataAccess.dll'.

The error message (at 'catch ( OracleException dbEx )') is -

"Oracle.DataAccess.Client.OracleException was caught
  Message=""
  StackTrace:
       at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure)
       at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src)
       at Oracle.DataAccess.Client.OracleConnection.Open()
       at OracleConnectionTest.Window1.button1_Click(Object sender, RoutedEventArgs e) in C:\Documents\Visual Studio 2008\Projects\OracleConnectionTest\OracleConnectionTest\Window1.xaml.cs:line 69
  InnerException: "

Line 69 is 'oraConnect.Open();'.

In addition, the following is reported -

"((Oracle.DataAccess.Client.OracleException)($exception)).DataSource' threw an exception of type 'System.NullReferenceException".

I'm assuming from the NullReferenceException within the datasource that the problem lies in one of the dlls' (?) as I 'new' the OracleConnection above before trying to reference it.

In addition, the code execution jumps the 'catch ( NullReferenceException nullExcept )'
and goes straight to the OracleException catch.

Sorry for rambling on but hope this makes sense?
Any help/advice appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡的优雅 2024-12-29 11:57:18

好吧,很晚才回到这个话题,对此深表歉意!

在此期间,我们的数据库已升级(!),并在更改 dll 列表以包括

  • “10”版本中的oraocci11.dll
  • oraociccus11.dll
  • OraOps11w.dll
  • orannzsbb1.dll

时,仍然没有成功,我编辑了 App.xaml (在此处和网络上进行广泛搜索后)使用以下内容 -

    <system.data>
      <DbProviderFactories>
         <add name="OracleClient Data Provider"
              invariant="System.Data.OracleClient"
              description=".Net Framework Data Provider for Oracle"
              type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=##################"/>
      </DbProviderFactories>
   </system.data>

   <!-- publicKeyToken obtained using Reflector to investigate dll -->
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="Oracle.DataAccess"
                              publicKeyToken="##################"
                              culture="neutral"/>
            <bindingRedirect oldVersion="10.2.0.100" 
                             newVersion="2.112.2.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>

BindingRedirect 成功了!

该 dll 似乎极其依赖于兼容的版本号

希望我能说我真的理解如何这是如何工作的,但它确实如此,而且我现在已经有了工作连接......

OK, very late getting back to this for which many apologies!

In the interim, our DB has been upgraded(!) and in changing the dll list to include -

  • oraocci11.dll
  • oraociccus11.dll
  • OraOps11w.dll
  • orannzsbb1.dll

from the '10' versions, still with no success, I edited App.xaml (following an extensive search here and on the web) with the following -

    <system.data>
      <DbProviderFactories>
         <add name="OracleClient Data Provider"
              invariant="System.Data.OracleClient"
              description=".Net Framework Data Provider for Oracle"
              type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=##################"/>
      </DbProviderFactories>
   </system.data>

   <!-- publicKeyToken obtained using Reflector to investigate dll -->
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="Oracle.DataAccess"
                              publicKeyToken="##################"
                              culture="neutral"/>
            <bindingRedirect oldVersion="10.2.0.100" 
                             newVersion="2.112.2.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>

The bindingRedirect did the trick!

The dll's appear to be extremely dependent on compatible version numbers

Wish I could say I really understood how this works but it does and I now have working connections...

你对谁都笑 2024-12-29 11:57:18

这发生在我身上。

经过一番巫术之后,我从注册表中删除了这个键:HKEY_CURRENT_USER\Software\ORACLE,一切又恢复正常了。

It happened to me.

After a bit of voodoo, I deleted this key from my registery : HKEY_CURRENT_USER\Software\ORACLE and everything worked fine again.

氛圍 2024-12-29 11:57:18

确保 ODAC 设置正确。我建议使用 TNSNAMES (在我看来,您不应该在连接字符串中包含所有这些信息)。请参阅本文档 (11.2) 的 TNSNAMES 设置部分。另请参阅常见连接问题的底部部分

一旦完成,它应该像将连接字符串放入项目的设置属性中并执行操作一样简单:

oraConnect = new OracleConnection(Properties.Settings.Default.MyConnString);

在您的示例中,您的 oraConnect 尚未实例化(您只有“OracleConnection oraConnect ”),因此失败的“new OracleConnection”部分会导致空引用异常(如果我理解你对其中断位置的解释)。 VS 中的调试器也应该在这里提供帮助;)

编辑:
您可能想设置一个简单的测试控制台应用程序,只需打开/关闭连接。这可能会消除除正确设置 ODAC 之外的任何噪音。像(未经测试,假设 tnsnames 设置):

使用...

namespace Testbed {
  class Program {
    static void Main(string[] args) {
      try {
        string connStr="User Id=my_user;Password=my_pass;Data Source=my_sid;";
        OracleConnection oraConnect = new OracleConnection(connStr);
        oraConnect.Open();
        Console.WriteLine("Opened Connection");
        oraConnect.Close();
        Console.WriteLine("Complete");
        Console.ReadLine();
      catch (System.Exception e) {
        Console.WriteLine(e.Message);
        Console.ReadLine();
      } ...

尝试运行它并报告从控制台返回的内容。

Make sure ODAC is setup properly. I would suggest to use TNSNAMES (you shouldn't have all that info in a connection string imo). See the TNSNAMES setup section of this document (11.2). Also see bottom section for common connection issues

Once that's done, it should be as easy as putting your connection string in the settings property of your project and doing:

oraConnect = new OracleConnection(Properties.Settings.Default.MyConnString);

In your example, your oraConnect hasn't been instantiated (you just have "OracleConnection oraConnect"), so the "new OracleConnection" part that fails results in a null reference exception (if I understand your explanation of where its breaking anyway). Debugger in VS should help here as well ;)

EDIT:
You may want to setup a simple test console app with just open/close connection. This might eliminate any noise other than getting your ODAC setup properly. Something like (untested, assume tnsnames setup):

using ...

namespace Testbed {
  class Program {
    static void Main(string[] args) {
      try {
        string connStr="User Id=my_user;Password=my_pass;Data Source=my_sid;";
        OracleConnection oraConnect = new OracleConnection(connStr);
        oraConnect.Open();
        Console.WriteLine("Opened Connection");
        oraConnect.Close();
        Console.WriteLine("Complete");
        Console.ReadLine();
      catch (System.Exception e) {
        Console.WriteLine(e.Message);
        Console.ReadLine();
      } ...

Try running that and report what comes back from console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文