App.config中的Winforms连接字符串

3
我正在使用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或通过我创建的静态字符串会抛出异常。


在Mystere Mans的答案基础上,尝试使用类似以下代码: foreach (ConnectionStringSettings connection in ConfigurationManager.ConnectionStrings) { MessageBox.Show(connection.Name); }在调用方法之前检查它实际在配置文件中找到了什么,以确保它是你认为的内容。 - Hans Olsson
1个回答

4

好的,这些都是一些简单的问题。你确定在测试机器的app.config文件中添加了连接字符串吗?而且你确定拼写正确吗?你确定将connectionStrings部分放在了app.config文件的正确位置吗?你确定将app.config文件放在了正确的位置吗?你确定拼写的是"app.config",而不是app.cfg或app.cofig(我之前也打错过)?


不,这不是一个愚蠢的问题。我从未使用过App.config。我一直以为App.config嵌入到可执行文件中,但是你的答案让我意识到我错了。我把配置文件移动到了可执行文件所在的文件夹中,现在一切都很好。感谢你的帮助! - Geo Ego
但是你不需要将 "app.config" 移动到程序目录中 - Visual Studio 应该已经在输出目录中创建了一个 "yourappname.exe.config" - "app.config" 只是一个系统占位符,将被转换为您的应用程序自己的配置。 - marc_s
很遗憾,无法嵌入app.configs。我希望它们可以,这在某些情况下会很有用。虽然如果您想简单地发布单个exe文件,您可以让您的代码在启动时将嵌入式app.config复制到本地目录中的文件(如果不存在)。 - Erik Funkenbusch
1
它确实创建了。在我添加app.config之前,我只是将可执行文件复制并粘贴到另一台机器上进行测试。在我添加配置文件后,我按照相同的步骤进行了操作,因此虽然VS已将其放置在输出目录中,但我没有将其添加到测试机器中。 - Geo Ego
我不介意应用程序有一个单独的配置文件。无论如何,我很快就会创建一个安装程序,而且这个应用程序严格是供大约半打人内部使用的。谢谢你的建议,不过我会研究一下像你建议的将配置复制到本地目录的方法。 - Geo Ego

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接