您现在的位置是: 软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > C#专区 > 正文


-windows xp 中的cmd
-AI插图之梦:纸面绘画到数码图像
-Win xp中的多种网络
-试验试验试验试验

Visual C#中的数据绑定
2002-02-26· ·阿虎··yesky

上一页  1 2 3 4 5 6  下一页

(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 ( ) ) ;
}
}



图02:对Label组件数据绑定后的程序界面

  得到了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 ( ) ) ;
}
}

上一页  1 2 3 4 5 6  下一页

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 C#锐利体验
 Visual C#如何使用Active X组件
 C#的多线程机制初探
 C#:消息队列应用程序
 用Visual C#发送电子邮件
 C#实时申请技术
 用C#写计算器程序
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!