将 .net 项目部署到开发服务器
我在我的机器上本地开发了一个 winforms 应用程序。它从 csv 文件读取数据并从数据库查找相关信息(通过执行存储过程)。
我将其移至开发服务器并尝试运行它,但在从 app.config 文件获取 SQL 连接字符串的行处出现错误。错误是:System.NullReferenceException:未将对象引用设置为对象的实例。。当然,该应用程序在我的机器上运行良好。
我注释掉了该代码后面的所有行,并显示了错误消息。我猜这与 conn.ConnectionString 行有关。
SQL 数据库指向正确的数据库服务器& UID& pwd 是正确的。什么会导致错误?
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SilverTicker"].ConnectionString;
I developed a winforms app locally on my machine. It reads data from a csv file and looks up related information from a database (by executing a stored proc).
I moved that to the development server and tried running it, but I get an error when at the line that gets SQL connection string from app.config file. The error is: System.NullReferenceException: Object reference not set to an instance of an object.. Of course the app runs fine on my machine.
I commented out all lines following this code, and the error message is displayed. I am guessing it has to do with the conn.ConnectionString line.
The SQL database is pointing to the correct db server & UID & pwd are correct. What would be causing the error?
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["SilverTicker"].ConnectionString;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码示例中的对象之一返回“null”。我猜测它是
在该行上放置一个断点并检查
.ConnectionStrings
集合的内容以确保它包含“SilverTicker”。我猜测
ConfigurationManager.ConnectionStrings["SilverTicket"]
未定义(可能是因为两个环境中app.config
之间不一致),因此访问它是.ConnectionString
属性抛出异常。One of the objects in your code sample is returning "null". I'm guessing that it's
Put a breakpoint on that line and check the contents of the
.ConnectionStrings
collection to be sure that it contains "SilverTicker".I'm guessing that
ConfigurationManager.ConnectionStrings["SilverTicket"]
is undefined (probably because of an inconsistency betweenapp.config
in the two environments), so accessing it's.ConnectionString
property is throwing an exception.检查它是否检索数据,
如果您已经编写了类似
检查 tis 返回数据的内容,并且 DataSet 中必须包含表。
Check its retriving data or not
if you have written like
check tis returning data and DataSet must contain table in it .
看起来您不需要第二个 .connectionstring。您必须将连接字符串放在 app.config 中的 connectionstrings 标记中的正确位置。该位置需要在 app.config 中指定,否则您的代码将找不到连接字符串。
Looks like you don't need the second .connectionstring. You have to put the connection string in the right place in the connectionstrings tag in the app.config. The place needs to be specific in the app.config otherwise your code won't find the connection string.