qyjun

长时间使用2.0的控件可能会使程序员退化的不会写代码,下面的代码是使GridView绑定到DataSet或DataReader上。

 1private 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    }

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-09-02
  • 2021-10-29
  • 2021-12-24
  • 2022-12-23
  • 2021-11-20
  • 2021-10-01
  • 2022-12-23
猜你喜欢
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-06-06
  • 2021-06-06
  • 2021-11-29
相关资源
相似解决方案