|
源代码网推荐
二.用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#处理数据库只是在选用数据库引擎上有较大的差别,在程序中的具体设计和处理上,还是很类似的。最后希望此篇文章能对你的数据库编程有所帮助。 源代码网推荐
源代码网供稿.
|