0

我像这样从数据库表中加载数据...

using (view_adapter = new SqlDataAdapter("select * from TVServiceProvider", connection_string))
        {
            using (dt = new DataTable())
            {
                view_adapter.Fill(dt);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (dt.Columns[i].ColumnName.Substring(0, 2).Equals("id"))
                        dt.Columns[i].ReadOnly = false;
                }
                bs.DataSource = dt;
            }
        }

哪里SqlDataAdapter view_adapterDataTable dt。要将更改应用于我创建的方法的数据库

 void View_Adapter_Click(object sender, EventArgs e)
    {
        try
        {
            view_adapter.Update(dt);
            dt.AcceptChanges();
        }
        catch (Exception exc)
        {
            this.radLabelElement1.Text = exc.Message;
        }
    }

但是当我单击按钮时,我遇到了异常。它需要更新命令。我应该在哪里使用什么命令?

4

4 回答 4

2

你必须为你创造UpdateCommand和。DeleteCommandview_adapter

编辑:

代码必须如下所示:

SqlDataAdapter view_adapter = new SqlDataAdapter();
view_adapter .SelectCommand = new SqlCommand(queryString, connection);
view_adapter .UpdateCommand = new SqlCommand(updateCommadString, connection);
view_adapter .DeleteCommand = new SqlCommand(deleteCommadString, connection);

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        view_adapter.Fill(dt);
        return dt;
    }
于 2012-11-22T18:47:18.427 回答
1

好吧,您的代码中有问题或不清楚。
view_adapter变量在using块语句中初始化。
因此,当从 using 块中退出时,view_adatpter 将被框架释放,并且在 click 事件中不可用。(就像您从未调用过 new 来初始化它一样)。
我怀疑你这里还有另一个问题。使用语句

其中的一部分,要自动创建使用 DataAdapter 执行 CRUD 操作所需的 UpdateCommand、InsertCommand 和 DeleteCommand,您可以使用SqlCommandBuilder
(仅当您在 select 语句中使用一个表并且该表定义了主键时才有可能)

所以总结一下:

string queryString = "select * from TVServiceProvider";
view_adapter = new SqlDataAdapter(queryString, connection_string);
SqlCommandBuilder builder = new SqlCommandBuilder(view_adapter)
builder.GetUpdateCommand(); // Force the building of commands
view_adapter.Fill(dt);

那么你的点击事件应该像现在一样工作。

于 2012-11-22T19:10:22.787 回答
0

此代码与您的代码无关,但如果您看一下,可能会对您有所帮助。我从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;
}
于 2012-11-22T18:54:22.713 回答
0

史蒂夫和哈姆雷特的建议对我真正有用的是以下内容。我遇到了一个小问题,因为我在更新视图适配器之前尝试对我的行和表进行接受更改。接受更改只需要在重新使用数据表以在网格视图中显示或其他操作之前保存对数据表的更改。

SqlDataAdapter viewAdapter = new SqlDataAdapter("Select * From Users", DBConn);
SqlCommandBuilder builder = new SqlCommandBuilder(viewAdapter);
viewAdapter.UpdateCommand = builder.GetUpdateCommand();


DataTable Users = new DataTable();
viewAdapter.Fill(Users);
foreach (DataRow user in Users.Rows)
{
    foreach (DataColumn c in Users.Columns)
    {
        Console.WriteLine(c.ColumnName);
        if (c.DataType != typeof(DateTime))
        {
            // Clean up empty space around field entries
            user[c.ColumnName] = user[c.ColumnName].ToString().Trim();
        }

    }
   // user.AcceptChanges(); 
   // Do not do an accept changes for either the table or the row before your ViewAdapter Update. 
   // It will appear as though you do not have changes to push.
}

// Users.AcceptChanges();
viewAdapter.Update(Users);
于 2015-01-29T20:15:18.233 回答