C
Für alle, die es interessiert:
Update:
SqlDataAdapter adap = new SqlDataAdapter("select customerid, companyname from customers where customerid like 'e%'", conn);
adap.Fill(ds, "customers");
foreach (DataRow r in ds.Tables["customers"].Rows)
{
r["companyname"] = "_" + r["companyname"];
}
//automatically will be delete-, insert-, update-statements according to the
//changes within the dataset created
SqlCommandBuilder cb = new SqlCommandBuilder(adap);
adap.Update(ds, "customers");
MessageBox.Show("All CompynyNames beginning with \"E\" has been changed to \"_E\"!", "Update");
Delete:
SqlDataAdapter adap = new SqlDataAdapter("select orderid, customerid, orderdate from orders where orderid >= 11078", conn);
adap.Fill(ds, "orders");
foreach (DataRow r in ds.Tables["orders"].Rows)
{
r.Delete();
}
//automatically will be delete-, insert-, update-statements according to the
//changes within the dataset created
SqlCommandBuilder cb = new SqlCommandBuilder(adap);
adap.Update(ds, "orders");
MessageBox.Show("All Orders with id >= 11078 has been deleted!", "Delete");
Insert:
//work with transactions (a)
SqlTransaction tx = conn.BeginTransaction();
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter("select customerid, companyname from customers", conn);
//work with transactions (b)
adap.SelectCommand.Transaction = tx;
adap.Fill(ds, "customers");
//just create a new row and fill up with neccessary data
DataRow newrow = ds.Tables["customers"].NewRow();
newrow["customerid"] = Guid.NewGuid().ToString().Substring(0, 4);
newrow["companyname"] = "new row";
ds.Tables["customers"].Rows.Add(newrow);
//automatically will be delete-, insert-, update-statements according to the
//changes within the dataset created
SqlCommandBuilder cb = new SqlCommandBuilder(adap);
adap.Update(ds, "customers");
//work with transactions (c)
tx.Commit();
MessageBox.Show("A new customer has been inserted!", "Insert");