“从Datagrid(C#WPF)更新SQL表时,尚未初始化ConnectionString属性”

问题描述 投票:0回答:2

类型为'System.InvalidOperationException'的未处理异常在System.Data.dll中发生]

其他信息:尚未提供ConnectionString属性初始化。

这是尝试将Sava DataGrid更新回SQL数据库时遇到的错误。我可以毫无问题地填充DataGrid,但不能更新任何内容。这是我的代码:

App.config:

<connectionStrings>
    <add connectionString="Data Source=**.**.**.**; User Id=**;Password=***; Initial Catalog=****;" name="ConString"/>
</connectionStrings>

MainWindow:

public MainWindow()

    {
        InitializeComponent(); 
    }

    string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    string CmdString = string.Empty;
    SqlCommand cmdE= null;
    SqlDataAdapter sdaE= null;
    DataTable dtE= null;

    public void Fill()
    {
        using (SqlConnection conE = new SqlConnection(ConString))
        {
            CmdString = "SELECT * FROM COMPAINES_EXT";
            cmdE = new SqlCommand(CmdString, conE);
            sdaE = new SqlDataAdapter(cmdE);
            dtE = new DataTable("COMPAINES_EXT");
            sdaE.Fill(dtE);
            dataGridE.ItemsSource = dtE.DefaultView;
        }
        dataGridE.Columns[0].Visibility = Visibility.Hidden;         
    }


    private void btnLoadE_Click(object sender, RoutedEventArgs e)
    {
        Fill();
    }

    private void btnSaveE_Click(object sender, RoutedEventArgs e)
    {
            SqlCommandBuilder builder = new SqlCommandBuilder(sdaE);
            sdaE.UpdateCommand = builder.GetUpdateCommand();
            sdaE.Update(dtE);
    }

Fill()方法运行完美,因此我认为与数据库的连接很好...但是当我单击btnSaveE按钮时,出现错误。我想念什么?

更新的btnSave代码:

private void btnSaveE_Click(object sender, RoutedEventArgs e)
{
    using (SqlConnection conE = new SqlConnection(ConString))
    {
            CmdString = "SELECT * FROM COMPANIES_EXT";
            cmdE = new SqlCommand(CmdString, conE);
            sdaE = new SqlDataAdapter(cmdE);
            dtE = new DataTable("COMPANIES_EXT");

        SqlCommandBuilder builder = new SqlCommandBuilder(sdaE);
        sdaE.UpdateCommand = builder.GetUpdateCommand();
        sdaE.Update(dtE);
    }
}
c# wpf datagrid wpfdatagrid
2个回答
0
投票
using (SqlConnection conE = new SqlConnection(ConString))
{

}

上面的using命令仅在括号内处理完对象后,才在括号内创建SqlConnection对象。因此,基本上就像大多数人建议的那样,在保存方法中创建另一个连接。


0
投票

取决于在DataTable中对数据进行了哪些更改,必须在调用DataAdapter的Update方法之前设置InsertCommand,UpdateCommand或DeleteCommand属性。

下面的示例演示如何创建SqlDataAdapter并将MissingSchemaAction设置为AddWithKey以便从数据库中检索其他架构信息。 SelectCommand,InsertCommand,UpdateCommand和DeleteCommand属性集以及它们对应的SqlParameter对象添加到Parameters集合中。该方法返回一个SqlDataAdapter对象。

public static SqlDataAdapter CreateSqlDataAdapter(SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

// Create the commands.
adapter.SelectCommand = new SqlCommand(
    "SELECT CustomerID, CompanyName FROM CUSTOMERS", connection);
adapter.InsertCommand = new SqlCommand(
    "INSERT INTO Customers (CustomerID, CompanyName) " +
    "VALUES (@CustomerID, @CompanyName)", connection);
adapter.UpdateCommand = new SqlCommand(
    "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
    "WHERE CustomerID = @oldCustomerID", connection);
adapter.DeleteCommand = new SqlCommand(
    "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

// Create the parameters.
adapter.InsertCommand.Parameters.Add("@CustomerID", 
    SqlDbType.Char, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add("@CompanyName", 
    SqlDbType.VarChar, 40, "CompanyName");

adapter.UpdateCommand.Parameters.Add("@CustomerID", 
    SqlDbType.Char, 5, "CustomerID");
adapter.UpdateCommand.Parameters.Add("@CompanyName", 
    SqlDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@oldCustomerID", 
    SqlDbType.Char, 5, "CustomerID").SourceVersion = 
    DataRowVersion.Original;

adapter.DeleteCommand.Parameters.Add("@CustomerID", 
    SqlDbType.Char, 5, "CustomerID").SourceVersion = 
    DataRowVersion.Original;

return adapter;
}
© www.soinside.com 2019 - 2024. All rights reserved.