简体   繁体   中英

Update/Delete/Insert DataGridView

I've created an DbDataAdapter from a DbConnection, filled a DataTable, and put it into a DataGridView.

When I addapt/insert/delete data in the DataGridView, I want to save it and update the database.

What do I exactly have to do?

(I've created the DbCommands myself; an example of the Update-command (is it correct?):

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 . You have to add the parameters to the command using updateCmd.Parameters.Add method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM