DataReader Demo.
数据是Northwind.
从Northwind 数据库中取出数据放入自己写的一个表中。然后用一个GridView控件显示。
从Northwind 数据库中取出数据放入自己写的一个表中。然后用一个GridView控件显示。
Code
1protected void Page_Load(object sender, EventArgs e)
2{
3 string conn = "Data Source=.;DataBase=Northwind;uid=sa;pwd=";
4 DataReaderTest(conn);
5}
6
7//DataReader test
8private void DataReaderTest(string connString)
9{
10 using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
11 {
12 string commandText = "SELECT TOP 50 CustomerID,CompanyName,City,Phone FROM dbo.Customers";
13 int count = 0;
14 GridView gd = new GridView();
15 DataTable dt = CreateTable();
16 DataRow dw;
17
18 System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandText, conn);
19 conn.Open();
20 System.Data.SqlClient.SqlDataReader customerReaderData = command.ExecuteReader();
21
22 while (customerReaderData.Read())
23 {
24 /**////*
25 dw = dt.NewRow();
26 dw["CustomerID"] = (string)customerReaderData[0];
27 dw["CompanyName"] = (string)customerReaderData[1];
28 dw["CustomerCity"] = (string)customerReaderData[2];
29 dw["CustomerPhone"] = (string)customerReaderData[3];
30
31 dt.Rows.Add(dw);
32 count++;
33 }
34
35 gd.DataSource = dt;
36 gd.DataBind();
37 form1.Controls.Add(gd);
38 customerReaderData.Close();
39 command.Dispose();
40 conn.Close();
41
42 }
43}
44
45//根据所取的数据库中的表(odb.Customers)创建一张表.
46private DataTable CreateTable()
47{
48 DataTable t1 = new DataTable("test");
49
50 //CustomerID Column
51 DataColumn id = new DataColumn();
52 id.DataType = System.Type.GetType("System.String");
53 id.ColumnName = "CustomerID";
54 t1.Columns.Add(id);
55 //CustomerName Column
56 DataColumn cName = new DataColumn();
57 cName.DataType = System.Type.GetType("System.String");
58 cName.ColumnName = "CompanyName";
59 t1.Columns.Add(cName);
60 //CustomerCity Column
61 DataColumn cCity = new DataColumn();
62 cCity.DataType = System.Type.GetType("System.String");
63 cCity.ColumnName = "CustomerCity";
64 t1.Columns.Add(cCity);
65 //CustomerPhone Column
66 DataColumn cPhone = new DataColumn();
67 cPhone.DataType = System.Type.GetType("System.String");
68 cPhone.ColumnName = "CustomerPhone";
69 t1.Columns.Add(cPhone);
70
71 return t1;
72}
1protected void Page_Load(object sender, EventArgs e)
2{
3 string conn = "Data Source=.;DataBase=Northwind;uid=sa;pwd=";
4 DataReaderTest(conn);
5}
6
7//DataReader test
8private void DataReaderTest(string connString)
9{
10 using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
11 {
12 string commandText = "SELECT TOP 50 CustomerID,CompanyName,City,Phone FROM dbo.Customers";
13 int count = 0;
14 GridView gd = new GridView();
15 DataTable dt = CreateTable();
16 DataRow dw;
17
18 System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(commandText, conn);
19 conn.Open();
20 System.Data.SqlClient.SqlDataReader customerReaderData = command.ExecuteReader();
21
22 while (customerReaderData.Read())
23 {
24 /**////*
25 dw = dt.NewRow();
26 dw["CustomerID"] = (string)customerReaderData[0];
27 dw["CompanyName"] = (string)customerReaderData[1];
28 dw["CustomerCity"] = (string)customerReaderData[2];
29 dw["CustomerPhone"] = (string)customerReaderData[3];
30
31 dt.Rows.Add(dw);
32 count++;
33 }
34
35 gd.DataSource = dt;
36 gd.DataBind();
37 form1.Controls.Add(gd);
38 customerReaderData.Close();
39 command.Dispose();
40 conn.Close();
41
42 }
43}
44
45//根据所取的数据库中的表(odb.Customers)创建一张表.
46private DataTable CreateTable()
47{
48 DataTable t1 = new DataTable("test");
49
50 //CustomerID Column
51 DataColumn id = new DataColumn();
52 id.DataType = System.Type.GetType("System.String");
53 id.ColumnName = "CustomerID";
54 t1.Columns.Add(id);
55 //CustomerName Column
56 DataColumn cName = new DataColumn();
57 cName.DataType = System.Type.GetType("System.String");
58 cName.ColumnName = "CompanyName";
59 t1.Columns.Add(cName);
60 //CustomerCity Column
61 DataColumn cCity = new DataColumn();
62 cCity.DataType = System.Type.GetType("System.String");
63 cCity.ColumnName = "CustomerCity";
64 t1.Columns.Add(cCity);
65 //CustomerPhone Column
66 DataColumn cPhone = new DataColumn();
67 cPhone.DataType = System.Type.GetType("System.String");
68 cPhone.ColumnName = "CustomerPhone";
69 t1.Columns.Add(cPhone);
70
71 return t1;
72}