Visual C#中的数据绑定之三(转)

本文介绍了如何使用C#实现TextBox组件的数据绑定,包括本地Access2000数据库和远程SQL Server数据库的绑定方法。提供了完整的示例代码,帮助读者快速掌握数据绑定技巧。
四.简单型组件的数据绑定:

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

[@more@]Visual C#中的数据绑定之二(转)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924576/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924576/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值