C# 中通用winform元件的資料綁定
我們知道在由于Visual C#自身沒有類庫,和其他的.Net開發語言一樣,Visual C#調用的類庫是.Net架構中的一個共有的類庫--.Net FrameWork SDK。ADO.NET是.Net FrameWork SDK提供給.Net開發語言進行資料庫開發的一個系列類庫的集合。在ADO.NET中雖然提供了大量的用于資料庫連接配接、資料處理的類庫,但卻沒有提供類似DbText元件、DbList元件、DbLable元件、DbCombox元件等。要想把資料記錄以ComBox、ListBox等形式顯示處理,使用資料綁定技術是最為友善、最為直接的方法。所謂資料綁定技術就是把已經打開的資料集中某個或者某些字段綁定到元件的某些屬性上面的一種技術。說的具體些,就是把已經打開資料的某個或者某些字段綁定到Text元件、ListBox元件、ComBox等元件上的能夠顯示資料的屬性上面。當對元件完成資料綁定後,其顯示字段的内容将随着資料記錄指針的變化而變化。這樣程式員就可以定制資料顯示方式和内容,進而為以後的資料處理作好準備。是以說資料綁定是Visual C#進行資料庫方面程式設計的基礎和最為重要的第一步。隻有掌握了資料綁定方法,才可以十分友善對已經打開的資料集中的記錄進行浏覽、删除、插入等具體的資料操作、處理。
資料綁定根據不同元件可以分為二種,一種是簡單型的資料綁定,另外一種就是複雜型的資料綁定。所謂簡單型的資料綁定就是綁定後元件顯示出來的字段隻是單個記錄,這種綁定一般使用在顯示單個值的元件上,譬如:TextBox元件和Label元件。而複雜型的資料綁定就是綁定後的元件顯示出來的字段是多個記錄,這種綁定一般使用在顯示多個值的元件上,譬如:ComBox元件、ListBox元件等。本文就是來詳細介紹如何用Visual C#實作這二種綁定。在資料庫的選擇上,為了使内容更加全面,采用了當下比較流行的二種資料庫,一種是本地資料庫Acess 2000,另外一種是遠端資料庫Sql Server 2000。
一. 本文程式設計和運作的軟體環境:
(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",密碼為空,在資料庫也定義了一張"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" ; OleDbConnection 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.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 " ; 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資料表 myCommand.Fill ( myDataSet , " person " ) ; file://關閉此OleDbConnection myConn.Close ( ) ; |
(二).根據不同元件,采用不同的資料綁定:
對于簡單型的資料綁定,資料綁定的方法其實比較簡單,在得到資料集以後,一般是通過把資料集中的某個字段綁定到元件的顯示屬性上面,譬如TextBox元件和Label元件,是綁定到"Text"屬性。對于複雜型的資料綁定一般是通過設定其某些屬性值來實作綁定的。這些下面将會具體介紹。
四.簡單型元件的資料綁定:
(1).TextBox元件的資料綁定:
通過下列語句就可以把資料集(即為:myDataSet)的某個字段綁定到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 myDataSet ; 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 ( disposing ) ; } 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 ( ) ; 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.DataBindings.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 myDataSet ; 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 ( disposing ) ; } 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 ( ) ; 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 ( ) ; } 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"字段綁定到Label元件的"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.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 ) ; } 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 ( ) ; 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 ) { label1.DataBindings.Add ( "Text" , myDataSet , "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 ; 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 ( disposing ) ; } 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 ( ) ; 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 ( ) ; } 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).ComboBox元件的資料綁定:
在得到資料集後,隻有設定好ComboBox元件的的三個屬性就可以完成資料綁定了,這三個屬性是:、"DisplayMember"、"ValueMember"。其中"DataSource"是要顯示的資料集,"DisplayMember"是ComboBox元件顯示的字段,"ValueMember"是實際使用值。具體如下:
ComboBox1.DataSource = myDataSet ;
ComboBox1.DisplayMember = "person.xm" ;
ComboBox1.ValueMember = "person.xm" ;
注釋:此時綁定是Access 2000資料庫中"person"表的"xm"字段。由此可以得到ComboBox元件資料綁定的源程式代碼(Combo01.cs),本代碼操作資料庫是Access 2000:
public class Form1 : Form
{
private 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 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 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.Fill ( myDataSet , "person" ) ;
file://關閉此OleDbConnection
myConn.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 ( ) ) ;
}
}
圖03:對ComboBox元件資料綁定的程式界面
得到了ComboBox元件對本地資料庫的資料綁定程式,也就十分友善的得到ComboBox元件綁定Sql Server 2000源程式代碼(Combox02.cs)具體如下:
public class Form1 : Form
{
private 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 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 = 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 OleDbDataAdapter ( strCom , myConn ) ;
file://把Dataset綁定person資料表
myCommand.Fill ( myDataSet , " person " ) ;
file://關閉此OleDbConnection
myConn.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 ( ) ) ;
}
}
(2).ListBox元件的資料綁定:
ListBox元件的資料綁定和ComboBox元件的資料綁定的方法大緻相同,也是通過設定"DisplayMember"、"ValueMember"。其中"DataSource"這三個屬性來完成的。并且這三個屬性在ListBox元件中代表的意思和ComboBox元件的意思基本一樣。由此可以得到ListBox元件對本地資料庫和遠端資料庫進行資料綁定的源程式。其中ListBox01.cs是對本地資料庫進行資料綁定,ListBox02.cs是對遠端資料庫進行資料綁定,具體如下:
ListBox01.cs源程式代碼節選:
public class Form1 : Form
{
private ListBox ListBox1 ;
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 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 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.Fill ( myDataSet , "person" ) ;
file://關閉此OleDbConnection
myConn.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 ( ) ) ;
}
}
圖04:對ListBox元件資料綁定的程式界面
以下代碼是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 ( ) ;
}
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 = "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 ( ) ;
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 )
{
ListBox1.DataSource = myDataSet ;
ListBox1.DisplayMember = "person.xm" ;
ListBox1.ValueMember = "person.xm" ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}
六. 總結:
本文介紹的實作資料綁定元件的都是在程式設計中經常用到的WinForm元件。當然在.Net FrameWork SDK中提供的WinForm元件是很多的,由于本文的限制,不可能一一介紹,一般來說,WinForm元件都可以實作資料綁定,雖然在某些具體的方法上有所差異,但也總是大同小異。在以下的文章中,将以此為基礎探讨Visual C#中資料庫程式設計。