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());
}
}