C#在连接字符串中抛出异常 [英] C# throwing exception in Connection String

查看:92
本文介绍了C#在连接字符串中抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有连接字符串的问题,错误(对象引用没有设置对象的实例)如何调试这个请



我的app.config

I have problem with connection String, Error (Object reference not set an instance of an object) how to debug this please

My app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      <connectionStrings>
        <add name = "databasecon" connectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\PCIT2\Desktop\Database.accdb"></add>
      </connectionStrings>
    </startup>
</configuration>





此连接字符串





This the connection String

string connStr = ConfigurationManager.ConnectionStrings["databasecon"].ConnectionString;

推荐答案

问题不在于连接字符串。



A 当您尝试访问给定变量的实例成员而此变量尚未初始化或已被处置时,将抛出NullReferenceException



在代码文件的某处,有一行您使用的变量,您认为它包含一个值,但实际上它不包含任何值。



行的编号显示在异常的堆栈跟踪的顶部。找到这一行,在其上放置一个断点,或在上面几行,并以调试模式启动项目。执行将在断点处暂停,您将能够逐行执行代码,在每个步骤检查变量实际上是否包含您希望它们保持的值。找到null的变量,它会给你(或我们)一些关于这个问题的线索。
The problem is not the connection string.

A NullReferenceException is throw when you try to access an instance member of a given variable while this variable has not been initialized, or been disposed.

Somewhere in your code file, there is a line where you are using a variable of which you think it contains a value, but which in fact does not contain any value.

The number of the line is indicated at the top of the stack trace of the exception. Find this line, put a breakpoint on it, or on a few lines above, and launch your project in debug mode. Execution will pause on the breakpoint, and you will be able to execute the code line by line, checking at each step that the variables actually hold the values you expect them to hold. Find the variable that is null, it will give you (or us) some clues about the problem.


我不知道它是否只是你在问题中发布该信息的方式,但是,看起来好像你的connectionStrings是''在'启动时的一部分 - 我不认为它应该是 - 尝试将它'向上'移动'或至少改变这个



I dont know if its just the way you've posted that info in your question, but, it appears as though your connectionStrings is part of/'under' startup - I dont think it should be - try moving it 'up' a level' or at least change this

<startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />





to





to

<startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>









Als o确保添加了对Configuration Dll的引用,即System.Configuration





Also make sure you have added a reference to the Configuration Dll ie System.Configuration


这篇关于C#在连接字符串中抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