Here is a basic sample:
SqlConnection conn = new SqlConnection(CONNECT); conn.Open();
// Create simple admin table with a few users. I commented this out but this demonstrates my table structure //xsql_noerror(conn, "drop table admin"); //xsql(conn, "create table admin (username varchar(256) primary key, password varchar(256))"); //xsql(conn, "insert into admin (username,password) values ('matt', 'matt')"); //xsql(conn, "insert into admin (username,password) values ('shu', 'shu')");
// Create data adapter to fill our dataset. SqlDataAdapter da = new SqlDataAdapter("select * from admin", conn); DataSet ds = new DataSet("MyAdminSet"); da.Fill(ds, "admin");
// Ok, now I have my records, say I want to change password for matt. DataTable t = ds.Tables["admin"]; t.PrimaryKey = new DataColumn [] { t.Columns["username"] }; DataRow r = t.Rows.Find(new object [] {"shu"});
// Change password. r["password"] = "SomeNewPassword"; // Now lets update our table. SqlCommand cmdUpdate = conn.CreateCommand(); cmdUpdate.CommandType = CommandType.Text;
";
da.UpdateCommand = cmdUpdate; da.Update(ds, "admin");
// Now read back the rows to verify we updated it. SqlCommand cmdTest = conn.CreateCommand(); cmdTest.CommandType = CommandType.Text; cmdTest.CommandText = "select * from admin where username='shu'"; SqlDataReader dr = cmdTest.ExecuteReader(); dr.Read(); System.Diagnostics.Debug.WriteLine("username=" + dr[0].ToString() + " password=" + dr["password"].ToString()); dr.Dispose(); conn.Close();
|