二.用visual c#往sql server数据库中插入记录:
(1)用visual c#往access 2000和sql server添加记录的主要区别在于使用了不同的数据库引擎。在编写程序之前,首先假设数据库服务器名称为:server1,要访问的数据库名称为:data1,数据表名称为:books。用户名为:sa。其中数据表的数据结构和access 2000的表的结构相同。下面是程序中打开sql server的数据引擎程序代码:
// 设定数据连接字符串,此字符串的意思是打开sql server数据库,服务器名称为server1,数据库为data1
| string strcon = "provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = data1 ; data source = server1 " ; oledbconnection myconn = new oledbconnection ( strcon ) ; myconn.open ( ) ; |
(2).用visual c#往sql server 数据库中插入记录的源程序代码( add02.cs ):
| using system ; using system.drawing ; using system.componentmodel ; using system.windows.forms ; using system.data.oledb ; using system.data ; //导入程序中使用到的名称空间 public class dataadd : form { private button lastrec ; private button nextrec ; private button previousrec ; private button firstrec ; private container components ; private label title ; private button t_new ; private button save ; private textbox t_bookstock ; private textbox t_bookprice ; private textbox t_bookauthor ; private textbox t_booktitle ; private textbox t_bookid ; private label l_bookstock ; private label l_bookprice ; private label l_bookauthor ; private label l_booktitle ; private label l_bookid ; private dataset mydataset ; private bindingmanagerbase mybind ; //定义在程序中要使用的组件 public dataadd ( ) { //连接到一个数据库 getconnected ( ) ; // 对窗体中所需要的内容进行初始化 initializecomponent ( ); } //释放程序使用过的所以资源 public override void dispose ( ) { base.dispose ( ) ; components.dispose ( ) ; } public static void main ( ) { application.run ( new dataadd ( ) ) ; } public void getconnected ( ) { try{ // 设定数据连接字符串,此字符串的意思是打开sql server数据库,服务器名称为server1,数据库为data1,用户名为sa。 string strcon = "provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = data1 ; data source = server1 " ; oledbconnection myconn = new oledbconnection ( strcon ) ; myconn.open ( ) ; string strcom = " select * from books " ; //创建一个 dataset mydataset = new dataset ( ) ; //用 oledbdataadapter 得到一个数据集 oledbdataadapter mycommand = new oledbdataadapter ( strcom , myconn ) ; //把dataset绑定books数据表 mycommand.fill ( mydataset , "books" ) ; //关闭此oledbconnection myconn.close ( ) ; } catch ( exception e ) { messagebox.show ( "连接错误! " + e.tostring ( ) , "错误" ) ; } } private void initializecomponent ( ) { components = new system.componentmodel.container ( ) ; nextrec = new button ( ) ; lastrec = new button ( ) ; previousrec = new button ( ) ; firstrec = new button ( ) ; t_bookprice = new textbox ( ) ; l_booktitle = new label ( ) ; l_bookprice = new label ( ) ; l_bookauthor = new label ( ) ; t_bookid = new textbox ( ) ; save = new button ( ) ; title = new label ( ) ; t_bookauthor = new textbox ( ) ; t_booktitle = new textbox ( ) ; t_new = new button ( ) ; l_bookstock = new label ( ) ; t_bookstock = new textbox ( ) ; l_bookid = new label ( ) ; //以下是对数据浏览的四个按钮进行初始化 firstrec.location = new system.drawing.point ( 65 , 312 ) ; firstrec.forecolor = system.drawing.color.black ; firstrec.size = new system.drawing.size ( 40 , 24 ) ; firstrec.font = new system.drawing.font("仿宋", 8f ); firstrec.text = "首记录"; firstrec.click += new system.eventhandler(gofirst); previousrec.location = new system.drawing.point ( 135 , 312 ) ; previousrec.forecolor = system.drawing.color.black ; previousrec.size = new system.drawing.size(40, 24) ; previousrec.font = new system.drawing.font ( "仿宋" , 8f ) ; previousrec.text = "上一条" ; previousrec.click += new system.eventhandler ( goprevious ) ; nextrec.location = new system.drawing.point ( 205 , 312 ); nextrec.forecolor = system.drawing.color.black ; nextrec.size = new system.drawing.size ( 40 , 24 ) ; nextrec.font = new system.drawing.font ( "仿宋" , 8f ) ; nextrec.text = "下一条" ; nextrec.click += new system.eventhandler ( gonext ); lastrec.location = new system.drawing.point ( 275 , 312 ) ; lastrec.forecolor = system.drawing.color.black ; lastrec.size = new system.drawing.size ( 40 , 24 ) ; lastrec.font = new system.drawing.font ( "仿宋" , 8f ) ; lastrec.text = "尾记录" ; lastrec.click += new system.eventhandler ( golast ) ; //以下是对显示标签进行初始化 l_bookid.location = new system.drawing.point ( 24 , 56 ) ; l_bookid.text = "书本序号:" ; l_bookid.size = new system.drawing.size ( 112, 20 ) ; l_bookid.font = new system.drawing.font ( "仿宋" , 10f ) ; l_bookid.textalign = system.drawing.contentalignment.middlecenter ; l_booktitle.location = new system.drawing.point ( 24 , 108 ) ; l_booktitle.text = "书 名:"; l_booktitle.size = new system.drawing.size ( 112 , 20 ) ; l_booktitle.font = new system.drawing.font ( "仿宋" , 10f ) ; l_booktitle.textalign = system.drawing.contentalignment.middlecenter ; l_bookprice.location = new system.drawing.point ( 24 , 212 ) ; l_bookprice.text = "价 格:" ; l_bookprice.size = new system.drawing.size ( 112 , 20 ) ; l_bookprice.font = new system.drawing.font ( "仿宋" , 10f ) ; l_bookprice.textalign = system.drawing.contentalignment.middlecenter ; l_bookstock.location = new system.drawing.point ( 24 , 264 ) ; l_bookstock.text = "书 架 号:" ; l_bookstock.size = new system.drawing.size ( 112 , 20 ) ; l_bookstock.font = new system.drawing.font ( "仿宋" , 10f ) ; l_bookstock.tabindex = 16 ; l_bookstock.textalign = system.drawing.contentalignment.middlecenter ; l_bookauthor.location = new system.drawing.point ( 24 , 160 ) ; l_bookauthor.text = "作 者:" ; l_bookauthor.size = new system.drawing.size ( 112 , 20 ) ; l_bookauthor.font = new system.drawing.font ( "仿宋" , 10f ) ; l_bookauthor.textalign = system.drawing.contentalignment.middlecenter ; title.location = new system.drawing.point ( 32 , 16 ) ; title.text = "利用vsiual c#来增加数据记录!" ; title.size = new system.drawing.size ( 336 , 24 ) ; title.forecolor = system.drawing.color.green ; title.font = new system.drawing.font ( "仿宋" , 14f , system.drawing.fontstyle.bold ) ; //以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不同的绑定到文本框"text"属性上 t_bookid.location = new system.drawing.point ( 184 , 56 ) ; t_bookid.size = new system.drawing.size ( 80 , 20 ) ; t_bookid.databindings.add ( "text" , mydataset , "books.bookid" ) ; t_bookstock.location = new system.drawing.point ( 184 , 264 ) ; t_bookstock.size = new system.drawing.size ( 80 , 20 ) ; t_bookstock.databindings.add ( "text" , mydataset , "books.bookstock" ) ; t_booktitle.location = new system.drawing.point ( 184 , 108 ) ; t_booktitle.size = new system.drawing.size ( 176 , 20 ) ; t_booktitle.databindings.add( "text" , mydataset , "books.booktitle" ) ; t_bookprice.location = new system.drawing.point ( 184 , 212 ) ; t_bookprice.size = new system.drawing.size ( 80 , 20 ) ; t_bookprice.databindings.add ( "text" , mydataset , "books.bookprice" ) ; t_bookauthor.location = new system.drawing.point ( 184 , 160 ) ; t_bookauthor.size = new system.drawing.size ( 128 , 20 ) ; t_bookauthor.databindings.add ( "text" , mydataset , "books.bookauthor" ) ; t_new.location = new system.drawing.point ( 62 , 354 ) ; t_new.size = new system.drawing.size ( 96 , 32 ) ; t_new.text = "新建记录" ; t_new.click += new system.eventhandler ( t_newclick ) ; save.location = new system.drawing.point ( 222 , 354 ) ; save.size = new system.drawing.size ( 96 , 32 ) ; save.tabindex = 4 ; save.text = "保存记录" ; save.click += new system.eventhandler ( saveclick ) ; this.text = "利用vsiual c#来增加数据记录的程序窗口!" ; this.autoscalebasesize = new system.drawing.size ( 5 , 13 ) ; this.formborderstyle = formborderstyle.fixed3d ; this.clientsize = new system.drawing.size ( 390 , 400 ) ; //在窗体中加入下列组件 this.controls.add ( lastrec ) ; this.controls.add ( nextrec ) ; this.controls.add ( previousrec ) ; this.controls.add ( firstrec ) ; this.controls.add ( title ) ; this.controls.add ( t_new ) ; this.controls.add ( save ) ; this.controls.add ( t_bookstock ) ; this.controls.add ( t_bookprice ) ; this.controls.add ( t_bookauthor ) ; this.controls.add ( t_booktitle ) ; this.controls.add ( t_bookid ) ; this.controls.add ( l_bookstock ) ; this.controls.add ( l_bookprice ) ; this.controls.add ( l_bookauthor ) ; this.controls.add ( l_booktitle ) ; this.controls.add ( l_bookid ) ; //把对象dataset和"books"数据表绑定到此mybind对象 mybind= this.bindingcontext [ mydataset , "books" ] ; } protected void saveclick ( object sender , system.eventargs e ) { try { //判断所有字段是否添完,添完则执行,反之弹出提示 if ( t_bookid.text != "" && t_booktitle.text != "" && t_bookauthor.text != "" && t_bookprice.text != "" && t_bookstock.text != "" ) { // 设定数据连接字符串,此字符串的意思是打开sql server数据库,服务器名称为server1,数据库为data1,用户名为sa。 string strconn = "provider = sqloledb.1 ; persist security info = false ; user id = sa ; initial catalog = datal ; data source = server1 " ; oledbconnection myconn = new oledbconnection ( strconn ) ; myconn.open ( ) ; string strinsert = " insert into books ( bookid , booktitle , bookauthor , bookprice , bookstock ) values ( " ; strinsert += t_bookid.text + ", " ; strinsert += t_booktitle.text + ", " ; strinsert += t_bookauthor.text + ", " ; strinsert += t_bookprice.text + ", " ; strinsert += t_bookstock.text + ")" ; oledbcommand inst = new oledbcommand ( strinsert , myconn ) ; inst.executenonquery ( ) ; myconn.close ( ) ; } else { messagebox.show ( "必须填满所有字段值!" , "错误!" ) ; } } catch ( exception ed ) { messagebox.show ( "保存数据记录发生 " + ed.tostring ( ) , "错误!" ) ; } } protected void t_newclick ( object sender , system.eventargs e ) { t_bookid.text = "" ; t_booktitle.text = "" ; t_bookauthor.text = "" ; t_bookprice.text = "" ; t_bookstock.text = "" ; } //按钮"尾记录"对象事件程序 protected void golast ( object sender , system.eventargs e ) { mybind.position = mybind.count – 1 ; } //按钮"下一条"对象事件程序 protected void gonext ( object sender , system.eventargs e ) { if ( mybind.position == mybind.count -1 ) messagebox.show ( "已经到了最后一条记录!" ) ; else mybind.position += 1 ; } //按钮"上一条"对象事件程序 protected void goprevious ( object sender , system.eventargs e ) { if ( mybind.position == 0 ) messagebox.show ( "已经到了第一条记录!" ) ; else mybind.position -= 1 ; } //按钮"首记录"对象事件程序 protected void gofirst ( object sender , system.eventargs e ) { mybind.position = 0 ; } } |
三.总结:
本文主要是通过二个程序的例子来具体说明用visual c#如何往远程数据库–sql server和本地数据库– access 2000中插入记录。对于其他类型的数据库也可以比照这二个这二个数据库来处理,一般来说,用visual c#处理数据库只是在选用数据库引擎上有较大的差别,在程序中的具体设计和处理上,还是很类似的。最后希望此篇文章能对你的数据库编程有所帮助。
