using System.Data.SqlClient; // create new SqlCommand object SqlCommand command = new SqlCommand("SELECT * FROM products WHERE id = 1", connection); // create new SqlDataAdapter object SqlDataAdapter adapter = new SqlDataAdapter(command); // create new SqlCommandBuilder object SqlCommandBuilder builder = new SqlCommandBuilder(adapter); // create new DataTable object DataTable dataTable = new DataTable(); // fill DataTable with data from database adapter.Fill(dataTable); // modify rows in DataTable foreach (DataRow row in dataTable.Rows) { row["name"] = "New Product Name"; } // update database with changes made to DataTable adapter.Update(dataTable);
using System.Data.SqlClient; // create new SqlCommandBuilder object SqlCommandBuilder builder = new SqlCommandBuilder(); // set the SqlCommandBuilder's DataAdapter property builder.DataAdapter = new SqlDataAdapter("SELECT * FROM products", connection); // use the SqlCommandBuilder to automatically generate and execute SQL insert command builder.DataAdapter.InsertCommand = builder.GetInsertCommand(); // create new DataRow object DataRow row = builder.DataAdapter.FillSchema(new DataTable(), SchemaType.Source).NewRow(); // modify DataRow with new data row["name"] = "New Product Name"; row["price"] = 10.99; // add new DataRow to database builder.DataAdapter.InsertCommand.ExecuteNonQuery();This example demonstrates how to use the SqlCommandBuilder object to automatically generate and execute the SQL insert command based on a new DataRow object. It also showcases the use of the FillSchema method to create a new DataTable object with the appropriate schema for the table in the database. The package library for the SqlCommandBuilder class is the System.Data.SqlClient namespace, which is part of the .NET Framework Class Library.