Visual_C_数据绑定技术[1]_第1页
Visual_C_数据绑定技术[1]_第2页
Visual_C_数据绑定技术[1]_第3页
Visual_C_数据绑定技术[1]_第4页
Visual_C_数据绑定技术[1]_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Visual C#中的数据绑定Visual C自身没有类库,和其他的.Net开发语言一样,Visual C#调用的类库是.Net框架中的一个共有的类库-.Net FrameWork SDK。ADO.NET是.Net FrameWork SDK提供给.Net开发语言进行数据库开发的一个系列类库的集合。在ADO.NET中虽然提供了大量的用于数据库连接、数据处理的类库,但却没有提供类 似DbText组件、DbList组件、DbLable组件、DbCombox组件等。要想把数据记录以ComBox、ListBox等形式显示处理,使 用数据绑定技术是最为方便、最为直接的方法。所谓数据绑定技术就是把已经打开

2、的数据集中某个或者某些字段绑定到组件的某些属性上面的一种技术。说的具体 些,就是把已经打开数据的某个或者某些字段绑定到Text组件、ListBox组件、ComBox等组件上的能够显示数据的属性上面。当对组件完成数据绑 定后,其显示字段的内容将随着数据记录指针的变化而变化。这样程序员就可以定制数据显示方式和内容,从而为以后的数据处理作好准备。所以说数据绑定是 Visual C进行数据库方面编程的基础和最为重要的第一步。只有掌握了数据绑定方法,才可以十分方便对已经打开的数据集中的记录进行浏览、删除、插入等具体的数据 操作、处理。 (下载源码就到源码网:)数据绑定根据不

3、同组件可以分为二种,一种是简单型的数据绑定,另外一种就是复杂型的数据绑定。所谓简单型的数据绑定就是绑定后组 件显示出来的字段只是单个记录,这种绑定一般使用在显示单个值的组件上,譬如:TextBox组件和Label组件。而复杂型的数据绑定就是绑定后的组件 显示出来的字段是多个记录,这种绑定一般使用在显示多个值的组件上,譬如:ComBox组件、ListBox组件等。本文就是来详细介绍如何用 Visual C实现这二种绑定。在数据库的选择上,为了使内容更加全面,采用了当下比较流行的二种数据库,一种是本地数据库Acess 2000,另外一种是远程数据库Sql Server 2000。一、本文程序设计和

4、运行的软件环境(1)微软公司视窗2000服务器版(2).Net FrameWork SDK Beta 2(3)MADC 2.6(Microsoft Acess Data Component)以上版本二、程序中使用的数据库的数据字典(1)本地数据库Access 2000的数据库的名称为db.mdb,在这个数据库中定义了一张表person。这张表的数据结构如下表:字段名称字段类型字段意思id数字序号xm文本姓名xb文本性别nl文本年龄zip文本邮政编码(2)远程数据库Sql Server 2000的数据库服务器名称为Server1,数据库名称为Data1,登陆的ID为sa,口令为空,在数据库也定义

5、了一张person表,数据结构如上表。三、数据绑定一般步骤(一)无论是简单型的数据绑定,还是复杂型的数据绑定,要实现绑定的第一步就是就是要连接数据库,得到可以操作的DataSet。下面二段代码是分别连接Access 2000和Sql Server 2000数据库,并获得DataSet。 (下载源码就到源码网:)(1)连接Access 2000,得到DataSet:/创建一个 OleDbConnection string strCon = Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb ; OleD

6、bConnection myConn = new OleDbConnection ( strCon ) ; string strCom = SELECT * FROM person ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把Dataset绑定person数据表 myCommand

7、.Fill ( myDataSet , person ) ; file:/关闭此OleDbConnection myConn.Close ( ) ;(2)连接Sql Server 2000,得到DataSet:/ 设定数据连接字符串,此字符串的意思是打开Sql server数据库, 服务器名称为server1,数据库为data1 string strCon = Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1

8、 ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strCom = SELECT * FROM person ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把Dataset绑定person数据表 my

9、Command.Fill ( myDataSet , person ) ; file:/关闭此OleDbConnection myConn.Close ( ) ;(二)根据不同组件,采用不同的数据绑定: 对于简单型的数据绑定,数据绑定的方法其实比较简单,在得到数据集以后,一般是通过把数据集中 的某个字段绑定到组件的显示属性上面,譬如TextBox组件和Label组件,是绑定到Text属性。对于复杂型的数据绑定一般是通过设定其某些属 性值来实现绑定的。这些下面将会具体介绍。四、简单型组件的数据绑定: (1)TextBox组件的数据绑定: 通过下列语句就可以把数据集(即为:myDataSet)的某

