Friday, August 10, 2007

Using the DataAdapter

string connectionString = "Data Source=(local);Initial Catalog=test;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

DataSet ds = new DataSet();

SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = new SqlCommand();
dataAdapter.SelectCommand.Connection = conn;
dataAdapter.SelectCommand.CommandText = "SELECT * FROM t1";


dataAdapter.UpdateCommand = new SqlCommand();
dataAdapter.UpdateCommand.Connection = conn;
dataAdapter.UpdateCommand.CommandText = "UPDATE t1 set name = 'changed5!!!' where 2=1";



dataAdapter.Fill(ds);

ds.Tables[0].Rows[0]["name"] = "ovidiu"; // Rowsate changes from Unchanged to Modified

dataAdapter.Update(ds); // update calls the update command
// in our case, the command does not have parameters, though it typically should

No comments: