[英]Update/Delete/Insert DataGridView
I've created an DbDataAdapter from a DbConnection, filled a DataTable, and put it into a DataGridView. 我已经从DbConnection创建了一个DbDataAdapter,填充了一个DataTable,并将其放入DataGridView中。
When I addapt/insert/delete data in the DataGridView, I want to save it and update the database. 在DataGridView中添加/插入/删除数据时,我想保存它并更新数据库。
What do I exactly have to do? 我到底要做什么?
(I've created the DbCommands myself; an example of the Update-command (is it correct?): (我自己创建了DbCommands; Update-command的示例(是否正确?):
DbCommand updateCmd = connection.CreateCommand();
DbParameter param1 = updateCmd.CreateParameter();
DbParameter param2 = updateCmd.CreateParameter();
DbParameter param3 = updateCmd.CreateParameter();
param1.Direction = ParameterDirection.Input;
param1.ParameterName = "@firstname";
param1.SourceColumn = "firstname";
param1.DbType = (DbType.String);
param2.Direction = ParameterDirection.Input;
param2.ParameterName = "@lastname";
param2.SourceColumn = "lastname";
param2.DbType = (DbType.String);
param3.Direction = ParameterDirection.Input;
param3.ParameterName = "@mail";
param3.SourceColumn = "mail";
param3.DbType = (DbType.String);
updateCmd.CommandText = "UPDATE Personen SET fistname=@firstname,lastname=@lastname,mail=@mail";
dbAdapter.UpdateCommand = updateCmd;
I think, the "save" button has this as code: 我认为,“保存”按钮具有以下代码:
adapt.Update(table);
But I get an error: 但是我得到一个错误:
Must declare the scalar variable "@firstname".
CreateParameter
method only creates a SqlParameter
instance, but does not add the parameter to the SqlCommand
. CreateParameter
方法仅创建SqlParameter
实例,而不会将参数添加到SqlCommand
。 You have to add the parameters to the command using updateCmd.Parameters.Add
method. 您必须使用
updateCmd.Parameters.Add
方法将参数添加到命令中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.