(2). 连接Sql Server 2000,得到DataSet:
(二).根据不同组件,采用不同的数据绑定:
对于简单型的数据绑定,数据绑定的方法其实比较简单,在得到数据集以后,一般是通过把数据集中的某个字段绑定到组件的显示属性上面,譬如TextBox组件和Label组件,是绑定到"Text"属性。对于复杂型的数据绑定一般是通过设定其某些属性值来实现绑定的。这些下面将会具体介绍。
注释:此时绑定是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"属性上:
注释:此时绑定是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"是实际使用值。具体如下:
注释:此时绑定是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 ( ) ) ;
}
}
得到了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 ( ) ) ;
}
}
以下代码是ListBox组件对Sql Server 2000数据库进行数据绑定的源程序节选(ListBox02.cs):
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#中数据库编程。