数据库数据绑定到Text组件、ListBox组件、ComBox等组件上去

 

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 " ;

//创建一个 DataSet

DataSet myDataSet = new DataSet ( ) ;

myConn.Open ( ) ;

//用 OleDbDataAdapter 得到一个数据集

OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

//把Dataset绑定person数据表

myCommand.Fill ( myDataSet , "person" ) ;

//关闭此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 " ;

//创建一个 DataSet

DataSet myDataSet = new DataSet ( ) ;

//用 OleDbDataAdapter 得到一个数据集

OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

//把Dataset绑定person数据表

myCommand.Fill ( myDataSet , " person " ) ;

//关闭此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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    protected override void Dispose ( bool disposing )

    {

        if ( disposing )

        {

            if ( components != null )

            {

                components.Dispose ( ) ;

            }

        }

        base.Dispose ( disposing ) ;

    }

    private void GetConnect ( )

    {

        //创建一个 OleDbConnection

        string strCon =

            " Provider = microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        string strCom = " SELECT * FROM person " ;

        //创建一个 DataSet

        DataSet myDataSet = new DataSet ( ) ;

        myConn.Open ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , "person" ) ;

        //关闭此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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    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 " ;

        //创建一个 DataSet myDataSet = new DataSet ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , " person " ) ;

        //关闭此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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    protected override void Dispose ( bool disposing )

    {

        if ( disposing )

        {

            if ( components != null )

            {

                components.Dispose ( ) ;

            }

        }

        base.Dispose ( disposing ) ;

    }

    private void GetConnect ( )

    {

        //创建一个 OleDbConnection

        string strCon = " Provider = microsoft.Jet.OLEDB.4.0 ;

        Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        string strCom = " SELECT * FROM person " ;

        //创建一个 DataSet myDataSet = new DataSet ( ) ;

        myConn.Open ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , "person" ) ;

        //关闭此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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    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 " ;

        //创建一个

        DataSet myDataSet = new DataSet ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , " person " ) ;

        //关闭此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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    protected override void Dispose ( bool disposing )

    {

        if ( disposing )

        {

            if ( components != null )

            {

                components.Dispose ( ) ;

            }

        }

        base.Dispose ( disposing ) ;

    }

    private void GetConnect ( )

    {

        //创建一个 OleDbConnectionstring

        strCon = " Provider = microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;s

        tring strCom = " SELECT * FROM person " ;

        //创建一个

        DataSetmyDataSet = new DataSet ( ) ;

        myConn.Open ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , "person" ) ;

        //关闭此OleDbConnectionmyConn.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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    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 " ;

        //创建一个 DataSetmyDataSet = new DataSet ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , " person " ) ;

        //关闭此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 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    protected override void Dispose ( bool disposing )

    {

        if ( disposing )

        {

            if ( components != null )

            {

                components.Dispose ( ) ;

            }

        }

        base.Dispose ( disposing ) ;

    }

    private void GetConnect ( )

    {

        //创建一个 OleDbConnection

        string strCon = " Provider = microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ;

        OleDbConnection myConn = new OleDbConnection ( strCon ) ;

        string strCom = " SELECT * FROM person " ;

        //创建一个

        DataSet myDataSet = new DataSet ( ) ;

        myConn.Open ( ) ;

        //用 OleDbDataAdapter 得到一个数据集

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        //把Dataset绑定person数据表

        myCommand.Fill ( myDataSet , "person" ) ;

        //关闭此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 class Form1 : Form

{

    private ListBox ListBox1 ;

    private Button button1 ;

    private system.Data.DataSet myDataSet ;

    private system.ComponentModel.Container components = null ;

    public Form1 ( )

    {

        //打开数据链接,得到数据集

        GetConnect ( ) ;

        InitializeComponent ( ) ;

    }

    //清除程序中使用过的资源

    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 " ;

         DataSetmyDataSet = new DataSet ( ) ;

        OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;

        myCommand.Fill ( myDataSet , " person " ) ;

        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#中数据库编程。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值