现在的位置: 首页 > 综合 > 正文

一段常用的Page_Load事件

2013年10月12日 ⁄ 综合 ⁄ 共 533字 ⁄ 字号 评论关闭
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());
   }
  }

抱歉!评论已关闭.