【问题标题】:Object reference not set to an instance of an object. - App.config你调用的对象是空的。 - 应用程序配置
【发布时间】:2013-04-14 10:14:16
【问题描述】:

我收到错误,并且在本地窗口中我看到 conSettings 和 connectionString 值为 null。我可以说 ConfigurationManager 为空,我需要创建一个新对象。也许我正在使用 Access,也许我错过了 App.config 文件中的某些内容。有人可以帮我解决这个问题吗?提前致谢。

App.config 文件...

   <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <appSettings>
       <add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data 
                   Source=E:\...\Database1.mdb"/>
    </appSettings>
    </configuration>

Form.cs 文件...

 private void btnShow_Click(object sender, EventArgs e)
    {
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];

        string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here

        try
        {
            con = new OleDbConnection(connectionString);
            con.Open();
            cmd = new OleDbCommand("SELECT * FROM Table1", con);
            objReader = cmd.ExecuteReader();
            while (objReader.Read())
            {
                txtID.Text = ds.Tables[0].Rows[rno][0].ToString();
                CBAgeGroup.Text = ds.Tables[0].Rows[rno][1].ToString();
                CBGender.Text = ds.Tables[0].Rows[rno][2].ToString();
                CBCrimOffen.Text = ds.Tables[0].Rows[rno][3].ToString();
                if (ds.Tables[0].Rows[rno][4] != System.DBNull.Value)
                {
                    photo_aray = (byte[])ds.Tables[0].Rows[rno][4];
                    MemoryStream ms = new MemoryStream(photo_aray);
                   pictureBox1.Image = Image.FromStream(ms);
                }
                txtCV.Text = ds.Tables[0].Rows[rno][5].ToString();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

有人建议我使用 App.config。

VS 2010 C# 微软访问 2003

更新 1 我的 App.config 现在看起来像这样...

<configuration>
    <ConnectionString>
        <add key="MyDBConnectionString"   value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Raj\Education\C_Sharp\Test1\Database1.mdb"/>
    </ConnectionString>

我现在收到错误消息...“配置系统初始化失败”。我现在正在 Google 上查看它。

更新 2 试过了...

<configuration>
 <connectionStrings>
<add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data   
        Source=E:\...\Database1.mdb"/>
  </connectionStrings>
 </configuration>

再次谷歌搜索时收到“对象引用未设置为对象实例”的错误

更新 3

<configuration>
<connectionStrings>
    <clear />
    <add name="MyDBConnectionString"
     providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Source=\Database1.mdb" />
</connectionStrings>

在更新 3 中,我收到了同样的错误。我已经包括了添加参考系统。配置,我已经使用 System.Configuration 进行了引用;

结论

也许 VS 2010 和 Access 2003 之间存在技术故障。这次我不会使用 App.config。我知道 SQL Server 不会有问题。所以我会留下它。感谢 Damith 和 Clint 抽出宝贵时间。

【问题讨论】:

    标签: c# winforms ms-access


    【解决方案1】:

    您需要阅读 AppSettings 键如下,

    string connectionString = 
          ConfigurationSettings.AppSettings["MyDBConnectionString"];
    

    仍然收到空值,请尝试以下步骤

    1. 在解决方案资源管理器中选择 App.Config 文件
    2. 在属性窗口中选择复制到输出目录进行复制 总是。
    3. 现在构建应用程序并重试。

    要像下面这样访问,您需要在应用配置中添加 connectionStrings 部分

      string connectionString = 
          ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
    

    示例应用配置

    <?xml version="1.0"?>
    <configuration>
      <connectionStrings>
        <add name="MyDBConnectionString" 
        connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data 
                   Source=E:\...\Database1.mdb"/>
      </connectionStrings>
    </configuration>
    

    【讨论】:

    • 这可行,但微软有 app.config 的连接字符串部分是有原因的,他们的官方指南说该部分应该用于连接字符串。
    • @Damith - 我已经尝试过 conntectionString 并且收到“对象引用未设置为对象实例”的错误消息。请你给我建议?
    • @bucketblast 你确定,你总是将应用配置副本设置为输出目录吗?
    • @Damith - 是的,我确定了这一点。鉴于我的 Clint,我正在使用 MSDN 指南
    【解决方案2】:

    检查是否已将 app.config 文件放在启动项目下。就我而言,我只需要将 app.config 文件放在我的启动项目下,问题就解决了。

    【讨论】:

      【解决方案3】:

      这发生在我的类库中,其中一个控制台应用程序设置为运行以进行调试。

      有必要在控制台应用程序中添加到 app.config 的连接,因为如果您从外部进程启动它,它将不会在您的库中找到它。

      【讨论】:

      • 哎呀!就我而言,我试图在我的网络表单存根中使用 web.config 而不是 app.config。
      【解决方案4】:

      这个:

      string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
      

      是你的问题。

      您正在访问ConfigurationManager.ConnectionStrings 以访问您的配置项,但在App.Config 文件中您将其放在appSettings 下,这是与ConnectionStrings 不同的配置文件部分

      您可以将连接字符串放在 app.config 的相关 ConnectionStrings 部分(可通过 ConfigurationManager.ConnectionStrings 访问,或通过 appSettings 部分访问它。

      见:

      http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx

      有关存储连接字符串的 MSDN 指南。

      【讨论】:

      • 我已按照您的建议进行操作,但出现配置系统无法初始化的错误。我正在寻找在 Goggle 上搜索以了解它是什么
      • @bucketblast 完全遵循 MSDN 指南中的示例,可能是您没有完全正确地了解文件连接字符串部分中的详细信息。
      【解决方案5】:

      即使在多次检查 app.config 文件和我的代码是否有错误之后,我也遇到了同样的问题,但我没有在我的代码中发现任何错误。最终我发现了一个愚蠢的错误,我的编译器默认将应用程序配置文件名设为“app1.config”,当我将其更改为“app.config”时,一切对我来说都很好。

      希望它会有所帮助。

      【讨论】:

        猜你喜欢
        • 2013-05-27
        • 2013-07-22
        相关资源
        最近更新 更多