【问题标题】:When opening an oracle connection, connection object is null打开oracle连接时,连接对象为空
【发布时间】:2016-01-20 18:02:30
【问题描述】:

我正在尝试连接到控制器中的 Oracle 数据库:

 using Oracle.DataAccess.Client;
 using Oracle.DataAccess.Types;

 // Other code

 OracleConnection con;
 con = new OracleConnection();

 con.ConnectionString = "DATA SOURCE=<DSOURCE_NAME>;PERSIST SECURITY INFO=True;USER ID=******;PASSWORD=*******";

 con.Open();

连接测试成功,我可以在 Visual Studio 的服务器资源管理器中浏览表、函数等,但是当我尝试执行上述代码时,我始终在 con.Open(); 行上得到[NullReferenceException: Object reference not set to an instance of an object.]

这是我第一次使用 ODP for .NET,而且我通常也是 C# 新手。关于可能是什么问题的任何想法?

堆栈跟踪:

    Source Error: 


Line 27: 
Line 28: 
Line 29:             con.Open();
Line 30: 

Source File: c:\Program Files\Project\source\Project\Project\Areas\Custom\Controllers\HomeController.cs    Line: 29 

 Stack Trace: 


    [NullReferenceException: Object reference not set to an instance of an object.]
   Oracle.DataAccess.Client.OracleConnection.Open() +12156
   Project.Areas.Custom.Controllers.HomeController.Id() in c:\Program Files\Project\source\Project\Project\Areas\Custom\Controllers\HomeController.cs:29
   lambda_method(Closure , ControllerBase , Object[] ) +101
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
   System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.Mvc.<>c__DisplayClassa.<EndProcessRequest>b__9() +44
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +34
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +69
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action) +123
   System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) +133
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1556

[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +3424518
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +77
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +29
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +24
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +977
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +112
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName) +47
   ASP._Page_Areas_Custom_Views_Home_Home_cshtml.Execute() in c:\Program Files\Project\source\Project\Project\Areas\Custom\Views\Home\Home.cshtml:13
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +270
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122
   System.Web.WebPages.StartPage.RunPage() +63
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +100
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9711525
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

  • 您应该在较小的测试环境中进行测试。创建一个minimal reproducible example
  • 请在 open() 调用周围添加一个 try catch 块,并在 OracleException 中提供 ORA 错误。
  • @ChristianShay 抱歉,我刚刚回复您。我添加了 try catch 块,它返回 ORA12154 - TNS 无法解析。经过一番研究,我在我的 ODP for .NET Oracle 主路径中的 tnsnames.ora 文件中添加了一个 SID,并且它起作用了!如果您创建一个答案,我很乐意为您标记答案(尽管问题可能应该更改为有关 TNS 的内容)
  • 请贴出整个控制器方法。你应该得到一个错误。我认为你在异步模式中做错了什么。

标签: c# .net oracle odp.net


【解决方案1】:

在 open() 调用周围添加一个 try catch 块并解决出现在 OracleException 中的 ORA 错误。

【讨论】:

  • 我添加了 try catch 块,它返回 ORA12154 - TNS 无法解析。经过一番研究,我在我的 ODP for .NET Oracle 主路径中的 tnsnames.ora 文件中添加了一个 SID,它可以正常工作
  • @Dan 您的术语有误。这是一个“连接标识符”。 SID 是连接标识符中的一个可能参数,您可以改用服务名称。
【解决方案2】:

我在开始使用 ODP.NET 时遇到了同样的问题。

你可以像这样调整你的代码:

try
{
    OracleConnection con;
    con = new OracleConnection();

    con.ConnectionString = "DATA SOURCE=<DSOURCE_NAME>;PERSIST SECURITY INFO=True;USER ID=******;PASSWORD=*******";

    con.Open();
}
catch (OracleException ex)
{
    Console.WriteLine("Oracle Exception Message");
    Console.WriteLine("Exception Message: " + ex.Message);
    Console.WriteLine("Exception Source: " + ex.Source);
}
catch (Exception ex)
{
    Console.WriteLine("Exception Message");
    Console.WriteLine("Exception Message: " + ex.Message);
    Console.WriteLine("Exception Source: " + ex.Source);
}

您可以在此处获取有关您的错误的更多信息:ORA-12154