10、个字段绑定到TextBox组件的“Text”属性上面了:textBox1.DataBindings.Add ( Text , myDataSet , person.xm ) ;注释:此时绑定是Access 2000数据库中person表的xm字段。 由此可以得到绑定TextBox组件的源程序代码(TextBox01.cs),下列代码操作的数据库是Access 2000,如下:public class Form1 : Form private TextBox textBox1 ; private Button button1 ; private System.Data.DataSet myDat

11、aSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; InitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override void Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( di

12、sposing ) ; private void GetConnect ( ) file:/创建一个 OleDbConnection string strCon = Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = SELECT * FROM person ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open

13、 ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把Dataset绑定person数据表 myCommand.Fill ( myDataSet , person ) ; file:/关闭此OleDbConnection myConn.Close ( ) ; private void button1_Click ( object sender , System.EventArgs e ) textBox1.Dat

14、aBindings.Add ( Text , myDataSet , person.xm ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; 得到TextBox组件对本地数据库中的字段进行数据绑定的程序后,可以方便的得到对远程数据库中的某些字段进行数据绑定的源程序代码(TextBox02.cs),具体如下:public class Form1 : Form private TextBox textBox1 ; private Button button1 ; private System.Data.DataSet myDat

15、aSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; InitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override void Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( di

16、sposing ) ; private void GetConnect ( ) / 设定数据连接字符串,此字符串的意思是打开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 ) ; myCon

17、n.Open ( ) ; string strCom = SELECT * FROM person ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把Dataset绑定person数据表 myCommand.Fill ( myDataSet , person ) ; file:/关闭此OleDbConnection myC

