使用DataTable更新数据库(增,删,改)
程序员文章站
2024-03-04 19:26:24
1、修改数据复制代码 代码如下: dat...
1、修改数据
datarow dr = hrdataset.tables["emp"].rows.find(textbox3.text);
//datarow dr = hrdataset.tables["emp"].select("id="+textbox3.text)[0];
dr.beginedit();
dr["name"] = textbox1.text;
dr.endedit();
sqlcommandbuilder cmd=new sqlcommandbuilder(emptableadapter);
emptableadapter.update(hrdataset);
this.emptableadapter.fill(this.hrdataset.emp);
2、插入数据
dataset ds = new dataset();
using (sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["db"].connectionstring))
{
string sql = "select id,name,age from emp";
sqldataadapter adp = new sqldataadapter(sql, conn);
adp.fill(ds);
datarow dr=ds.tables[0].newrow();
dr["name"]=txtname.text;
dr["age"] = convert.toint32(txtage.text);
sqlcommandbuilder cmd=new sqlcommandbuilder(adp);
ds.tables[0].rows.add(dr);
adp.update(ds);
}
3、删除数据
datarow dr = hrdataset.tables["emp"].rows.find(textbox3.text);
//datarow dr = hrdataset.tables["emp"].select("id="+textbox3.text)[0];
dr.delete();
sqlcommandbuilder cmd=new sqlcommandbuilder(emptableadapter);
emptableadapter.update(hrdataset);
this.emptableadapter.fill(this.hrdataset.emp);
复制代码 代码如下:
datarow dr = hrdataset.tables["emp"].rows.find(textbox3.text);
//datarow dr = hrdataset.tables["emp"].select("id="+textbox3.text)[0];
dr.beginedit();
dr["name"] = textbox1.text;
dr.endedit();
sqlcommandbuilder cmd=new sqlcommandbuilder(emptableadapter);
emptableadapter.update(hrdataset);
this.emptableadapter.fill(this.hrdataset.emp);
2、插入数据
复制代码 代码如下:
dataset ds = new dataset();
using (sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["db"].connectionstring))
{
string sql = "select id,name,age from emp";
sqldataadapter adp = new sqldataadapter(sql, conn);
adp.fill(ds);
datarow dr=ds.tables[0].newrow();
dr["name"]=txtname.text;
dr["age"] = convert.toint32(txtage.text);
sqlcommandbuilder cmd=new sqlcommandbuilder(adp);
ds.tables[0].rows.add(dr);
adp.update(ds);
}
3、删除数据
复制代码 代码如下:
datarow dr = hrdataset.tables["emp"].rows.find(textbox3.text);
//datarow dr = hrdataset.tables["emp"].select("id="+textbox3.text)[0];
dr.delete();
sqlcommandbuilder cmd=new sqlcommandbuilder(emptableadapter);
emptableadapter.update(hrdataset);
this.emptableadapter.fill(this.hrdataset.emp);
推荐阅读