问题是您的连接字符串中的Data Source。我假设它看起来像这样:Data Source=Server.Source,您可以在计算机上的 TNSNAMES.ORA 文件中找到。

问题在于 ODP.NET 不像 Visual Studio 那样读取 TNSNAMES.ORA 文件。

你有多种选择来解决这个问题:

  1. 编辑您的 Web.ConfigApp.config 文件,告诉 ODP.NET 如何处理您的源代码。
  2. TNSNAMES.ORA 文件复制到与.exe 相同的目录中。 (如果不是网络应用)
  3. 更改Data Source 以便写入长版本而不是别名。例如。 :Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=sales-server)......)))

我最喜欢的方法是#3。遇到问题时调试起来会容易得多。

您可以在Configuring Oracle Data Provider for .NET 文档的数据源部分中找到更多信息。

【讨论】:

  • “ODP.NET 不读取 TNSNAMES.ORA 文件”是不正确的。它使用“TNS_ADMIN”变量来查找它,这取决于您安装 ODP.NET 的方式,可能会在安装时在 machine.config 中设置(OUI 机器范围安装),或者根本不设置(Nuget)。当 TNS_ADMIN 未设置时,您的应用程序还将读取在您的应用程序工作目录中找到的 tnsnames .ora 文件。有关详细信息,请参阅文档。
  • 您阅读完整答案了吗?你错过了我句子的最后一部分,它“就像 Visual Studio 所做的那样”解释了为什么连接测试有效,但它不在代码中。然后,对于所有 3 个选项,我从我在答案中链接的文档中获得了它们!是的,ODP.NET 使用“TNS_ADMIN”变量(这是我的观点 #1)。 :-)
  • @ChristianShay 如果TNS_ADMIN 未设置,它不会也检查ORACLE_HOME 吗?
【解决方案3】:

我认为某处存在过度尝试,或者堆栈让我认为这是一个异步控制器调用,但您没有使用异步方法(“一直异步”)。

解释:

这里发生了其他事情。虽然我很高兴有关 try/catch 块的建议可以帮助您解决问题,但简化代码块不应该要求它们 - 应该抛出 ora 异常来代替空引用异常。

【讨论】:

  • 我明白你的意思,但我仍然有答案。我把那部分放在第一位。
  • 其他两个答案传播了一个普遍的想法,即将问题包装在 try/catch 中以获取更多信息是这里的解决方案,这绝对不正确。我可以对他们俩发表评论并肯定地说。但与此同时,其他人看到了这两个答案。我宁愿增加阻止这种传播的机会并发布另一个答案。如果 OP 想发布更多详细信息,我很乐意进一步探索。
  • 不确定投反对票的 cmets 去了哪里,但如果他在删除 cmets 时也删除他的投反对票,那就太好了;)。
  • 可能只是像我一样对 Oracle 感到沮丧的人。 (我没有投反对票。)尽管其他答案无助于找到解决方案,但这个答案也无济于事。
  • 这确实有帮助。也许发布您自己的问题,我会再拍一张;)。我很乐意提供帮助,但很明显,这个 q 在没有完整故事的情况下被遗弃了。
【解决方案4】:

在我的版本中,Oracle Path 变量设置错误(因为之前安装了另一个 Oracle 客户端)。所以我建议也考虑这种可能性。

通过我的电脑(这台电脑)->属性->高级系统设置->高级->环境变量->路径 您可以编辑 Oracle Home 路径。

【讨论】:

    【解决方案5】:

    我有同样的问题并解决了添加到我的 C# 项目引用 Oracle.ManagedDataAccess 而不是 Oracle.DataAccess
    为此,请转到 (Tools / Nugget Package Manager / Nugget Package Manager for Solution) 浏览 Oracle References 并选择 Oracle.ManagedDataAccess
    然后您需要注释或删除您对 ManagedDataAccess 的实际 DataAccess 调用:

    //using Oracle.DataAccess.Client; 
    //using Oracle.DataAccess.Types;
    using Oracle.ManagedDataAccess.Client;
    

    无需更改您的连接代码,只需使用编码。 运行后,您可以从项目参考中删除 Oracle.DataAccess

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 2012-09-19
      • 2023-03-18
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2016-07-12
      • 2021-01-23
      相关资源
      最近更新 更多