protected void Page_Load(object sender, EventArgs e)
  {
   SqlConnection myConnection = new SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=;");//创建SQL连接
   SqlCommand myCommand = new SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection);//创建SQL命令
  
   try
   {
    myConnection.Open();//打开数据库连接
    MyGrid.DataSource = myCommand.ExecuteReader();//指定 DataGrid 的数据源
    MyGrid.DataBind();//绑定数据到 DataGrid
    myConnection.Close();//关闭数据连接
   }
   catch(Exception ex)
   {
    //捕获错误
    HttpContext.Current.Response.Write(ex.ToString());
   }
  } 

相关文章:

  • 2021-07-22
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2022-01-07
猜你喜欢
  • 2022-02-26
  • 2021-12-12
  • 2021-08-17
  • 2021-06-02
  • 2021-12-19
  • 2021-08-21
  • 2021-12-23
相关资源
相似解决方案