用userAdapter.Fill(userDataSet)把数据弄到DataSet里了,我现在想把数据改动然后再存回数据库里,怎么做?
------解决方案--------------------------------------------------------
//利用CommandBuilder更新数据库
//添加记录
public void Insert()
{
SqlConnection oConn = DBClass.Conn;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand( "select * from [Student] ",oConn );
SqlCommandBuilder cmdBld = new SqlCommandBuilder( da );
oConn.Open();
da.Fill(dt);
DataRow dRow = dt.NewRow();
dRow[ "Name "] = Name.Text;
dRow[ "Age "] = Age.Text;
dRow[ "Address "] = Address.Text;
dRow[ "CreatedTime "] = Time.Text;
dt.Rows.Add(dRow);
da.Update(dt.Select( null,null,DataViewRowState.Added ) );
oConn.Close();
}
//删除记录
public void Delete()
{
SqlConneciton oConn = DBClass.Conn;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand( "select * from [Student] where Id= "+Id,oConn );
SqlCommandBuilder cmdBld = new SqlCommandBuilder( da );
oConn.Open();
da.Fill(dt);
dt.Rows[0].Delete();
da.Update(dt.Select( null,null,DataViewRowState.Deleted ) );
oConn.Close();
}
//修改记录
public void Update()
{
SqlConnection oConn = DBClass.Conn;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand( "select * from [Student] where Id= "+Id,oConn );
SqlCommandBuilder cmdBld = new SqlCommandBuilder( da );
oConn.Open();
da.Fill(dt);
dt.Rows[0][ "Name "] = Name.Text;
dt.Rows[0][ "Age "] = Age.Text;
dt.Rows[0][ "Address "] = Address.Text;
dt.Rows[0][ "CreatedTime "] = Time.Text;
da.Update(dt.Select( null,null,DataViewRowState.ModifiedCurrent ) );
oConn.Close();
}