欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用DataTable更新数据库(增,删,改)

程序员文章站 2024-03-03 16:19:52
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);