18、onn.Close ( ) ; private void button1_Click ( object sender , System.EventArgs e ) textBox1.DataBindings.Add ( Text , myDataSet , person.xm ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; (2)Label组件的数据绑定: 在掌握了TextBox组件数据绑定以后,可以十分方便的得到Label组件的数据绑定方法,因为这二者实现的方法实在是太相似了。下列语句是把得到数据集的xm字段绑定到La

19、bel组件的“Text”属性上:label1.DataBindings.Add ( Text , myDataSet , person.xm ) ;注释:此时绑定是Access 2000数据库中person表的xm字段。由此可以得到Label组件数据绑定的源程序代码(Label01.cs),本代码操作数据库是Access 2000:public class Form1 : Form private Label label1 ; private Button button1 ; private System.Data.DataSet myDataSet ; private System.Comp

20、onentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; InitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override void Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( disposing ) ; private void G

21、etConnect ( ) file:/创建一个 OleDbConnection string strCon = Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = SELECT * FROM person ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file:/用 OleDbDataAd

22、apter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把Dataset绑定person数据表 myCommand.Fill ( myDataSet , person ) ; file:/关闭此OleDbConnection myConn.Close ( ) ; private void button1_Click ( object sender , System.EventArgs e ) label1.DataBindings.Add ( Text , myDat

23、aSet , person.xm ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; 得到了Label组件对Access 2000数据库数据绑定的程序代码,改换一下程序中数据链接,就可以得到Label组件对Sql Server 2000数据库数据绑定的源程序代码(Label02.cs),具体如下:public class Form1 : Form private Label label1 ; private Button button1 ; private System.Data.DataSet myDataSet ; pr

24、ivate System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; InitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override void Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( disposing )

25、 ; private void GetConnect ( ) / 设定数据连接字符串,此字符串的意思是打开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 (

26、) ; string strCom = SELECT * FROM person ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把Dataset绑定person数据表 myCommand.Fill ( myDataSet , person ) ; file:/关闭此OleDbConnection myConn.Close

27、 ( ) ; private void button1_Click ( object sender , System.EventArgs e ) label1.DataBindings.Add ( Text , myDataSet , person.xm ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; 五、复杂型组件的数据绑定:在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。首先来介绍一下ComboBox组件的数据绑定。(下载源码就到源码网:)(1).C

28、omboBox组件的数据绑定:在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:DisplayMember、 ValueMember。其中DataSource是要显示的数据集,DisplayMember是ComboBox组件显示的字段, ValueMember是实际使用值。具体如下:ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = person.xm ;ComboBox1.ValueMember = person.xm ;注释:此时绑定是Access 2000数据库中person表

29、的xm字段。由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access 2000:public class Form1 : Formprivate ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private System.ComponentModel.Container components = null ;public Form1 ( )file:/打开数据链接,得到数据集GetConnect ( ) ;Initializ

30、eComponent ( ) ;file:/清除程序中使用过的资源protected override void Dispose ( bool disposing )if ( disposing )if ( components != null ) components.Dispose ( ) ;base.Dispose ( disposing ) ;private void GetConnect ( )file:/创建一个 OleDbConnectionstring strCon = Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.

31、mdb ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;string strCom = SELECT * FROM person ;file:/创建一个 DataSetmyDataSet = new DataSet ( ) ;myConn.Open ( ) ;file:/用 OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file:/把Dataset绑定person数据表myComman

32、d.Fill ( myDataSet , person ) ;file:/关闭此OleDbConnectionmyConn.Close ( ) ;private void button1_Click ( object sender , System.EventArgs e )ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = person.xm ;ComboBox1.ValueMember = person.xm ;static void Main ( ) Application.Run ( new Form1 ( ) ) ;

33、得到了ComboBox组件对本地数据库的数据绑定程序,也就十分方便的得到ComboBox组件绑定Sql Server 2000源程序代码(Combox02.cs)具体如下:public class Form1 : Formprivate ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private System.ComponentModel.Container components = null ;public Form1 ( )file:/打开数据链接,得到数据集GetCo

34、nnect ( ) ;InitializeComponent ( ) ;file:/清除程序中使用过的资源protected override void Dispose ( bool disposing )if ( disposing )if ( components != null ) components.Dispose ( ) ;base.Dispose ( disposing ) ;private void GetConnect ( )/ 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1string strCon = P

35、rovider = 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 person ;file:/创建一个 DataSetmyDataSet = new DataSet ( ) ;file:/用 OleDbDataAdapt

36、er 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file:/把Dataset绑定person数据表myCommand.Fill ( myDataSet , person ) ;file:/关闭此OleDbConnectionmyConn.Close ( ) ;private void button1_Click ( object sender , System.EventArgs e )ComboBox1.DataSource = myDataSet ;ComboBox1.Disp

37、layMember = person.xm ;ComboBox1.ValueMember = person.xm ;static void Main ( ) Application.Run ( new Form1 ( ) ) ;(2)ListBox组件的数据绑定: ListBox组件的数据绑定和ComboBox组件的数据绑定的方法大致相同,也是通过设定DisplayMember、 ValueMember。其中DataSource这三个属性来完成的。并且这三个属性在ListBox组件中代表的意思和ComboBox组件 的意思基本一样。由此可以得到ListBox组件对本地数据库和远程数据库进行数据

38、绑定的源程序。其中ListBox01.cs是对本地数据库进行数据绑 定,ListBox02.cs是对远程数据库进行数据绑定,具体如下:ListBox01.cs源程序代码节选:public class Form1 : Formprivate ListBox ListBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private System.ComponentModel.Container components = null ;public Form1 ( )file:/打开数据链接,得到数据集GetCon

39、nect ( ) ;InitializeComponent ( ) ;file:/清除程序中使用过的资源protected override void Dispose ( bool disposing )if ( disposing )if ( components != null ) components.Dispose ( ) ;base.Dispose ( disposing ) ;private void GetConnect ( )file:/创建一个 OleDbConnectionstring strCon = Provider = Microsoft.Jet.OLEDB.4.0

40、; Data Source = db.mdb ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;string strCom = SELECT * FROM person ;file:/创建一个 DataSetmyDataSet = new DataSet ( ) ;myConn.Open ( ) ;file:/用 OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file:/把Dataset

41、绑定person数据表myCommand.Fill ( myDataSet , person ) ;file:/关闭此OleDbConnectionmyConn.Close ( ) ;private void button1_Click ( object sender , System.EventArgs e )ListBox1.DataSource = myDataSet ;ListBox1.DisplayMember = person.xm ;ListBox1.ValueMember = person.xm ;static void Main ( ) Application.Run ( n

42、ew Form1 ( ) ) ;以下代码是ListBox组件对Sql Server 2000数据库进行数据绑定的源程序节选(ListBox02.cs):private ListBox ListBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private System.ComponentModel.Container components = null ; public Form1 ( )file:/打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;

43、file:/清除程序中使用过的资源protected override void Dispose ( bool disposing )if ( disposing )if ( components != null ) components.Dispose ( ) ;base.Dispose ( disposing ) ;private void GetConnect ( )/ 设定数据连接字符串,此字符串的意思是打开Sql server数据库,服务器名称为server1,数据库为data1string strCon = Provider = SQLOLEDB.1 ; Persist Secur

44、ity Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;myConn.Open ( ) ;string strCom = SELECT * FROM person ;file:/创建一个 DataSetmyDataSet = new DataSet ( ) ;file:/用 OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand

45、 = new OleDbDataAdapter ( strCom , myConn ) ;file:/把Dataset绑定person数据表myCommand.Fill ( myDataSet , person ) ;file:/关闭此OleDbConnectionmyConn.Close ( ) ;private void button1_Click ( object sender , System.EventArgs e )ListBox1.DataSource = myDataSet ;ListBox1.DisplayMember = person.xm ;ListBox1.ValueMember = person.xm ;static void Main ( ) Application.Run ( new Form1 (

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论