当前位置: 首页 > 工具软件 > Database .NET > 使用案例 >

[我的ASP.net学习历程]DataBase Settion(5)

叶富
2023-12-01
  protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 写法一:
// string connectionString = "SERVER=172.24.17.134;USER ID=sa;PWD=testPass;DATABASE=TestDb";
// 写法二:
// string connectionString = "SERVER=HD-SERVER;USER ID=sa;PWD=testPass;DATABASE=TestDb";
// 写法三:
// string connectionString = "SERVER=(local);USER ID=sa;PWD=testPass;DATABASE=TestDb";
string connectionString = "SERVER=(local);USER ID=sa;PWD=testPass;DATABASE=TestDb";
string strSQL = "SELECT * FROM Table_1";

SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(strSQL,conn);
cmd.Connection.Open();

SqlDataReader dr = cmd.ExecuteReader();

DataGrid1.DataSource = dr;
DataGrid1.DataBind();

cmd.Dispose();
dr.Close();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
 类似资料: