App.config 中的 Winforms 连接字符串

发布于 2024-09-01 08:35:03 字数 2780 浏览 16 评论 0原文

我有一个用 C# 开发的 Winforms 应用程序,它将用作 SQL Server 2005 数据库的前端。我将可执行文件发布到测试机器并运行它。直到我进行最后一轮更改之前,它在测试机器上运行得非常好。然而,现在在测试机器上,它在打开时立即抛出以下异常:

System.NullReferenceException: Object reference not set to an instance of an object.
       at PSRD_Specs_Database_Administrat.mainMenu.mainMenu_Load(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

在此版本中,我唯一更改的与 mainMenu_Load 相关的内容是调用数据库连接字符串的方式。以前,我在需要调用它的每个表单上设置了一个带有连接字符串的字符串,例如:

string conString = "Data Source = SHAREPOINT;Trusted_Connection = yes;" +
                   "database = CustomerDatabase;connection timeout = 15";

随着我的应用程序的增长并向其中添加表单,我决定将 App.config 添加到项目中。我在其中定义了连接字符串:

<connectionStrings>
  <add name="conString"
   providerName="System.Data.SqlClient"
   connectionString="Data Source = SHAREPOINT;Trusted_Connection = yes;database = CustomerDatabase;connection timeout = 15" />
</connectionStrings>

然后创建了一个将返回 conString 的静态字符串:

public static string GetConnectionString(string conName)
{
    string strReturn = string.Empty;
    if (!(string.IsNullOrEmpty(conName)))
    {
        strReturn = ConfigurationManager.ConnectionStrings[conName].ConnectionString;
    }
    else
    {
        strReturn = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    }
    return strReturn;
}

我删除了 conString 变量,现在像这样调用连接字符串:

PublicMethods.GetConnectionString("conString").ToString()

看来这给了我错误。我将这些实例更改为直接从 App.config 调用连接字符串,而不使用 GetConnectionString。例如,在 SQLConnection 中:

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))

这也引发了异常。然而,当我重新在每个表单上使用 conString 变量时,我没有遇到任何问题。我不明白的是为什么这三种方法在我的开发机器上都工作正常,而直接使用 App.config 或通过我创建的静态字符串抛出异常。

I have a Winforms app that I am developing in C# that will serve as a frontend for a SQL Server 2005 database. I rolled the executable out to a test machine and ran it. It worked perfectly fine on the test machine up until the last round of changes that I made. However, now on the test machine, it throws the following exception immediately upon opening:

System.NullReferenceException: Object reference not set to an instance of an object.
       at PSRD_Specs_Database_Administrat.mainMenu.mainMenu_Load(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The only thing that I changed in this version that pertains to mainMenu_Load is the way that the connection string to the database is called. Previously, I had set a string with the connection string on every form that I needed to call it from, like:

string conString = "Data Source = SHAREPOINT;Trusted_Connection = yes;" +
                   "database = CustomerDatabase;connection timeout = 15";

As my app grew and I added forms to it, I decided to add an App.config to the project. I defined the connection string in it:

<connectionStrings>
  <add name="conString"
   providerName="System.Data.SqlClient"
   connectionString="Data Source = SHAREPOINT;Trusted_Connection = yes;database = CustomerDatabase;connection timeout = 15" />
</connectionStrings>

I then created a static string that would return the conString:

public static string GetConnectionString(string conName)
{
    string strReturn = string.Empty;
    if (!(string.IsNullOrEmpty(conName)))
    {
        strReturn = ConfigurationManager.ConnectionStrings[conName].ConnectionString;
    }
    else
    {
        strReturn = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    }
    return strReturn;
}

I removed the conString variable and now call the connection string like so:

PublicMethods.GetConnectionString("conString").ToString()

It appears that this is giving me the error. I changed these instances to directly call the connection string from App.config without using GetConnectionString. For instance, in a SQLConnection:

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))

This also threw the exception. However, when I went back to using the conString variable on each form, I had no issues. What I don't understand is why all three methods work fine on my development machine, while using the App.config directly or via the static string I created throw exceptions.

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

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

发布评论

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

评论(1

眼泪淡了忧伤 2024-09-08 08:35:03

好吧,愚蠢的问题。您确定已将连接字符串添加到测试机器上的 app.config 中吗?你确定你拼写正确吗?您确定将 connectionStrings 部分放在 app.config 中的正确位置吗?您确定将 app.config 放在正确的位置吗?你确定你拼写的“app.config”正确吗,而不是 app.cfg 或 app.cofig (我之前就搞错了)

Ok, stupid questions. Are you sure you added the connection string to the app.config on the test machine? And are you sure you spelled it correctly? Are you sure you put the connectionStrings section in the correct place in the app.config? And are you sure you put the app.config in the correct location? Are you sure you spelled "app.config" correctly, and not app.cfg or app.cofig (i've fat fingered that one before)

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