长时间使用2.0的控件可能会使程序员退化的不会写代码,下面的代码是使GridView绑定到DataSet或DataReader上。
1
private void Bind()
2
{
3
string dbConnection = ConfigurationManager.ConnectionStrings["NewsConnectionString"].ToString();//获得数据库连接字符串
4
Response.Write(dbConnection);
5
SqlConnection conn = new SqlConnection(dbConnection);
6
conn.Open();
7
string sql = "select * from News_News where Class_Id=" +this.ClassId;
8
SqlCommand cmd = new SqlCommand(sql, conn);
9
DataSet ds = new DataSet();
10
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
11
da.Fill(ds);
12
//SqlDataReader dr = cmd.ExecuteReader();
13
GridView1.DataSource = ds.Tables[0];
14
//GridView1.DataSource = dr;
15
GridView1.DataBind();//将GridView绑定到DataSet上
16
string sql1 = "select Class_Name from News_Class where Id=" + this.ClassId;
17
cmd.CommandText = sql1;
18
string className=cmd.ExecuteScalar().ToString();//返回首行首列,动态显示新闻分类
19
Label1.Text = className;
20
conn.Close();
21
22
}

2



3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22
