尝试通过测试单元引用数据库连接时出现 C#.net 异常
我的数据库连接不起作用,有人可以告诉我为什么吗?
我在 VS 2010 中的单元测试中运行此代码。简单的 Assert.IsTrue(testDbConnection) 但它失败了。 我还尝试将其作为网络应用程序在正常模式下运行,但我得到了相同的异常。
请注意,XXXXX 当然是有目的的。
web.config
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="DomainDatabase" connectionString="Data Source=XXXXXXXXXX;Initial Catalog=domain;Persist Security Info=True;User ID=campain_xxx;Password=XXXXXXXXX" providerName="System.Data.SqlClient" />
</connectionStrings>
我的测试方法
public bool testDBConnection()
{
try
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["DomainDatabase"].ConnectionString);
sqlCon.Open();
sqlCon.Close();
return true;
}
catch(Exception e)
{
Console.WriteLine(e);
return false;
}
}
这给了我例外
System.NullReferenceException: Object reference not set to an instance of an object.
My DB Connection dosnt work, can someone please tell me why?
I run this from a unit test in VS 2010. Simple Assert.IsTrue(testDbConnection) and it fails.
i'v also tried running it in normal mode as a web application and I get the same exception.
please note the XXXXX are ofcourse done with purpose.
web.config
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="DomainDatabase" connectionString="Data Source=XXXXXXXXXX;Initial Catalog=domain;Persist Security Info=True;User ID=campain_xxx;Password=XXXXXXXXX" providerName="System.Data.SqlClient" />
</connectionStrings>
My test method
public bool testDBConnection()
{
try
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["DomainDatabase"].ConnectionString);
sqlCon.Open();
sqlCon.Close();
return true;
}
catch(Exception e)
{
Console.WriteLine(e);
return false;
}
}
This gives me the exception
System.NullReferenceException: Object reference not set to an instance of an object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于测试或 Web 应用程序都无法正常工作,因此您看到的问题很可能是因为未加载配置。我怀疑以下...
...返回 null。当您尝试访问该 null 对象(引用)上的 ConnectionString 属性时,会引发 NullReferenceException。我将验证您的 connectionStrings 配置是否与定义的定义相匹配 此处:
您可以通过以下测试验证您的设置是否未加载:
如果此测试失败,请使用正确的配置将 app.config 添加到您的测试项目。当...你可以从我手里拿走这颗鹅卵石——不等,错误的参考——这个测试通过了。
提示:这个答案 建议您应该编写测试来验证您的设置是否存在,以防止疯狂。
As neither the tests or the web application is working, it's likely that the problem you are seeing is because the configuration is not being loaded. I suspect that the following...
...returns null. The NullReferenceException is thrown when you attempt to access the ConnectionString property on that null object (reference). I would verify that your configuration for connectionStrings matches the definition defined here:
You can verify that your settings aren't being loaded with the following test:
If this test fails, add an app.config to your test project with the proper configuration. You will know that you have solved this problem when ...you can take this pebble from my hand -- no wait, wrong reference -- this test passes.
Tip: This answer suggests you should write tests to verify that your settings are present to prevent madness.
尝试将包含connectionString的app.config文件复制到测试项目中,它可以工作!
Try to copy app.config file wich has connectionString into Test-project and it works!