ASP.NET中数据提供程序的两种工作方式

本文介绍了两种常见的SQL数据处理方式:一种是使用Connection、Command及DataReader对象直接查询并展示数据;另一种则是利用DataAdapter与DataSet对象进行数据填充和管理。这两种方法分别通过GridView控件展示了查询结果。

 

 1 using  System;
 2
 3 // 包含了用于访问和存储关系型数据的基本对象和公共类,如DataSet、DataTable、DataRelation
 4 using  System.Data;
 5 using  System.Collections.Generic;
 6 using  System.Linq;
 7 using  System.Web;
 8 using  System.Web.UI;
 9 using  System.Web.UI.WebControls;
10 using  System.Data.Sql ;
11
12 // 包含了用于连接和操纵SQLServer数据库的公共类,如SqlConnection、SqlCommand
13 using  System.Data.SqlClient;
14 ExpandedBlockStart.gif /// <summary>
15/// 数据提供程序的两种工作方式
16/// </summary>

17 public   partial   class  _Default : System.Web.UI.Page 
18 ExpandedBlockStart.gif {
19    protected void Page_Load(object sender, EventArgs e)
20ExpandedSubBlockStart.gif  {
21ExpandedSubBlockStart.gif      /***
22         * 使用Connection对象,Command对象和DataReader对象来处理数据
23         * 
24         * */

25
26        //构造用于构造建立连接的Connection对象
27        SqlConnection conn1 = new SqlConnection();
28
29        //设置连接对象的连接字符串属性ConnectionString
30        conn1.ConnectionString = "Data Source=SHERRY;Initial Catalog=test;Integrated Security=True";
31
32        //构造命令对象Command对象
33        SqlCommand cmd1 = new SqlCommand();
34
35        //设置命令对象的CommandText属性
36        cmd1.CommandText = "select *from [user]";
37
38        //设置命令对象的Connection属性,该属性表示命令对象是向哪一个连接发送命令
39        cmd1.Connection = conn1;
40        
41        //打开数据库连接
42        conn1.Open();
43        
44        //执行命令,并且将返回结果指向DataReader对象
45        SqlDataReader reader = cmd1.ExecuteReader();
46
47        //用GridView控件显示数据
48        GridView1.DataSource = reader;
49        GridView1.DataBind();
50       
51        //关闭连接
52        conn1.Close();
53
54ExpandedSubBlockStart.gif      /***
55         * 使用DataAdapter对象和DataSet对象来处理数据
56         * 
57         * */

58
59        //定义数据适配器对象
60        SqlDataAdapter adapter = new SqlDataAdapter();
61
62        //定义数据集对象
63        DataSet dsDataSet = new DataSet();
64
65        //定义数据连接对象,并设置连接字符串属性
66        SqlConnection conn2 = new SqlConnection();
67        conn2.ConnectionString = "Data Source=SHERRY;Initial Catalog=test;Integrated Security=True";
68
69        //定义命令对象,并设置相关属性
70        SqlCommand cmd2 = new SqlCommand();
71        cmd2.CommandText = "select *from [user]";
72        cmd2.Connection = conn2;
73
74        //将查询命令设置为数据适配器对象的SelectCommand属性
75        adapter.SelectCommand = cmd2;
76
77        //定义DataTable对象
78        DataTable table = new DataTable();
79
80        //使用数据适配器对象的Fill方法填充数据集
81        adapter.Fill(dsDataSet, "table");
82
83        //放入DataTable中
84        table = dsDataSet.Tables["table"];
85
86        //输出DataSet中DataTable的默认视图
87        this.GridView2.DataSource = table.DefaultView;
88        this.GridView2.DataBind();
89    }

90    
91}

转载于:https://www.cnblogs.com/zhzzyp/archive/2009/03/03/1401952.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值