通过DataSet更新数据库

来源:互联网 发布:股票收益数据挖掘 编辑:程序博客网 时间:2024/05/22 14:15
  SqlConnection con = new SqlConnection("server=(local); integrated security=SSPI;database=Mange");
SqlDataAdapter da = new SqlDataAdapter("select * from UserTable", con);
DataSet dsPubs = new DataSet("Pubs");
 da.FillSchema(dsPubs, SchemaType.Source, "UserTable");//就把表结构也读取进来       
 da.Fill(dsPubs, "UserTable");

 dsPubs.Tables[0].Rows[0].Delete(); //删除一行数据

DataRow row = null;
row = dsPubs.Tables[0].NewRow();
 row["LoginName"] = "zhonggong";

dsPubs.Tables[0].Rows.Add(row);//添加一行

object dd = "999 ";
row=dsPubs.Tables[0].Rows.Find(dd);//查找数据
如果没有把表结构读进来,则要设置表中的主键 如:
DataColumn[] colKey= new DataColumn[1];
            colKey[0] = dsPubs.Tables[0].Columns["LoginName"];

dsPubs.Tables[0].PrimaryKey = colKey;
row=dsPubs.Tables[0].Rows.Find(dd);//查找数据
 
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(da);
            da.Update(dsPubs, "UserTable");

原创粉丝点击