DataReader Deom2_NextResult
Web.config中加:
DataReaderTest2.aspx.cs
Code
1 <connectionStrings>
2 <add name="pubsData" connectionString="Data Source=.;DataBase=pubs;uid=sa;pwd="/>
3 <add name="Northwind" connectionString="Data Source=.;DataBase=Northwind;uid=sa;pwd="/>
4 </connectionStrings>
1 <connectionStrings>
2 <add name="pubsData" connectionString="Data Source=.;DataBase=pubs;uid=sa;pwd="/>
3 <add name="Northwind" connectionString="Data Source=.;DataBase=Northwind;uid=sa;pwd="/>
4 </connectionStrings>
DataReaderTest2.aspx.cs
Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3
4 //DbDataReaderTest1();
5 ConnectionStringSettings strconn = ConfigurationManager.ConnectionStrings["Northwind"];
6 System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(strconn.ConnectionString);
7 DataReaderTest(sqlConn);
8 }
9
10 private void DbDataReaderTest1()
11 {
12 TextBox tb = new TextBox();
13 tb.Columns = 50;
14 tb.Rows = 40;
15 tb.TextMode = TextBoxMode.MultiLine;
16
17 ConnectionStringSettings pubs = ConfigurationManager.ConnectionStrings["pubsData"];
18
19 System.Data.Common.DbConnection connection = new System.Data.SqlClient.SqlConnection(pubs.ConnectionString);
20 System.Data.Common.DbCommand cmd = connection.CreateCommand();
21 cmd.CommandType = CommandType.Text;
22 cmd.CommandText = "SELECT stor_id,ord_num FROM Sales";
23 connection.Open();
24
25 System.Data.Common.DbDataReader rdr = cmd.ExecuteReader();
26 while (rdr.Read())
27 {
28 tb.Text += (string)rdr["stor_id"] + " : " +(string)rdr["ord_num"];
29 tb.Text += "\n";
30 }
31 form1.Controls.Add(tb);
32 rdr.Close();
33 cmd.Dispose();
34 connection.Close();
35 }
36
37 private void DataReaderTest(System.Data.SqlClient.SqlConnection connection)
38 {
39 using (connection)
40 {
41 TextBox tb = new TextBox();
42 tb.Columns = 50;
43 tb.Rows = 50;
44 tb.TextMode = TextBoxMode.MultiLine;
45 SqlCommand command = new SqlCommand(
46 "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
47 "SELECT EmployeeID, LastName FROM dbo.Employees",
48 connection);
49 connection.Open();
50
51 SqlDataReader reader = command.ExecuteReader();
52
53 while (reader.HasRows)
54 {
55 tb.Text += reader.GetName(1)+ "\t";
56
57 while (reader.Read())
58 {
59 tb.Text += reader.GetString(1)+"\n";
60 }
61 reader.NextResult();
62 }
63 form1.Controls.Add(tb);
64 connection.Dispose();
65 command.Dispose();
66 connection.Close();
67 }
68
69 }
1 protected void Page_Load(object sender, EventArgs e)
2 {
3
4 //DbDataReaderTest1();
5 ConnectionStringSettings strconn = ConfigurationManager.ConnectionStrings["Northwind"];
6 System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(strconn.ConnectionString);
7 DataReaderTest(sqlConn);
8 }
9
10 private void DbDataReaderTest1()
11 {
12 TextBox tb = new TextBox();
13 tb.Columns = 50;
14 tb.Rows = 40;
15 tb.TextMode = TextBoxMode.MultiLine;
16
17 ConnectionStringSettings pubs = ConfigurationManager.ConnectionStrings["pubsData"];
18
19 System.Data.Common.DbConnection connection = new System.Data.SqlClient.SqlConnection(pubs.ConnectionString);
20 System.Data.Common.DbCommand cmd = connection.CreateCommand();
21 cmd.CommandType = CommandType.Text;
22 cmd.CommandText = "SELECT stor_id,ord_num FROM Sales";
23 connection.Open();
24
25 System.Data.Common.DbDataReader rdr = cmd.ExecuteReader();
26 while (rdr.Read())
27 {
28 tb.Text += (string)rdr["stor_id"] + " : " +(string)rdr["ord_num"];
29 tb.Text += "\n";
30 }
31 form1.Controls.Add(tb);
32 rdr.Close();
33 cmd.Dispose();
34 connection.Close();
35 }
36
37 private void DataReaderTest(System.Data.SqlClient.SqlConnection connection)
38 {
39 using (connection)
40 {
41 TextBox tb = new TextBox();
42 tb.Columns = 50;
43 tb.Rows = 50;
44 tb.TextMode = TextBoxMode.MultiLine;
45 SqlCommand command = new SqlCommand(
46 "SELECT CategoryID, CategoryName FROM dbo.Categories;" +
47 "SELECT EmployeeID, LastName FROM dbo.Employees",
48 connection);
49 connection.Open();
50
51 SqlDataReader reader = command.ExecuteReader();
52
53 while (reader.HasRows)
54 {
55 tb.Text += reader.GetName(1)+ "\t";
56
57 while (reader.Read())
58 {
59 tb.Text += reader.GetString(1)+"\n";
60 }
61 reader.NextResult();
62 }
63 form1.Controls.Add(tb);
64 connection.Dispose();
65 command.Dispose();
66 connection.Close();
67 }
68
69 }