




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
用Visual C中轻松浏览数据库记录 一、 程式的主要功能介绍: 程式打开本地Acess数据库(sample.mdb)中的book数据表,然后把book数据表中的 字段绑定到程式提供的文本框中,显示出来。通过程式中的四个按钮首记录、尾记录、上一条、下一条,实现对book数据表中的记录浏览。程式的运行界面如下: 图01:对数据表中记录浏览程式的运行界面 二、程式设计和运行的环境设置: (1)视窗2000服务器版 (2)Microsoft Acess Data Component 2.6 ( MADC 2.6 ) 三、程式设计难点和应该注意的问题: (1)怎么实现把数据表中的字段用文本框来显示: 如果直接把字段的值赋值给文本框,这时如果用下一条等按钮来浏览数据记录的时候,文本框的值是不会变化的。怎么让文本框根据数据表中的记录指针来动态的显示要字段值,这是本文的一个重点,也是个难点。 本文是通过把数据表中的字段值绑定到文本框的Text属性上,来实现动态显示字段数值的。实现这种处理要用到文本框的DataBindings属性和其中的Add方法。具体语法如下: 文本组件名称.DataBindings.Add ( Text , DataSet对象 , 数据表和字段名称 ) ; 在程式具体如下: t_bookid.DataBindings.Add ( Text , myDataSet , books.bookid ) ; 这样就能根据记录指针来实现要显示的字段值了。 (2)怎么改动记录指针: 只有掌控怎么改动记录指针,才能随心所欲的浏览记录。Visual C#改动记录指针是通过一个命叫BindingManagerBase对象来实现的。此对象封装在名称空间System.视窗系统.Froms中。BindingManagerBase对象是个抽象的对象,管理所有绑定的同类的数据源和数据成员。在程式设计中主要用到BindingManagerBase对象中的二个属性,即:Position属性和Count属性。第一个属性是记录了数据集的当前指针,后一个属性是当前数据集中的记录总数。由此能得到改动记录指针的四个按钮对应的程式代码: i.首记录: myBind.Position = 0 ; ii.尾记录: myBind.Position = myBind.Count - 1 ; iii.下一条记录和操作后运行界面: if ( myBind.Position = myBind.Count -1 ) MessageBox.Show ( 已到了最后一条记录! ) ; else myBind.Position += 1 ; iV.上一条记录和操作后运行界面: if ( myBind.Position = 0 ) MessageBox.Show ( 已到了第一条记录! ) ; else myBind.Position -= 1 ; 四.程式原始码: using System ; using System.Drawing ; using System.ComponentModel ; using System.视窗系统.Forms ; using System.Data.OleDb ; using System.Data ; public class DataView : Form private System.ComponentModel.Container components ; private Button lastrec ; private Button nextrec ; private Button previousrec ; private Button firstrec ; private TextBox t_books ; private TextBox t_bookprice ; private TextBox t_bookauthor ; private TextBox t_booktitle ; private TextBox t_bookid ; private Label l_books ; private Label l_bookprice ; private Label l_bookauthor ; private Label l_booktitle ; private Label l_bookid ; private Label label1 ; private System.Data.DataSet myDataSet ; private BindingManagerBase myBind ; public DataView ( ) /连接到一个数据库 GetConnected ( ) ; / 对窗体中所需要的内容进行初始化 InitializeComponent ( ); public override void Dispose ( ) base.Dispose ( ) ; components.Dispose ( ) ; public static void Main ( ) Application.Run ( new DataView ( ) ) ; public void GetConnected ( ) try /创建一个 OleDbConnection string strCon = Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = SELECT * FROM books ; /创建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; /用 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 ( ) ponents = new System.ComponentModel.Container ( ) ; this.t_bookid = new TextBox ( ) ; this.nextrec = new Button ( ) ; this.lastrec = new Button ( ) ; this.l_bookid = new Label ( ) ; this.t_books = new TextBox ( ) ; this.t_booktitle = new TextBox ( ) ; this.t_bookprice = new TextBox ( ) ; this.firstrec = new Button ( ) ; this.l_booktitle = new Label ( ) ; this.l_bookprice = new Label ( ) ; this.l_books = new Label ( ) ; this.previousrec = new Button ( ) ; this.l_bookauthor = new Label ( ) ; this.t_bookauthor = new TextBox ( ) ; this.label1 = new Label ( ) ; /以下是对数据浏览的四个按钮进行初始化 firstrec.Location = new System.Drawing.Point ( 55 , 312 ) ; firstrec.ForeColor = System.Drawing.Color.Black ; firstrec.Size = new System.Drawing.Size ( 40 , 24 ) ; firstrec.TabIndex = 5 ; firstrec.Font = new System.Drawing.Font(仿宋, 8f ); firstrec.Text = 首记录; firstrec.Click += new System.EventHandler(GoFirst); previousrec.Location = new System.Drawing.Point ( 125 , 312 ) ; previousrec.ForeColor = System.Drawing.Color.Black ; previousrec.Size = new System.Drawing.Size(40, 24) ; previousrec.TabIndex = 6 ; previousrec.Font = new System.Drawing.Font ( 仿宋 , 8f ) ; previousrec.Text = 上一条 ; previousrec.Click += new System.EventHandler ( GoPrevious ) ; nextrec.Location = new System.Drawing.Point ( 195 , 312 ); nextrec.ForeColor = System.Drawing.Color.Black ; nextrec.Size = new System.Drawing.Size ( 40 , 24 ) ; nextrec.TabIndex = 7 ; nextrec.Font = new System.Drawing.Font ( 仿宋 , 8f ) ; nextrec.Text = 下一条 ; nextrec.Click += new System.EventHandler ( GoNext ); lastrec.Location = new System.Drawing.Point ( 265 , 312 ) ; lastrec.ForeColor = System.Drawing.Color.Black ; lastrec.Size = new System.Drawing.Size ( 40 , 24 ) ; lastrec.TabIndex = 8 ; lastrec.Font = new System.Drawing.Font ( 仿宋 , 8f ) ; lastrec.Text = 尾记录 ; lastrec.Click += new System.EventHandler ( GoLast ) ; /以下是对为显示数据记录而设定的标签和文本框进行初始化,并把记录绑定在不同的绑定到文本框Text属性上 t_bookid.Location = new System.Drawing.Point ( 184 , 56 ) ; t_bookid.TabIndex = 9 ; t_bookid.Size = new System.Drawing.Size ( 80 , 20 ) ; t_bookid.DataBindings.Add ( Text , myDataSet , books.bookid ) ; t_books.Location = new System.Drawing.Point ( 184 , 264 ) ; t_books.TabIndex = 10 ; t_books.Size = new System.Drawing.Size ( 80 , 20 ) ; t_books.DataBindings.Add ( Text , myDataSet , books.bookstock ) ; t_booktitle.Location = new System.Drawing.Point ( 184 , 108 ) ; t_booktitle.TabIndex = 11 ; 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.TabIndex = 12 ; 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.TabIndex = 18 ; t_bookauthor.Size = new System.Drawing.Size ( 128 , 20 ) ; t_bookauthor.DataBindings.Add ( Text , myDataSet , books.bookauthor ) ; 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.TabIndex = 13 ; 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.TabIndex = 14 ; 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.TabIndex = 15 ; l_bookprice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ; l_books.Location = new System.Drawing.Point ( 24 , 264 ) ; l_books.Text = 书 架 号: ; l_books.Size = new System.Drawing.Size ( 112 , 20 ) ; l_books.Font = new System.Drawing.Font ( 仿宋 , 10f ) ; l_books.TabIndex = 16 ; l_books.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.TabIndex = 17 ; l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ; label1.Location = new System.Drawing.Point ( 49 , 8 ) ; label1.Text = 浏览书籍信息 ; label1.Size = new System.Drawing.Size ( 296 , 24 ) ; label1.ForeColor = System.Drawing.Color.Green ; label1.Font = new System.Drawing.Font ( 仿宋 , 15f ) ; label1.TabIndex = 19 ; /对窗体进行设定 this.Text = 用C#做浏览数据库中记录的程式!; this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ; this.FormBorderStyle = FormBorderStyle.FixedSingle ; this.ClientSize = new System.Drawing.Size ( 394 , 375 ) ; /在窗体中加入组件 this.Controls.Add ( lastrec ) ; this.Controls.Add ( nextrec ) ; this.Controls.Add ( previousrec ) ; this.Controls.Add ( firstrec ) ; this.Controls.Add ( t_books ) ; this.Controls.Add ( t_bookprice ) ; this.Controls.Add ( t_bookauthor ) ; this.Controls.Add ( t_booktitle ) ; this.Controls.Add ( t_bookid ) ; this.Controls.Add ( l_books ) ; this.Controls.Add ( l_bookpr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年保安证考试模拟试题及答案解析
- 2025年临床检验知识试题及答案
- 2025年小学英语教师进城选调考试试题及参考答案
- 2025年电气自动化工程师职业资格考试试题及答案
- 2025年四川省乐山市事业单位工勤技能考试题库及答案
- 林业基因编辑研究创新创业项目商业计划书
- 2025年门急诊医院感染培训试题(附答案)
- 放射性污染监测服务创新创业项目商业计划书
- 2025年国家公务员考试公文基础知识试题库及答案
- 摄影电商平台创新创业项目商业计划书
- 中秋节知识课件
- 110kV变电站及110kV输电线路运维投标技术方案
- 人教版(新教材)高中生物选择性必修1课件3:4 3 免疫失调
- 《SLT 582-2025水工金属结构制造安装质量检验检测规程》知识培训
- “燕园元培杯”2023-2024学年全国中学生地球科学奥林匹克竞赛决赛试题详解
- 中国血脂管理指南(基层版+2024年)解读
- 分子诊断技术在感染性疾病中的应用-深度研究
- 《智能AI分析深度解读报告》课件
- 气道异物护理教学
- 2024年版机电产品国际招标标准招标文件
- 企业合规经营规范手册
评论
0/150
提交评论