我从 DbConnection 创建了一个 DbDataAdapter,填充了一个 DataTable,并将其放入 DataGridView。
当我在 DataGridView 中添加/插入/删除数据时,我想保存它并更新数据库。
我到底要做什么?
(我自己创建了 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;
我认为,“保存”按钮的代码如下:
adapt.Update(table);
但我收到一个错误:
Must declare the scalar variable "@firstname".