如何用DataSet 更新数据库中的数据?
在线等,有客户表:
ID code
1 A1001
2 B1001
现在需要把code取出来在程序代码中处理,比如改成A2007061001,
用DataSet 和DataTable修改这些数据,并更新回数据库中?
谢谢!
------解决方案-------------------- SqlConnection conn=new SqlConnection(GetSqlConnectionString(m_db.Text));
SqlCommand cmd=new SqlCommand( "select userID,isnull(password, ' ') as passwrod from admUser ",conn);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
DataSet ds=new DataSet();
if (conn.State==ConnectionState.Closed) conn.Open();
da.Fill(ds);
DataTable dt=ds.Tables[0];
Ciphertext ct=new Ciphertext();
for(int i=0;i <ds.Tables[0].Rows.Count;i++)
{
ds.Tables[0].Rows[i].BeginEdit();
ds.Tables[0].Rows[i][1] =ct.Encrypto(ds.Tables[0].Rows[i][1].ToString());
ds.Tables[0].Rows[i].EndEdit();
}
cmd=new SqlCommand( "update admUser set password=@password where userID=@userID ",conn);
cmd.Parameters.Add( "@userID ",SqlDbType.Int,4, "id ");
cmd.Parameters.Add( "@password ",SqlDbType.VarChar,30, "password ");
da.UpdateCommand=cmd;
da.Update(ds.Tables[0]);
ds.AcceptChanges();
conn.Close();