1、1Visual 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
5、“,数据库名称为“Data1“,登陆的 ID为“sa“,口令为空,在数据库也定义了一张“person“表,数据结构如上表。三、数据绑定一般步骤(一)无论是简单型的数据绑定,还是复杂型的数据绑定,要实现绑定的第一步就是就是要连接数据库,得到可以操作的 DataSet。下面二段代码是分别连接 Access 2000 和 Sql Server 2000 数据库,并获得DataSet。 (下载源码就到源码网 :)(1)连接 Access 2000,得到 DataSet:/创建一个 OleDbConnection string strCon = “ Provider = Microsoft.Jet.OL
6、EDB.4.0 ; Data Source = db.mdb“ ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = “ SELECT * FROM person “ ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; 2myConn.Open ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myC
7、onn ) ; file:/把 Dataset 绑定 person 数据表 myCommand.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 I
8、D = sa ; Initial Catalog = data1 ; Data Source = server1 “ ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strCom = “ SELECT * FROM person “ ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDb
9、DataAdapter ( strCom , myConn ) ; file:/把 Dataset 绑定 person 数据表 myCommand.Fill ( myDataSet , “ person “ ) ; file:/关闭此 OleDbConnection myConn.Close ( ) ;(二)根据不同组件,采用不同的数据绑定: 对于简单型的数据绑定,数据绑定的方法其实比较简单,在得到数据集以后,一般是通过把数据集中 的某个字段绑定到组件的显示属性上面,譬如 TextBox 组件和 Label 组件,是绑定到“Text“属性。对于复杂型的数据绑定一般是通过设定其某些属 性值来实现
10、绑定的。这些下面将会具体介绍。四、简单型组件的数据绑定: (1)TextBox 组件的数据绑定: 通过下列语句就可以把数据集(即为:myDataSet)的某个字段绑定到 TextBox 组件的“Text”属性上面了:textBox1.DataBindings.Add ( “Text“ , myDataSet , “person.xm“ ) ;注释:此时绑定是 Access 2000 数据库中“person“表的“xm“字段。 由此可以得到绑定 TextBox 组件的源程序代码(TextBox01.cs),下列代码操作的数据库是 Access 2000,如下:public class Form1
11、 : Form private TextBox textBox1 ; private Button button1 ; private System.Data.DataSet myDataSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; 3InitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override void Dispose ( bool dis
12、posing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( disposing ) ; private void GetConnect ( ) file:/创建一个 OleDbConnection string strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb“ ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; st
13、ring strCom = “ SELECT * FROM person “ ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file:/把 Dataset 绑定 person 数据表 myCommand.Fill ( myDataSet , “person“ ) ; file:/关闭此 OleDb
14、Connection myConn.Close ( ) ; private void button1_Click ( object sender , System.EventArgs e ) textBox1.DataBindings.Add ( “Text“ , myDataSet , “person.xm“ ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; 得到 TextBox 组件对本地数据库中的字段进行数据绑定的程序后,可以方便的得到对远程数据库中的某些字段进行数据绑定的源程序代码(TextBox02.cs),具体
15、如下:public class Form1 : Form private TextBox textBox1 ; private Button button1 ; private System.Data.DataSet myDataSet ; 5private System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; InitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override vo
16、id Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( disposing ) ; private void GetConnect ( ) / 设定数据连接字符串,此字符串的意思是打开 Sql server 数据库,服务器名称为 server1,数据库为 data1 string strCon = “Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID
17、= sa ; Initial Catalog = data1 ; Data Source = server1 “ ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strCom = “ SELECT * FROM person “ ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; file:/用 OleDbDataAdapter 得到一个数据集 OleDbDataAdapter myCommand = new OleDbDa
18、taAdapter ( strCom , myConn ) ; file:/把 Dataset 绑定 person 数据表 myCommand.Fill ( myDataSet , “ person “ ) ; file:/关闭此 OleDbConnection myConn.Close ( ) ; private void button1_Click ( object sender , System.EventArgs e ) textBox1.DataBindings.Add ( “Text“ , myDataSet , “person.xm“ ) ; static void Main (
19、 ) Application.Run ( new Form1 ( ) ) ; 6(2)Label 组件的数据绑定: 在掌握了 TextBox 组件数据绑定以后,可以十分方便的得到 Label 组件的数据绑定方法,因为这二者实现的方法实在是太相似了。下列语句是把得到数据集的“xm“字段绑定到 Label 组件的“Text”属性上:label1.DataBindings.Add ( “Text“ , myDataSet , “person.xm“ ) ;注释:此时绑定是 Access 2000 数据库中“person“表的“xm“字段。由此可以得到 Label 组件数据绑定的源程序代码(Label
20、01.cs),本代码操作数据库是 Access 2000:public class Form1 : Form private Label label1 ; private Button button1 ; private System.Data.DataSet myDataSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; InitializeComponent ( ) ; file:/清除程序中使用过的资源
21、protected override void Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( disposing ) ; private void GetConnect ( ) file:/创建一个 OleDbConnection string strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb“ ; OleDbConnection my
22、Conn = 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.Fill
23、( myDataSet , “person“ ) ; file:/关闭此 OleDbConnection myConn.Close ( ) ; 7private void button1_Click ( object sender , System.EventArgs e ) label1.DataBindings.Add ( “Text“ , myDataSet , “person.xm“ ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; 得到了 Label 组件对 Access 2000 数据库数据绑定的程序代码,改换
24、一下程序中数据链接,就可以得到Label 组件对 Sql Server 2000 数据库数据绑定的源程序代码(Label02.cs),具体如下:public class Form1 : Form private Label label1 ; private Button button1 ; private System.Data.DataSet myDataSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) file:/打开数据链接,得到数据集 GetConnect ( ) ; I
25、nitializeComponent ( ) ; file:/清除程序中使用过的资源 protected override void Dispose ( bool disposing ) if ( disposing ) if ( components != null ) components.Dispose ( ) ; base.Dispose ( disposing ) ; private void GetConnect ( ) / 设定数据连接字符串,此字符串的意思是打开 Sql server 数据库,服务器名称为 server1,数据库为 data1 string strCon = “
26、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 person “ ; file:/创建一个 DataSet myDataSet = new DataSet ( ) ; 8file:/用
27、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 ) label1.DataBinding
28、s.Add ( “Text“ , myDataSet , “person.xm“ ) ; static void Main ( ) Application.Run ( new Form1 ( ) ) ; 五、复杂型组件的数据绑定:在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。首先来介绍一下 ComboBox 组件的数据绑定。(下载源码就到源码网:)(1).ComboBox 组件的数据绑定:在得到数据集后,只有设定好 ComboBox 组件的的三个属性就可以完成数据绑定了,这三个属性是:“DisplayMember“、 “ValueMember“。其中“D
29、ataSource“是要显示的数据集,“DisplayMember“是ComboBox 组件显示的字段, “ValueMember“是实际使用值。具体如下:ComboBox1.DataSource = myDataSet ;ComboBox1.DisplayMember = “person.xm“ ;ComboBox1.ValueMember = “person.xm“ ;注释:此时绑定是 Access 2000 数据库中“person“表的“xm“字段。由此可以得到 ComboBox 组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是 Access 2000:public
30、class Form1 : Formprivate ComboBox ComboBox1 ;private Button button1 ;private System.Data.DataSet myDataSet ;private System.ComponentModel.Container components = null ;public Form1 ( )file:/打开数据链接,得到数据集GetConnect ( ) ;InitializeComponent ( ) ;file:/清除程序中使用过的资源protected override void Dispose ( bool d
31、isposing )if ( disposing )9if ( components != null ) components.Dispose ( ) ;base.Dispose ( disposing ) ;private void GetConnect ( )file:/创建一个 OleDbConnectionstring strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb“ ;OleDbConnection myConn = new OleDbConnection ( strCon ) ;string
32、strCom = “ SELECT * FROM person “ ;file:/创建一个 DataSetmyDataSet = new DataSet ( ) ;myConn.Open ( ) ;file:/用 OleDbDataAdapter 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file:/把 Dataset 绑定 person 数据表myCommand.Fill ( myDataSet , “person“ ) ;file:/关闭此 OleDbConnectionmyC
33、onn.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 ( ) ) ;得到了 ComboBox 组件对本地数据库的数据绑定程序,也就十分方便的得到 ComboBox 组件绑定 S
34、ql 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:/打开数据链接,得到数据集GetConnect ( ) ;10InitializeComponent ( ) ;file:/清除程序
35、中使用过的资源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 Security I
36、nfo = 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 myComman
37、d = 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.DisplayMember = “person.xm“ ;C
38、omboBox1.ValueMember = “person.xm“ ;static void Main ( ) Application.Run ( new Form1 ( ) ) ;(2)ListBox 组件的数据绑定: ListBox 组件的数据绑定和 ComboBox 组件的数据绑定的方法大致相同,也是通过设定“DisplayMember“、 “ValueMember“。其中“DataSource“这三个属性来完成的。并且这三个属性在 ListBox 组件中代表的意思和 ComboBox 组件 的意思基本一样。由此可以得到 ListBox 组件对本地数据库和远程数据库进行数据绑11定的源
39、程序。其中 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
40、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.
41、0 ; 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:/把
42、 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“ ;12ListBox1.ValueMember = “person.xm“ ;static void Main ( )
43、 Application.Run ( new 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 ( ) ;I
44、nitializeComponent ( ) ;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
45、 = 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:/用 OleDbDataAdapte
46、r 得到一个数据集OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;file:/把 Dataset 绑定 person 数据表myCommand.Fill ( myDataSet , “ person “ ) ;13file:/关闭此 OleDbConnectionmyConn.Close ( ) ;private void button1_Click ( object sender , System.EventArgs e )ListBox1.DataSource = myDataSet ;ListB
47、ox1.DisplayMember = “person.xm“ ;ListBox1.ValueMember = “person.xm“ ;static void Main ( ) Application.Run ( new Form1 ( ) ) ;六、总结本文介绍的实现数据绑定组件的都是在程序设计中经常用到的 WinForm 组件。当然在.Net FrameWork SDK 中提供的 WinForm 组件是很多的,由于本文的限制,不可能一一介绍,一般来说,WinForm 组件都可以实现数据绑定,虽然在某些具体的方法上 有所差异,但也总是大同小异。在以下的文章中,将以此为基础探讨 Visual
48、 C中数据库编程。(下载源码就到源码网:)void Page_Load(Object sender, EventArgs e)/ This example uses Microsoft SQL Server and connects/ to the Northwind sample database. The data source needs/ to be bound to the GridView control only when the / page is first loaded. Thereafter, the values are/ stored in view state. if(!IsPostBack)/ Declare the query string.String queryString = “Select CustomerID, CompanyName, Address, City, PostalCode, Country From Customers“;/ Run the query and bind the resulting DataSet/ to the GridView control.DataSet ds = GetData(queryStr