c#之ado.net数据库绑定

   在visual c#中ADO.NET 2.0中的新数据绑定技术介绍。

将bindingSource1绑定到数据,将TextBox控件绑定到bindingSource1。若要执行此操作,可将下面的代码粘贴到窗体中,并从窗体的构造函数调用LoadData或调用 Load事件处理方法。


  
  
private void LoadData()   {       //设置XML       string xml = @"<US><states>"          + @"<state><name>Washington</name><capital> Olympia</capital></state>"          + @"<state><name>Oregon</name><capital>Salem </capital></state>"          + @"<state><name>California</name><capital> Sacramento</capital>   </state>"           + @"<state><name>Nevada</name><capital>Carson  City</capital></state>"          + @"</states></US>";         //将XML数据从String类型转换为Bytes类型并加载到流中       byte[] xmlBytes = Encoding.UTF8.GetBytes(xml);       MemoryStream stream = new MemoryStream(xmlBytes, false);         // Create a DataSet and load the xml into it.       DataSet set = new DataSet();       set.ReadXml(stream);         // 设置DataSet的 DataSourc和DataMember属性       bindingSource1.DataSource = set;       bindingSource1.DataMember = "state";         textBox1.DataBindings.Add("Text", bindingSource1, "name");       textBox2.DataBindings.Add("Text", bindingSource1, "capital");  

 将名为bindingNavigator1的BindingNavigator控件添加到窗体。

 将bindingNavigator1的BindingSource属性设置为bindingSource1。

(2)使用Windows窗体BindingNavigator控件浏览数据集

生成数据驱动的应用程序时,经常需要向用户显示数据集合。BindingNavigator控件与BindingSource组件一起为滚动集合并按顺序显示其中的项提供方便的可扩展解决方案。

实例  使用 BindingNavigator。

下面演示如何使用BindingNavigator控件滚动数据库查询结果。结果集包含在DataSet中,它使用BindingSource组件绑定到TextBox控件。


  
  
using System;   using System.Collections.Generic;   using System.ComponentModel;   using System.Data;   using System.Drawing;   using System.Data.SqlClient;   using System.Windows.Forms;     public class Form1 : Form   {       //创建一个BindingNavigator对象       BindingNavigator customersBindingNavigator = new BindingNavigator();         //创建一个BindingSource 对象,以提供绑定数据       BindingSource customersBindingSource = new BindingSource();         //创建一个TextBox用来显示CompanyName的数据       TextBox companyNameTextBox = new TextBox();         public Form1()       {           //将BindingSource绑定到BindingNavigator上           this.customersBindingNavigator.BindingSource =   this.customersBindingSource;           this.customersBindingNavigator.Dock = DockStyle.Top;           this.Controls.Add(this.customersBindingNavigator);             //设置TextBox控件显示CompanyName           this.companyNameTextBox.Dock = DockStyle.Bottom;           this.Controls.Add(this.companyNameTextBox);             //设置窗口的大小           this.Size = new Size(800, 200);           this.Load += new EventHandler(Form1_Load);       }         void Form1_Load(object sender, EventArgs e)       {           //打开数据库连接           //Replace the value of connectString with a valid           //设置连接字符串连接到Northwind数据库           string connectString =               "Integrated Security=SSPI;Persist Security Info=False;" +               "Initial Catalog=Northwind;Data Source=localhost";           SqlConnection connection = new SqlConnection();           connection.ConnectionString = connectString;           connection.Open();             //执行查询命令           SqlCommand command = new SqlCommand(               "Select * From Customers", connection);           SqlDataReader reader = command.ExecuteReader(               CommandBehavior.CloseConnection);             //向DataSet加载查询结果           DataSet ds = new DataSet("Northwind Customers");           ds.Load(               reader,               LoadOption.OverwriteChanges,               new string[] { "Customers" });             //将DataSet绑定到BindingSource           this.customersBindingSource.DataSource = ds;             //将CompanyName字段绑定到TextBox控件           this.companyNameTextBox.DataBindings.Add(               new Binding("Text",               this.customersBindingSource,               "CompanyName",               true));       }  

3.DataGridView 控件

在ADO.NET 2.0中DataGridView控件取代了DataGrid控件。DataGridView控件提供了DataGrid控件中没有的许多基本功能和高级功能。此外,DataGridView控件的结构使得它比DataGrid控件更容易扩展和自定义。表10-4列出了DataGridView控件相对于DataGrid控件新增的几个主要功能。

表10-4  DataGridView的主要功能

DataGridView控件功能

    

多种列类型

DataGrid控件相比,DataGridView控件提供了

更多的内置列类型。这些列类型能满足大多数常

见方案的需要,而且比DataGrid控件中的列类型

更容易扩展或替换。有关更多信息,可参见

Windows窗体DataGridView控件中的列类型

续表

DataGridView控件功能

    

多种数据显示方式

DataGrid控件仅限于显示外部数据源的数据;

DataGridView控件可显示存储在控件中的未绑

定数据、来自绑定数据源的数据或者同时显示绑定

数据和未绑定数据,也可以在DataGridView控件中

实现虚拟模式以提供自定义数据管理。有关更多信息,

可参见Windows窗体DataGridView控件中的数据显示模式

用于自定义数据显示的多种方式

DataGridView控件提供了许多属性和事件,可以使用

它们指定数据的格式设置方式和显示方式。例如,

可以根据单元格、行和列中包含的数据更改其外观,

或者将一种数据类型的数据替换为另一种类型的等效

数据。有关更多信息,可参见Windows窗体

DataGridView控件中的数据格式设置

用于更改单元格、行、列、

标头外观和行为的多个选项

利用DataGridView控件能够以多种方式使用各个

网格组件。例如,可以冻结行和列以阻止其滚动;

隐藏行、列和表头;更改调整行、列和表头大小

的方式;更改用户进行选择的方式;以及

为各个单元格、行和列提供工具提示和快捷菜单

不过,在ADO.NET 2.0中仍然保留了DataGrid控件,以备向后兼容和特殊需要,但几乎所有目的都应使用DataGridView控件来实现。

【提示】DataGridView不能显示主/从两个表。

DataGrid控件中提供而DataGridView控件中未提供的唯一功能是在一个控件中分层显示两个相关表中的信息。必须使用两个DataGridView控件显示具有主/从(详细信息)关系的两个表中的信息。

ADO.NET是一种将基于Microsoft .NET的Windows应用程序以及Web应用程序连接到数据库或XML文件等的数据访问技术。它不同于ADO技术,主要在于它可以在非连接环境下访问数据库,支持对XML的无缝访问。DataSet是ADO.NET中的核心技术,它的出现使得访问数据库变得非常方便。DataSet可以看成是应用程序所在计算机内存中的数据库,应用程序可以通过DataSet对数据库进行非连接模式的访问,即通过DataAdapter将数据从数据库服务器中取出并填充到DataSet中,应用程序访问和操作DataSet中的数据,这一切就像是访问本地的数据库一样,然后再通过DataAdapter将数据更新传送回数据库服务器。


转载于:https://my.oschina.net/bigfool007139/blog/535319

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值