You can add a lot of records to the client, and then update the server with an update
First get the data and put it in the DataGrid.
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection ("Server=localhost;database=nort Hwind;uid=sa;password=110 ");
Conn. Open ();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter ("SELECT * from student", C Onn);
DT = new System.Data.DataSet ();
Da. Fill (DT, "student");
Then bind the dataset and the DataGrid
datagrid.setdatabinding (DT, "student");
If necessary, you can bind a textbox to input and display it in a DataGrid
THIS.TEXTBOX16.DATABINDINGS.ADD ("Text", DT, "Student.stuno");
The data is then manipulated like this:
Increase:
This. BINDINGCONTEXT[DT, "student"]. AddNew ();
Delete:
This. BINDINGCONTEXT[DT, "student"]. RemoveAt (this. BINDINGCONTEXT[DT, "student"]. Position);
Finally, the results are written back to the database:
SqlInsertCommand1
//
This.sqlInsertCommand1.CommandText = "INSERT into student (Stuno, name) VALUES (@stuno, @name)";
This.sqlInsertCommand1.Connection = This.conn;
THIS.SQLINSERTCOMMAND1.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@stuno", System.Data.SqlDbType.VarChar, 4, "Stuno"));
THIS.SQLINSERTCOMMAND1.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@name", System.Data.SqlDbType.VarChar, "name"));
//
SqlUpdateCommand1
//
This.sqlUpdateCommand1.CommandText = "UPDATE student SET Stuno = @stuno, name = @name WHERE (stuno = @Original_stuno) ";
This.sqlUpdateCommand1.Connection = This.conn;
THIS.SQLUPDATECOMMAND1.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@stuno", System.Data.SqlDbType.VarChar, 4, "Stuno"));
THIS.SQLUPDATECOMMAND1.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@name", System.Data.SqlDbType.VarChar, "name"));
THIS.SQLUPDATECOMMAND1.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, False, ((System.Byte) (0)), ((System.Byte ) (0)), "Stuno", System.Data.DataRowVersion.Original, NULL);
SqlDeleteCommand1
//
This.sqlDeleteCommand1.CommandText = "DELETE from student WHERE (Stuno = @Original_stuno)";
This.sqlDeleteCommand1.Connection = This.conn;
THIS.SQLDELETECOMMAND1.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, False, ((System.Byte) (0)), ((System.Byte ) (0)), "Stuno", System.Data.DataRowVersion.Original, NULL);
This.sqlDa.DeleteCommand = This.sqldeletecommand1;
This.sqlDa.InsertCommand = This.sqlinsertcommand1;
This.sqlDa.UpdateCommand = This.sqlupdatecommand1;
Try
{
Sqlda.update (dt. GetChanges, "student");
return true;
}
catch (System.Data.SqlClient.SqlException ex)
{
return false;
}
Finally
{
Conn. Close ();
}
Give you an update example: The database table has only one field ID, the table name is test, and the other analogy:
conn = new System.Data.SqlClient.SqlConnection ("server=localhost;database=hos;uid=sa;pwd=");
Conn. Open ();
da = new System.Data.SqlClient.SqlDataAdapter ("SELECT * FROM Test", Conn);
DT1 = new System.Data.DataSet ();
Da. Fill (DT1, "test");
DataRow Myrow;
for (int i=0;i<10;i++)
{
Myrow = dt1. Tables[0]. NewRow ();
Then Add the new row to the collection.
myrow["ID"] = i.ToString ();
Dt1. Tables[0]. Rows.Add (Myrow);
}
System.Data.SqlClient.SqlDataAdapter DAA = new System.Data.SqlClient.SqlDataAdapter ("Select ID from Test", conn);
System.Data.SqlClient.SqlCommandBuilder MYCB = new System.Data.SqlClient.SqlCommandBuilder (DAA);
Daa. MissingSchemaAction = MissingSchemaAction.AddWithKey;
Daa. Update (DT1, "test");