好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#访问SQLServer增删改查代码实例

一个专门实现访问sql server数据库增删改查的操作代码,分享给大家,具体内容如下

 

?

using system;

using system.collections.generic;

using system测试数据ponentmodel;

using system.data;

using system.drawing;

using system.linq;

using system.text;

using system.windows.forms;

using system.data;

using system.data.sqlclient;

 

namespace windowsformsapplication1

{

   public partial class form1 : form

   {

     public form1()

     {

       initializecomponent();

     }

 

     //查询

     private void button1_click( object sender, eventargs e)

     {

       string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ; //定义数据库连接参数

       sqlconnection myconnection = new sqlconnection(myconn); //定义一个数据连接实例

       sqlcommand mycommand = new sqlcommand( "select * from 图书借阅" , myconnection); //定义一个数据库操作指令

       sqldataadapter selectadapter = new sqldataadapter(); //定义一个数据适配器

       selectadapter.selectcommand = mycommand; //定义数据适配器的操作指令

       dataset mydataset = new dataset(); //定义一个数据集

       myconnection.open(); //打开数据库连接

       selectadapter.selectcommand.executenonquery(); //执行数据库查询指令

       myconnection.close(); //关闭数据库

       selectadapter.fill(mydataset); //填充数据集

       datagrid1.datasource = mydataset.tables[0];

       //datagrid1.databind();//将数据表格用数据集中的数据填充

     }

 

     //添加

     private void button2_click( object sender, eventargs e)

     {

       string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ;

       sqlconnection myconnection = new sqlconnection(myconn);

       string myinsert = "insert into 图书借阅 (图书编号,读者编号,续借次数) values ('" + convert.tostring(textbox2.text) + "','" +

         convert.tostring(textbox3.text)+ "','" +convert.toint32(textbox4.text)+ "')" ;

       sqlcommand mycommand = new sqlcommand(myinsert, myconnection);

       try //异常处理

       {

         myconnection.open();

         mycommand.executenonquery();

         myconnection.close();

       }

       catch (exception ex)

       {

         messagebox.show(ex.message);

       }

     }

 

     //更新

     private void button3_click( object sender, eventargs e)

     {

       string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ;

       sqlconnection myconnection = new sqlconnection(myconn);

       string myupdate = "update 图书借阅 set 操作员='" + textbox2.text + "'" + " where 借阅编号=" + "'" + textbox1.text + "'" ;

       sqlcommand mycommand = new sqlcommand(myupdate, myconnection);

       try

       {

         myconnection.open();

         mycommand.executenonquery();

         myconnection.close();

         textbox1.text = "" ;

       }

       catch (exception ex)

       {

         messagebox.show(ex.message);

       }

     }

 

     //删除

     private void button4_click( object sender, eventargs e)

     {

       string myconn = "server=127.0.0.1;uid=sa;pwd=123654;database=libbook;trusted_connection=no" ;

       sqlconnection myconnection = new sqlconnection(myconn);

       string mydelete = "delete from 图书借阅 where 借阅编号=" + textbox1.text;

       sqlcommand mycommand = new sqlcommand(mydelete, myconnection);

       try

       {

         myconnection.open();

         mycommand.executenonquery();

         myconnection.close();

         textbox1.text = "" ;

       }

       catch (exception ex)

       {

         messagebox.show(ex.message);

       }

     }

   }

}

 数据库如下;

winform中查询成功;

插入时,因为借阅编号为自增,不能插入值,会自己生成;

更新,外键冲突;插入的图书编号为000999,无此图书,故出错;

插入成功;

更新操作员为"王老师";

删除借阅编号为31的记录;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

dy("nrwz");

查看更多关于C#访问SQLServer增删改查代码实例的详细内容...

  阅读:53次