使用数据集更新 TableAdapter - 更新需要有效的 DeleteCommand 错误 [英] Update TableAdapter with DataSet - Update requires a valid DeleteCommand error

查看:15
本文介绍了使用数据集更新 TableAdapter - 更新需要有效的 DeleteCommand 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,当传递带有已删除行的 DataRow 集合时,更新需要有效的 DeleteCommand"是什么意思.是什么意思?

In the code below what does "Update requires a valid DeleteCommand when passed DataRow collection with deleted rows." mean?

foreach (DataGridViewRow item in this.dataGridView2.SelectedRows)
{
    fuelStopsDataSet1.Tables[0].Rows[item.Index].Delete();
}

this.fuelStopsTableAdapter.Update(this.fuelStopsDataSet1.FuelStops);

推荐答案

这意味着您正在使用 DataAdapter 来更新包含已删除 DataRows(它们的 DataRowsa href="http://msdn.microsoft.com/en-us/library/system.data.datarowstate.aspx" rel="nofollow">RowState已删除).然后 DataAdapter 使用指定的 DeleteCommand 删除数据库中的这一行.但你没有提供.

It means that you are using a DataAdapter to update a table which contains deleted DataRows (their RowState is Deleted). Then the DataAdapter uses the specified DeleteCommand to delete this row in the database. But you haven't provided it.

所以你需要提供它.

MSDN 示例:

public static SqlDataAdapter CreateCustomerAdapter(
    SqlConnection connection)
{
    SqlDataAdapter adapter = new SqlDataAdapter();

    // Create the SelectCommand.
    SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
        "WHERE Country = @Country AND City = @City", connection);

    // Add the parameters for the SelectCommand.
    command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
    command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

    adapter.SelectCommand = command;

    // Create the InsertCommand.
    command = new SqlCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (@CustomerID, @CompanyName)", connection);

    // Add the parameters for the InsertCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

    adapter.InsertCommand = command;

    // Create the UpdateCommand.
    command = new SqlCommand(
        "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
        "WHERE CustomerID = @oldCustomerID", connection);

    // Add the parameters for the UpdateCommand.
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
    SqlParameter parameter = command.Parameters.Add(
        "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.UpdateCommand = command;

    // Create the DeleteCommand.
    command = new SqlCommand(
        "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

    // Add the parameters for the DeleteCommand.
    parameter = command.Parameters.Add(
        "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
    parameter.SourceVersion = DataRowVersion.Original;

    adapter.DeleteCommand = command;

    return adapter;
}

最后一个命令是DeleteCommand.

这篇关于使用数据集更新 TableAdapter - 更新需要有效的 DeleteCommand 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