C# 尚未初始化c connectionstring属性

C# 尚未初始化c connectionstring属性,c#,sql-server,datagridview,C#,Sql Server,Datagridview,我已经构建了一个DataGridView并很好地读取了一个sql表。但SqlDataAdapter无法将数据更新回SQL Server。更新,我得到错误: connectionstring属性尚未初始化 这是我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; usi

我已经构建了一个DataGridView并很好地读取了一个sql表。但SqlDataAdapter无法将数据更新回SQL Server。更新,我得到错误:

connectionstring属性尚未初始化

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace test_2
{
  public partial class Form1 : Form
  {
    SqlDataAdapter sda;
    DataSet ds;
    BindingSource bind1 = new BindingSource();
    SqlCommandBuilder scb;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (SqlConnection cn = new     SqlConnection(Properties.Settings.Default.ConnString))
        {
            ShowData();
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);
            bind1.DataSource = ds;
        }
    }

    private void ShowData()
    {
        using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ConnString))
        {
           sda = new SqlDataAdapter("select key_seq, po_no, ref_no from mpo_master", cn);
            ds = new DataSet();
            sda.Fill(ds, "MPO");
            dataGridView1.DataSource = ds.Tables["MPO"]; 

        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection cn = new SqlConnection(Properties.Settings.Default.ConnString))
            {
        scb = new SqlCommandBuilder(sda);
                sda.Update(ds, "MPO");
                MessageBox.Show("Information Updated", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  }
}
这是配置文件:

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
     <configSections>
         <sectionGroup name="applicationSettings"       type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="VGB_Purchase.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<applicationSettings>
    <VGB_Purchase.Properties.Settings>
        <setting name="connString" serializeAs="String">
            <value>Data Source=vgb-angus;Initial Catalog=VGB_Purchase;Persist Security Info=True;User ID=sa;Password=jessie</value>
        </setting>
    </VGB_Purchase.Properties.Settings>
</applicationSettings>

您的代码有一些问题-未使用的连接实例,但最重要的是,您没有打开连接

e、 g

重新访问所有函数,并在不使用cn初始化的地方删除它,例如

private void Form1_Load(object sender, EventArgs e)
{
    ShowData();
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);
    bind1.DataSource = ds;
}
另外,我假设sda是一个类变量?从您的代码中不容易确定,我上面的示例假设它是

最后,您绝对需要异常处理;我的示例只是插入缺少的打开调用。但是,您需要处理连接无法打开的时间


注意:这只是一个关于如何根据给定的代码结构处理DB调用的示例。相应地调整以适应更新。

上述编码风格看起来没有那么好

在代码中,即使在更新中创建连接对象,也未设置连接属性

如果您试图创建一个类级对象,请不要在以表单\u Load创建连接对象时使用using关键字,这不是一个好方法,但现在可以解决您的问题-您应该选择一些架构


要销毁对象,您可能必须在第页Unload上调用dispose这也不是一个好方法

请共享输入到配置文件中的连接字符串。当然,您可以非常小心地屏蔽敏感信息,但我们希望看到您是如何构造字符串的。谢谢Sfugua,添加了配置文件我怀疑@rachel ambler是对的。不过,这些都不是问题的答案。诚然,编码风格不是很好,但是您没有回答这里关于为什么会发生错误的问题。此外,如果您的回答是100%不正确,则您的回答无法得出答案,但可以解决您的问题。连接属性使用关键字used立即处理表单加载完成情况。因此,在更新部分,它不可用。这就是原因。在更新中,他创建了连接的另一个对象,该对象未分配给对象sda。但他正在使用sda运行更新。这就造成了问题。为什么答案不正确?在我的建议中,我试图告诉他不要处理创建表单的连接对象。当然,表单初始化不能使用该对象,以便您可以在按钮单击中使用相同的连接。如果你这样做。另一种方法是分配他在按钮单击中创建的连接对象。它比以前的连接最差。连接未打开是另一个问题。但它将正确地告诉您没有打开的连接错误。此处的错误语句是connectionstring属性尚未初始化CN=new SqlConnection;cn.ConnectionString=@Data Source=vgb angus;初始目录=VGB\U采购;持久安全信息=True;用户ID=sa;密码=杰西;中国公开;
private void Form1_Load(object sender, EventArgs e)
{
    ShowData();
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);
    bind1.DataSource = ds;
}