一.Command類
Command類可以執行任何類型的SQL語句。
1.Command對象的主要屬性
在使用任何一個Command對象之前,必須完成對Command對象以下三個屬性的設定。
(1).CommandType
其值為一枚舉。枚舉值分別為:Command.Text;Command.StoredProcedure;Command.TableDirect;
Command對象預設的CommandType類型為Command.Text
(2).CommandText
其值為SQL語句或者為預存程序的名字。
(3).Connection
Command初始化的樣本:
C#
1 SqlCommand cmd = new SqlCommand();
2 cmd.Connection = connection;
3 cmd.CommandType = CommandType.Text;
4 cmd.CommandText = "SELECT * FROM Productions";
5
6 SqlCommand cmd1 = new SqlCommand("SELECT * FROM Productions", connection);
7
8 SqlCommand cmd2 = new SqlCommand("SelectAllProductions", connection);
9 cmd2.CommandType = CommandType.StoredProcedure;
2.Command對象的主要方法
(1).ExecuteNonQuery()
執行非select語句,如插入、刪除、更新等。返回執行命令影響的行數。
(2).ExecuteScalar()
指向select查詢,返回結果集第一行第一列的資料。常用於計算單個值的彙總SELECT語句。
(3).ExecuteReader()
執行select查詢,返回一個封裝了唯讀、唯進遊標的DataReader對象。
二.DataReader類
DataReader允許以唯進、唯讀方式每次讀取一條select命令返回的記錄。
DataReader的主要方法
(1).read()
將行遊標行進到流的下一行。在讀取第一行記錄前也必須調用這個方法。當還有其他行時,返回真;如果為最後一行,返回假。
(2).GetValue()
返回當前行中制定序號的欄位值。
(3).GetValues()
將當前行中的值儲存到一個數組中。可以使用DataReader.FieldCount確定行中欄位的個數。
(4).Getxxx()
如GetInt32等。作用如同GetValue(),只是傳回值是帶類型的。注意欄位為空白是的保護,此時它返回DBNull.value
(5).NextResult()
如果DataReader包含不止一個行集,則該方法將遊標移動到下一個行集的第一行之前。
(6).Close()
關閉DataReader。如果原命令執行一個預存程序並帶有一個輸出參數,該參數在Reader關閉後才可讀。
三.Command和DataReader聯合使用的一個樣本
HTML
1 <div>
2 <asp:Literal ID="litInfo" runat="server"></asp:Literal>
3 <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
4 <br />
5 <ul><li><b>共有商品 : </b><%= this.CommandScalar() %>種</li></ul>
6 <ul><li><b>共更新 : </b><%= this.CommandNonQuery() %>條記錄</li></ul>
7 </div>
後台代碼
C#
1 protected void SqlCommondSelect()
2 {
3 string connectionString = ConfigurationManager.ConnectionStrings["MyDemo"].ConnectionString;
4 SqlConnection connection = new SqlConnection(connectionString);
5 SqlCommand cmd2 = new SqlCommand("SelectAllProductions", connection);
6 cmd2.CommandType = CommandType.StoredProcedure;
7 try
8 {
9 connection.Open();
10 SqlDataReader reader = cmd2.ExecuteReader(CommandBehavior.CloseConnection);
11 StringBuilder htmlStr = new StringBuilder();
12 htmlStr.Append("<ul>");
13 while (reader.Read())
14 {
15 htmlStr.Append("<li><b>Production Name : </b>" + reader.GetString(2));
16 htmlStr.Append("<b> Price : </b>" + (decimal)reader["DefaultPrice"] + "</li>");
17 }
18 htmlStr.Append("</ul>");
19 reader.Close();
20 connection.Close();
21 this.litInfo.Text = htmlStr.ToString();
22 }
23 catch (Exception err)
24 {
25 this.lblError.Text = err.Message;
26 }
27 }
28
29 protected int CommandScalar()
30 {
31 string connectionString = ConfigurationManager.ConnectionStrings["MyDemo"].ConnectionString;
32 SqlConnection con = new SqlConnection(connectionString);
33 SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Productions", con);
34 con.Open();
35 int count = (int)cmd.ExecuteScalar();
36 con.Close();
37 return count;
38 }
39
40 protected int CommandNonQuery()
41 {
42 string connectionString = ConfigurationManager.ConnectionStrings["MyDemo"].ConnectionString;
43 SqlConnection con = new SqlConnection(connectionString);
44 SqlCommand cmd = new SqlCommand("UPDATE Productions SET ProductionName=ProductionName WHERE CateGoryID=1", con);
45 con.Open();
46 int count = cmd.ExecuteNonQuery();
47 con.Close();
48 return count;
49 }
清清楚楚asp.net系列學習部落格目錄
參考資料:Pro ASP.NET 3.5 in C# 2008