|
<p ><ccid_nobr>用Delphi或者VB编程,在对数据库中的记录进行操作的时候,经常用到一个名称为数据导航器的组件,通过这个组件,可以非常方便的实现对已经绑定到此组件的数据表中的记录进行浏览。就是所谓的上一条记录、下一条记录、首记录、尾记录等。那么在Visual C#是否也存在这样的组件呢?答案是否定的。但由于Visual C#有着强大的数据库处理能力,所以可以比较方便的做一个类似于此组件的程序。本文就是来介绍此程序的具体制作过程。<br/><br/><b>一、 程序的主要功能介绍:</b><br/>程序打开本地Acess数据库(sample.mdb)中的book数据表,然后把book数据表中的<br/>字段绑定到程序提供的文本框中,显示出来。通过程序中的四个按钮"首记录"、"尾记录"、"上一条"、"下一条",实现对book数据表中的记录浏览。程序的运行界面如下:<br/><br/>图01:对数据表中记录浏览程序的运行界面<br/><br/><b>二、程序设计和运行的环境设置:</b><br/>(1)视窗2000服务器版<br/>(2)Microsoft Acess Data Component 2.6 ( MADC 2.6 )<br/><br/><b>三、程序设计难点和应该注意的问题:</b><br/>(1)如何实现把数据表中的字段用文本框来显示:<br/>如果直接把字段的值赋值给文本框,这时如果用"下一条"等按钮来浏览数据记录的时候,文本框的值是不会变化的。如何让文本框根据数据表中的记录指针来动态的显示要字段值,这是本文的一个重点,也是一个难点。<br/>本文是通过把数据表中的字段值绑定到文本框的"Text"属性上,来实现动态显示字段数值的。实现这种处理要用到文本框的DataBindings属性和其中的Add方法。具体语法如下:<br/>文本组件名称.DataBindings.Add ( "Text" , DataSet对象 , 数据表和字段名称 ) ;<br/>在程序具体如下:<br/><table width="580" border="1" cellspacing="0" cellpadding="0" bordercolorlight ="" "black" bordercolordark ="" "#ffffff"><tr><td bgcolor="e6e6e6" class="code">t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;</td></tr></table><br/>这样就可以根据记录指针来实现要显示的字段值了。<br/>(2)如何改变记录指针:<br/>只有掌握如何改变记录指针,才可以随心所欲的浏览记录。Visual C#改变记录指针是通过一个命叫BindingManagerBase对象来实现的。此对象封装在名称空间System.Windows.Froms中。BindingManagerBase对象是一个抽象的对象,管理所有绑定的同类的数据源和数据成员。在程序设计中主要用到BindingManagerBase对象中的二个属性,即:Position属性和Count属性。第一个属性是记录了数据集的当前指针,后一个属性是当前数据集中的记录总数。由此可以得到改变记录指针的四个按钮对应的程序代码:<br/><table width="580" border="1" cellspacing="0" cellpadding="0" bordercolorlight ="" "black" bordercolordark ="" "#ffffff"><tr><td bgcolor="e6e6e6" class="code">i>.首记录:<br/>myBind.Position = 0 ;<br/>ii>.尾记录:<br/>myBind.Position = myBind.Count - 1 ;<br/>iii>.下一条记录和操作后运行界面:<br/>if ( myBind.Position == myBind.Count -1 )<br/>MessageBox.Show ( "已经到了最后一条记录!" ) ;<br/>else<br/>myBind.Position += 1 ;<br/><br/>iV>.上一条记录和操作后运行界面:<br/>if ( myBind.Position == 0 )<br/>MessageBox.Show ( "已经到了第一条记录!" ) ;<br/>else<br/>myBind.Position -= 1 ;<br/><br/></td></tr></table><br/><b>四.程序源代码:</b><br/><table width="580" border="1" cellspacing="0" cellpadding="0" bordercolorlight ="" "black" bordercolordark ="" "#ffffff"><tr><td bgcolor="e6e6e6" class="code">using System ;<br/>using System.Drawing ;<br/>using System.ComponentModel ;<br/>using System.Windows.Forms ;<br/>using System.Data.OleDb ;<br/>using System.Data ;<br/><br/>public class DataView : Form {<br/>private System.ComponentModel.Container components ;<br/>private Button lastrec ;<br/>private Button nextrec ;<br/>private Button previousrec ;<br/>private Button firstrec ;<br/>private TextBox t_books ;<br/>private TextBox t_bookprice ;<br/>private TextBox t_bookauthor ;<br/>private TextBox t_booktitle ;<br/>private TextBox t_bookid ;<br/>private Label l_books ;<br/>private Label l_bookprice ;<br/>private Label l_bookauthor ;<br/>private Label l_booktitle ;<br/>private Label l_bookid ;<br/>private Label label1 ;<br/>private System.Data.DataSet myDataSet ;<br/>private BindingManagerBase myBind ;<br/><br/>public DataView ( )<br/>{<br/>//连接到一个数据库<br/>GetConnected ( ) ;<br/>// 对窗体中所需要的内容进行初始化<br/>InitializeComponent ( );<br/>}<br/>public override void Dispose ( ) {<br/>base.Dispose ( ) ;<br/>components.Dispose ( ) ;<br/>}<br/>public static void Main ( ) {<br/>Application.Run ( new DataView ( ) ) ;<br/>}<br/>public void GetConnected ( )<br/>{<br/>try{<br/>//创建一个 OleDbConnection<br/>string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb" ;<br/>OleDbConnection myConn = new OleDbConnection ( strCon ) ;<br/>string strCom = " SELECT * FROM books " ;<br/>//创建一个 DataSet<br/>myDataSet = new DataSet ( ) ;<br/><br/>myConn.Open ( ) ;<br/>//用 OleDbDataAdapter 得到一个数据集<br/>OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;<br/>//把Dataset绑定books数据表<br/>myCommand.Fill ( myDataSet , "books" ) ;<br/>//关闭此OleDbConnection<br/>myConn.Close ( ) ;<br/>}<br/>catch ( Exception e )<br/>{<br/>MessageBox.Show ( "连接错误! " + e.ToString ( ) , "错误" ) ;<br/>}<br/>}<br/>private void InitializeComponent ( )<br/>{<br/>this.components = new System.ComponentModel.Container ( ) ;<br/>this.t_bookid = new TextBox ( ) ;<br/>this.nextrec = new Button ( ) ;<br/>this.lastrec = new Button ( ) ;<br/>this.l_bookid = new Label ( ) ;<br/>this.t_books = new TextBox ( ) ;<br/>this.t_booktitle = new TextBox ( ) ;<br/>this.t_bookprice = new TextBox ( ) ;<br/>this.firstrec = new Button ( ) ;<br/>this.l_booktitle = new Label ( ) ;<br/>this.l_bookprice = new Label ( ) ;<br/>this.l_books = new Label ( ) ;<br/>this.previousrec = new Button ( ) ;<br/>this.l_bookauthor = new Label ( ) ;<br/>this.t_bookauthor = new TextBox ( ) ;<br/>this.label1 = new Label ( ) ;<br/><br/>//以下是对数据浏览的四个按钮进行初始化<br/>firstrec.Location = new System.Drawing.Point ( 55 , 312 ) ;<br/>firstrec.ForeColor = System.Drawing.Color.Black ;<br/>firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br/>firstrec.TabIndex = 5 ;<br/>firstrec.Font = new System.Drawing.Font("仿宋", 8f );<br/>firstrec.Text = "首记录";<br/>firstrec.Click += new System.EventHandler(GoFirst);<br/>previousrec.Location = new System.Drawing.Point ( 125 , 312 ) ;<br/>previousrec.ForeColor = System.Drawing.Color.Black ;<br/>previousrec.Size = new System.Drawing.Size(40, 24) ;<br/>previousrec.TabIndex = 6 ;<br/>previousrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;<br/>previousrec.Text = "上一条" ;<br/>previousrec.Click += new System.EventHandler ( GoPrevious ) ;<br/>nextrec.Location = new System.Drawing.Point ( 195 , 312 );<br/>nextrec.ForeColor = System.Drawing.Color.Black ;<br/>nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br/>nextrec.TabIndex = 7 ;<br/>nextrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;<br/>nextrec.Text = "下一条" ;<br/>nextrec.Click += new System.EventHandler ( GoNext );<br/><br/>lastrec.Location = new System.Drawing.Point ( 265 , 312 ) ;<br/>lastrec.ForeColor = System.Drawing.Color.Black ;<br/>lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ;<br/>lastrec.TabIndex = 8 ;<br/>lastrec.Font = new System.Drawing.Font ( "仿宋" , 8f ) ;<br/>lastrec.Text = "尾记录" ;<br/>lastrec.Click += new System.EventHandler ( GoLast ) ;<br/>//以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不同的绑定到文本框"Text"属性上<br/>t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ;<br/>t_bookid.TabIndex = 9 ;<br/>t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ;<br/>t_bookid.DataBindings.Add ( "Text" , myDataSet , "books.bookid" ) ;<br/><br/>t_books.Location = new System.Drawing.Point ( 184 , 264 ) ;<br/>t_books.TabIndex = 10 ;<br/>t_books.Size = new System.Drawing.Size ( 80 , 20 ) ;<br/>t_books.DataBindings.Add ( "Text" , myDataSet , "books.bookstock" ) ;<br/><br/>t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ;<br/>t_booktitle.TabIndex = 11 ;<br/>t_booktitle.Size = new System.Drawing.Size ( 176 , 20 ) ;<br/>t_booktitle.DataBindings.Add( "Text" , myDataSet , "books.booktitle" ) ;<br/><br/>t_bookprice.Location = new System.Drawing.Point ( 184 , 212 ) ;<br/>t_bookprice.TabIndex = 12 ;<br/>t_bookprice.Size = new System.Drawing.Size ( 80 , 20 ) ;<br/>t_bookprice.DataBindings.Add ( "Text" , myDataSet , "books.bookprice" ) ;<br/><br/>t_bookauthor.Location = new System.Drawing.Point ( 184 , 160 ) ;<br/>t_bookauthor.TabIndex = 18 ;<br/>t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ;<br/>t_bookauthor.DataBindings.Add ( "Text" , myDataSet , "books.bookauthor" ) ;<br/>l_bookid.Location = new System.Drawing.Point ( 24 , 56 ) ;<br/>l_bookid.Text = "书本序号:" ;<br/>l_bookid.Size = new System.Drawing.Size ( 112, 20 ) ;<br/>l_bookid.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;<br/>l_bookid.TabIndex = 13 ;<br/>l_bookid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br/>l_booktitle.Location = new System.Drawing.Point ( 24 , 108 ) ;<br/>l_booktitle.Text = "书 名:";<br/>l_booktitle.Size = new System.Drawing.Size ( 112 , 20 ) ;<br/>l_booktitle.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;<br/>l_booktitle.TabIndex = 14 ;<br/>l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br/><br/>l_bookprice.Location = new System.Drawing.Point ( 24 , 212 ) ;<br/>l_bookprice.Text = "价 格:" ;<br/>l_bookprice.Size = new System.Drawing.Size ( 112 , 20 ) ;<br/>l_bookprice.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;<br/>l_bookprice.TabIndex = 15 ;<br/>l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br/><br/>l_books.Location = new System.Drawing.Point ( 24 , 264 ) ;<br/>l_books.Text = "书 架 号:" ;<br/>l_books.Size = new System.Drawing.Size ( 112 , 20 ) ;<br/>l_books.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;<br/>l_books.TabIndex = 16 ;<br/>l_books.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br/><br/>l_bookauthor.Location = new System.Drawing.Point ( 24 , 160 ) ;<br/>l_bookauthor.Text = "作 者:" ;<br/>l_bookauthor.Size = new System.Drawing.Size ( 112 , 20 ) ;<br/>l_bookauthor.Font = new System.Drawing.Font ( "仿宋" , 10f ) ;<br/>l_bookauthor.TabIndex = 17 ;<br/>l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;<br/><br/>label1.Location = new System.Drawing.Point ( 49 , 8 ) ;<br/>label1.Text = "浏览书籍信息" ;<br/>label1.Size = new System.Drawing.Size ( 296 , 24 ) ;<br/>label1.ForeColor = System.Drawing.Color.Green ;<br/>label1.Font = new System.Drawing.Font ( "仿宋" , 15f ) ;<br/>label1.TabIndex = 19 ;<br/>//对窗体进行设定<br/>this.Text = "用C#做浏览数据库中记录的程序!";<br/>this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;<br/>this.FormBorderStyle = FormBorderStyle.FixedSingle ;<br/>this.ClientSize = new System.Drawing.Size ( 394 , 375 ) ;<br/>//在窗体中加入组件<br/>this.Controls.Add ( lastrec ) ;<br/>this.Controls.Add ( nextrec ) ;<br/>this.Controls.Add ( previousrec ) ;<br/>this.Controls.Add ( firstrec ) ;<br/>this.Controls.Add ( t_books ) ;<br/>this.Controls.Add ( t_bookprice ) ;<br/>this.Controls.Add ( t_bookauthor ) ;<br/>this.Controls.Add ( t_booktitle ) ;<br/>this.Controls.Add ( t_bookid ) ;<br/>this.Controls.Add ( l_books ) ;<br/>this.Controls.Add ( l_bookprice ) ;<br/>this.Controls.Add ( l_bookauthor ) ;<br/>this.Controls.Add ( l_booktitle ) ;<br/>this.Controls.Add ( l_bookid ) ;<br/>this.Controls.Add ( label1 ) ;<br/>//把对象DataSet和"books"数据表绑定到此myBind对象<br/>myBind= this.BindingContext [ myDataSet , "books" ] ;<br/>}<br/>//按钮"尾记录"对象事件程序<br/>protected void GoLast ( object sender , System.EventArgs e )<br/>{<br/>myBind.Position = myBind.Count - 1 ;<br/>}<br/><br/>//按钮"下一条"对象事件程序<br/>protected void GoNext ( object sender , System.EventArgs e )<br/>{<br/>if ( myBind.Position == myBind.Count -1 )<br/>MessageBox.Show ( "已经到了最后一条记录!" ) ;<br/>else<br/>myBind.Position += 1 ;<br/>}<br/>//按钮"上一条"对象事件程序<br/>protected void GoPrevious ( object sender , System.EventArgs e )<br/>{<br/>if ( myBind.Position == 0 )<br/>MessageBox.Show ( "已经到了第一条记录!" ) ;<br/>else<br/>myBind.Position -= 1 ;<br/>}<br/>//按钮"首记录"对象事件程序<br/>protected void GoFirst ( object sender , System.EventArgs e )<br/>{<br/>myBind.Position = 0 ;<br/>}<br/>}<br/></td></tr></table><br/><b>五.总结:</b><br/> 本文的重点就在于如何用Visual C#改变数据集的记录指针和如何让文本框根据记录指针的变化而改变显示内容。虽然此类处理在Visual C#比起用其他语言要显得麻烦些。但对于程序设计人员却更灵活了,使得程序设计人员有了更大的发展空间。<br/><br/>(责任编辑 <ccid_nobr>尤北</ccid_nobr>)<p ></ccid_nobr><p align="center"></p></p> |
|