Visual2BC23数据绑定技术.docx
- 文档编号:26361711
- 上传时间:2023-06-18
- 格式:DOCX
- 页数:18
- 大小:19.86KB
Visual2BC23数据绑定技术.docx
《Visual2BC23数据绑定技术.docx》由会员分享,可在线阅读,更多相关《Visual2BC23数据绑定技术.docx(18页珍藏版)》请在冰豆网上搜索。
Visual2BC23数据绑定技术
VisualC#中的数据绑定
VisualC#自身没有类库,和其他的.Net开发语言一样,VisualC#调用的类库是.Net框架中的一个共有的类库--.NetFrameWorkSDK。
ADO.NET是.NetFrameWorkSDK提供给.Net开发语言进行数据库开发的一个系列类库的集合。
在ADO.NET中虽然提供了大量的用于数据库连接、数据处理的类库,但却没有提供类似DbText组件、DbList组件、DbLable组件、DbCombox组件等。
要想把数据记录以ComBox、ListBox等形式显示处理,使用数据绑定技术是最为方便、最为直接的方法。
所谓数据绑定技术就是把已经打开的数据集中某个或者某些字段绑定到组件的某些属性上面的一种技术。
说的具体些,就是把已经打开数据的某个或者某些字段绑定到Text组件、ListBox组件、ComBox等组件上的能够显示数据的属性上面。
当对组件完成数据绑定后,其显示字段的内容将随着数据记录指针的变化而变化。
这样程序员就可以定制数据显示方式和内容,从而为以后的数据处理作好准备。
所以说数据绑定是VisualC#进行数据库方面编程的基础和最为重要的第一步。
只有掌握了数据绑定方法,才可以十分方便对已经打开的数据集中的记录进行浏览、删除、插入等具体的数据操作、处理。
数据绑定根据不同组件可以分为二种,一种是简单型的数据绑定,另外一种就是复杂型的数据绑定。
所谓简单型的数据绑定就是绑定后组件显示出来的字段只是单个记录,这种绑定一般使用在显示单个值的组件上,譬如:
TextBox组件和Label组件。
而复杂型的数据绑定就是绑定后的组件显示出来的字段是多个记录,这种绑定一般使用在显示多个值的组件上,譬如:
ComBox组件、ListBox组件等。
本文就是来详细介绍如何用VisualC#实现这二种绑定。
在数据库的选择上,为了使内容更加全面,采用了当下比较流行的二种数据库,一种是本地数据库Acess2000,另外一种是远程数据库SqlServer2000。
一、本文程序设计和运行的软件环境
(1)微软公司视窗2000服务器版
(2).NetFrameWorkSDKBeta2
(3)MADC2.6(MicrosoftAcessDataComponent)以上版本
二、程序中使用的数据库的数据字典
(1)本地数据库Access2000的数据库的名称为"db.mdb",在这个数据库中定义了一张表"person"。
这张表的数据结构如下表:
字段名称
字段类型
字段意思
id
数字
序号
xm
文本
姓名
xb
文本
性别
nl
文本
年龄
zip
文本
邮政编码
(2)远程数据库SqlServer2000的数据库服务器名称为"Server1",数据库名称为"Data1",登陆的ID为"sa",口令为空,在数据库也定义了一张"person"表,数据结构如上表。
三、数据绑定一般步骤
(一)无论是简单型的数据绑定,还是复杂型的数据绑定,要实现绑定的第一步就是就是要连接数据库,得到可以操作的DataSet。
下面二段代码是分别连接Access2000和SqlServer2000数据库,并获得DataSet。
(1)连接Access2000,得到DataSet:
//创建一个OleDbConnection
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
stringstrCom="SELECT*FROMperson";
file:
//创建一个DataSet
myDataSet=newDataSet();
myConn.Open();
file:
//用OleDbDataAdapter得到一个数据集
OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);
file:
//把Dataset绑定person数据表
myCommand.Fill(myDataSet,"person");
file:
//关闭此OleDbConnection
myConn.Close();
(2)连接SqlServer2000,得到DataSet:
//设定数据连接字符串,此字符串的意思是打开Sqlserver数据库,
服务器名称为server1,数据库为data1
stringstrCon="Provider=SQLOLEDB.1;PersistSecurityInfo=False;
UserID=sa;InitialCatalog=data1;DataSource=server1";
OleDbConnectionmyConn=newOleDbConnection(strCon);
myConn.Open();
stringstrCom="SELECT*FROMperson";
file:
//创建一个DataSet
myDataSet=newDataSet();
file:
//用OleDbDataAdapter得到一个数据集
OleDbDataAdaptermyCommand=newOleDbDataAdapter(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");
注释:
此时绑定是Access2000数据库中"person"表的"xm"字段。
由此可以得到绑定TextBox组件的源程序代码(TextBox01.cs),下列代码操作的数据库是Access2000,如下:
publicclassForm1:
Form
{
privateTextBoxtextBox1;
privateButtonbutton1;
privateSystem.Data.DataSetmyDataSet;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
file:
//打开数据链接,得到数据集
GetConnect();
InitializeComponent();
}
file:
//清除程序中使用过的资源
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
privatevoidGetConnect()
{
file:
//创建一个OleDbConnection
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
stringstrCom="SELECT*FROMperson";
file:
//创建一个DataSet
myDataSet=newDataSet();
myConn.Open();
file:
//用OleDbDataAdapter得到一个数据集
OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);
file:
//把Dataset绑定person数据表
myCommand.Fill(myDataSet,"person");
file:
//关闭此OleDbConnection
myConn.Close();}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
textBox1.DataBindings.Add("Text",myDataSet,"person.xm");
}
staticvoidMain()
{
Application.Run(newForm1());
}
}
得到TextBox组件对本地数据库中的字段进行数据绑定的程序后,可以方便的得到对远程数据库中的某些字段进行数据绑定的源程序代码(TextBox02.cs),具体如下:
publicclassForm1:
Form
{
privateTextBoxtextBox1;
privateButtonbutton1;
privateSystem.Data.DataSetmyDataSet;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
file:
//打开数据链接,得到数据集
GetConnect();
InitializeComponent();
}
file:
//清除程序中使用过的资源
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
privatevoidGetConnect()
{
//设定数据连接字符串,此字符串的意思是打开Sqlserver数据库,
服务器名称为server1,数据库为data1
stringstrCon="Provider=SQLOLEDB.1;PersistSecurityInfo=False;
UserID=sa;InitialCatalog=data1;DataSource=server1";
OleDbConnectionmyConn=newOleDbConnection(strCon);
myConn.Open();
stringstrCom="SELECT*FROMperson";
file:
//创建一个DataSet
myDataSet=newDataSet();
file:
//用OleDbDataAdapter得到一个数据集
OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);
file:
//把Dataset绑定person数据表
myCommand.Fill(myDataSet,"person");
file:
//关闭此OleDbConnection
myConn.Close();
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
textBox1.DataBindings.Add("Text",myDataSet,"person.xm");
}
staticvoidMain()
{
Application.Run(newForm1());
}
}
(2)Label组件的数据绑定:
在掌握了TextBox组件数据绑定以后,可以十分方便的得到Label组件的数据绑定方法,因为这二者实现的方法实在是太相似了。
下列语句是把得到数据集的"xm"字段绑定到Label组件的“Text”属性上:
label1.DataBindings.Add("Text",myDataSet,"person.xm");
注释:
此时绑定是Access2000数据库中"person"表的"xm"字段。
由此可以得到Label组件数据绑定的源程序代码(Label01.cs),本代码操作数据库是Access2000:
publicclassForm1:
Form
{
privateLabellabel1;
privateButtonbutton1;
privateSystem.Data.DataSetmyDataSet;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
file:
//打开数据链接,得到数据集
GetConnect();
InitializeComponent();
}
file:
//清除程序中使用过的资源
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
privatevoidGetConnect()
{
file:
//创建一个OleDbConnection
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
stringstrCom="SELECT*FROMperson";
file:
//创建一个DataSet
myDataSet=newDataSet();
myConn.Open();
file:
//用OleDbDataAdapter得到一个数据集
OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);
file:
//把Dataset绑定person数据表
myCommand.Fill(myDataSet,"person");
file:
//关闭此OleDbConnection
myConn.Close();}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
label1.DataBindings.Add("Text",myDataSet,"person.xm");
}
staticvoidMain()
{
Application.Run(newForm1());
}}
得到了Label组件对Access2000数据库数据绑定的程序代码,改换一下程序中数据链接,就可以得到Label组件对SqlServer2000数据库数据绑定的源程序代码(Label02.cs),具体如下:
publicclassForm1:
Form
{
privateLabellabel1;
privateButtonbutton1;
privateSystem.Data.DataSetmyDataSet;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
file:
//打开数据链接,得到数据集
GetConnect();
InitializeComponent();
}
file:
//清除程序中使用过的资源
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
privatevoidGetConnect()
{
//设定数据连接字符串,此字符串的意思是打开Sqlserver数据库,
服务器名称为server1,数据库为data1
stringstrCon="Provider=SQLOLEDB.1;PersistSecurityInfo=False;
UserID=sa;InitialCatalog=data1;DataSource=server1";
OleDbConnectionmyConn=newOleDbConnection(strCon);
myConn.Open();
stringstrCom="SELECT*FROMperson";
file:
//创建一个DataSet
myDataSet=newDataSet();
file:
//用OleDbDataAdapter得到一个数据集
OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);
file:
//把Dataset绑定person数据表
myCommand.Fill(myDataSet,"person");
file:
//关闭此OleDbConnection
myConn.Close();
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
label1.DataBindings.Add("Text",myDataSet,"person.xm");
}
staticvoidMain()
{
Application.Run(newForm1());
}
}
五、复杂型组件的数据绑定:
在上面的介绍中,了解到对复杂型组件的数据绑定是通过设定组件的某些属性来完成数据绑定的。
首先来介绍一下ComboBox组件的数据绑定。
(1).ComboBox组件的数据绑定:
在得到数据集后,只有设定好ComboBox组件的的三个属性就可以完成数据绑定了,这三个属性是:
"DisplayMember"、"ValueMember"。
其中"DataSource"是要显示的数据集,"DisplayMember"是ComboBox组件显示的字段,"ValueMember"是实际使用值。
具体如下:
ComboBox1.DataSource=myDataSet;
ComboBox1.DisplayMember="person.xm";
ComboBox1.ValueMember="person.xm";
注释:
此时绑定是Access2000数据库中"person"表的"xm"字段。
由此可以得到ComboBox组件数据绑定的源程序代码(Combo01.cs),本代码操作数据库是Access2000:
publicclassForm1:
Form
{
privateComboBoxComboBox1;
privateButtonbutton1;
privateSystem.Data.DataSetmyDataSet;
privateSystem.ComponentModel.Containercomponents=null;
publicForm1()
{
file:
//打开数据链接,得到数据集
GetConnect();
InitializeComponent();
}
file:
//清除程序中使用过的资源
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
privatevoidGetConnect()
{
file:
//创建一个OleDbConnection
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=db.mdb";
OleDbConnectionmyConn=newOleDbConnection(strCon);
stringstrCom="SELECT*FROMperson";
file:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Visual2BC23 数据 绑定 技术