【1】在web.config配置文件中,
<connectionStrings> <add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient" /> </connectionStrings> |
protected void Page_Load(object sender, EventArgs e) { //从web.config中引用连接字符串 string strConn = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; SqlConnection myConn = new SqlConnection(strConn); string strSQL = "SELECT Top 5 CategoryID,CategoryName,Description From Categories"; SqlCommand myCommand = new SqlCommand(strSQL, myConn); myConn.Open(); myGv.DataSource = myCommand.ExecuteReader(); myGv.DataBind(); myConn.Close(); } |