c#.net中sqldataadapter与dateset的简单使用

 1 using System.Data.SqlClient;
 2 using System.Data;
 3 
 4 public partial class test4 : System.Web.UI.Page
 5 {
 6     protected void Page_Load(object sender, EventArgs e)
 7     {
 8         SqlConnection con = new SqlConnection();
 9         con.ConnectionString = "server=.;database=航空订票系统;uid=sa;pwd=;";
10         SqlCommand cmd = new SqlCommand();
11         cmd.Connection = con;
12         SqlDataAdapter da = new SqlDataAdapter();
13         cmd.CommandText = "select 用户名 from 用户表 ";
14         da.SelectCommand = cmd;
15         DataSet ds = new DataSet();
16         try                       //连接数据库异常处理
17         {
18             con.Open();
19             try     //填入dataset的异常处理
20             {
21                 ds.Clear();
22                 da.Fill(ds, "用户表");         /**
23                                              * 
24                                              * 将da获取到的表数据和结构填充到ds数据集中,ds相当于一个数据库,
25                                              * 里面有很多表,第一个表就是ds.table[0]。我们限制的是表名为用户表,
26                                              * 这个表名是自己设定的,在使用的时候就直接ds.table["用户表"],
27                                              * 就选用da获取到的数据表。
28                                              * 
29                                              * **/
30                     if (ds.Tables["用户表"].Rows.Count == 0)
31                     {
32                         Response.Write("没有查询到数据,请重试");
33                     }
34                     else
35                     {
36                         Response.Write("<br>");
37                         for (int i = 0; i < ds.Tables["用户表"].Rows.Count; i++)
38                         {
39                             Response.Write(ds.Tables["用户表"].Rows[i]["用户名"] + "<br>");
40 
41                         }
42 
43 
44                     }
45                 con.Close();
46                 }
47                
48             
49             catch (SqlException)                                      /**
50                                                                      * 
51                                                                      * 这个异常抛出是对da.fill(ds."用户表")是产生的数据操作异常
52                                                                      * 
53                                                                      * **/
54             {
55                 Response.Write("数据操作出现异常,请检查");
56                 con.Close();
57             }
58         }
59             
60         catch (Exception)                                               /**
61                                                                          * 
62                                                                          * 这个异常时抛出打开数据库会出现的异常
63                                                                          * 
64                                                                          * **/
65 
66         {
67             Response.Write("数据库连接失败");
68             con.Close();
69         } 
70              
71       }
72 }

在30行处应该还有一个抛出sql没有数据的异常,但是不理解无数据跟数据为0条的差异。。。。郁闷

 1 using System.Data.SqlClient;
 2 using System.Data;
 3 
 4 public partial class test4 : System.Web.UI.Page
 5 {
 6     protected void Page_Load(object sender, EventArgs e)
 7     {
 8         SqlConnection con = new SqlConnection();
 9         con.ConnectionString = "server=.;database=航空订票系统;uid=sa;pwd=;";
10         SqlCommand cmd = new SqlCommand();
11         cmd.Connection = con;
12         SqlDataAdapter da = new SqlDataAdapter();
13         cmd.CommandText = "select 用户名 from 用户表 where 用户名='1'";
14         da.SelectCommand = cmd;
15         DataSet ds = new DataSet();
16         try                       //连接数据库异常处理
17         {
18             con.Open();
19         }
20               catch (Exception)                                               /**
21                                                                          * 
22                                                                          * 这个异常时抛出打开数据库会出现的异常
23                                                                          * 
24                                                                          * **/
25         {
26             Response.Write("数据库连接失败");
27             con.Close();
28         } 
29             try     //填入dataset的异常处理
30             {
31                 ds.Clear();
32                 da.Fill(ds, "用户表");         /**
33                                              * 
34                                              * 将da获取到的表数据和结构填充到ds数据集中,ds相当于一个数据库,
35                                              * 里面有很多表,第一个表就是ds.table[0]。我们限制的是表名为用户表,
36                                              * 这个表名是自己设定的,在使用的时候就直接ds.table["用户表"],
37                                              * 就选用da获取到的数据表。
38                                              * 
39                                              * **/
40             }
41            catch (SqlException)                                      /**
42                                                                      * 
43                                                                      * 这个异常抛出是对da.fill(ds."用户表")是产生的数据操作异常
44                                                                      * 
45                                                                      * **/
46             {
47                 Response.Write("数据操作出现异常,请检查");
48                 con.Close();
49             }
50             try
51             {
52                 if (ds.Tables["用户表"].Rows.Count == 0)
53                 {
54                     Response.Write("没有查询到数据,请重试");
55                 }
56                 else
57                 {
58                     Response.Write("<br>");
59                     for (int i = 0; i < ds.Tables["用户表"].Rows.Count; i++)
60                     {
61                         Response.Write(ds.Tables["用户表"].Rows[i]["用户名"] + "<br>");
62 
63                     }
64                 }
65                 con.Close();
66             }
67             catch (SqlException)
68             {
69                 Response.Write("无数据");//这个异常抛出很迷茫,怎样才会出现
70                 con.Close();                    //怎么才会出现这种异常呢?望解答
71             }
72 
73             }
74          
75         }         
就当吧,你说的都对,行,好的,嗯.
原文地址:https://www.cnblogs.com/beio/p/2874841.html