属性 | 说明 |
Depth | 其返回类型为int,取得表示当前行嵌入深度的值 |
FieldCount | 其返回类型为int,取得当前行的列数 |
IsColsed | 其返回类型为bool,取得一个布尔值,表示是否关闭数据读取 |
RecordsAffected | 其返回类型为int, 取得执行SQL语句增加、修改或删除的行数。 |
方法 | 说明 |
Reader() | 其返回类型为bool,将数据阅读器移到结果集的下一行并读取该行。这个方法返回的布尔值表示结果集中是否有多行 |
GetValue() | 其返回类型为object, 返回指定列的值 |
GetValues() | 其返回类型为int,将当前行中所有列的值复制到指定对象数组。这个方法返回的int是数组元素的个数 |
NextResult() | 其返回类型为bool,将数据阅读器移到结果集的下一行。这个方法返回的布尔值表示结果集中是否有多行 |
Close() 关闭 | SqlDataReader 对象 |
GetInt32(),GetChar(), GateDataTime(),Get×××() |
返回指定列的值,并且返回的类型为相应的数据类型。例如GetInt32()返回整型的数值。注意,如果你将返回值赋予一个类型不匹配的变量时,将会抛出一个InvalidCastException异常 |
01 public partial class _Default : System.Web.UI.Page 02 { 03 protected void Page_Load(object sender, EventArgs e) 04 { 05 string connectionString = 06 ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; 07 SqlConnection con = new SqlConnection(connectionString); 08 string sql = "SELECT top 5 CustomerID,CompanyName,ContactName,Address 09 FROM Customers"; 10 SqlCommand cmd = new SqlCommand(sql, con); 11 con.Open(); 12 SqlDataReader reader = cmd.ExecuteReader(); 13 StringBuilder htmlStr = new StringBuilder(""); 14 while (reader.Read()) 15 { 16 htmlStr.Append("CustomerID:" + reader["CustomerID"] + "<br>"); 17 htmlStr.Append("CompanyName:" + reader["CompanyName"] + "<br>"); 18 htmlStr.Append("ContactName:" + reader.GetString(2) + "<br>"); 19 htmlStr.Append("Address:" + reader.GetString(3) + "<br>"); 20 htmlStr.Append("<hr>"); 21 } 22 reader.Close(); 23 con.Close(); 24 HtmlContent.Text = htmlStr.ToString(); 25 } 26 } |
|
01 public partial class _Default : System.Web.UI.Page 02 { 03 protected void Page_Load(object sender, EventArgs e) 04 { 05 string connectionString = 06 ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; 07 SqlConnection con = new SqlConnection(connectionString); 08 SqlCommand cmd = con.CreateCommand(); 09 cmd.CommandText = "SELECT TOP 3 ProductID,ProductName 10 FROM Products ORDER BY ProductID;" + 11 "SELECT TOP 3 CustomerID,CompanyName 12 FROM Customers ORDER BY CustomerID;" + 13 "SELECT TOP 3 OrderID,CustomerID 14 FROM Orders ORDER BY OrderID;"; 15 con.Open(); 16 SqlDataReader reader = cmd.ExecuteReader(); 17 StringBuilder htmStr=new StringBuilder(""); 18 int i = 0; 19 do 20 { 21 htmStr.Append("结果集"); 22 htmStr.Append(i.ToString()); 23 htmStr.Append("<br>"); 24 while (reader.Read()) 25 { 26 htmStr.Append("reader[0]=" + reader[0]); 27 htmStr.Append("<br>"); 28 htmStr.Append("reader[1]=" + reader[1]); 29 htmStr.Append("<br><br>"); 30 } 31 htmStr.Append("<hr>"); 32 i++; 33 } while (reader.NextResult()); 34 reader.Close(); 35 con.Close(); 36 HtmlContent.Text = htmStr.ToString(); 37 } 38 } |
|
01 public partial class _Default : System.Web.UI.Page 02 { 03 protected void Page_Load(object sender, EventArgs e) 04 { 05 string connectionString = 06 ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; 07 SqlConnection con = new SqlConnection(connectionString); 08 SqlCommand cmd = con.CreateCommand(); 09 cmd.CommandText = "SELECT COUNT(*) FROM Products"; 10 con.Open(); 11 int returnValue = (int)cmd.ExecuteScalar(); 12 HtmlContent.Text = "Products表中共有" + returnValue.ToString()+"条记录"; 13 } 14 } |
|