天天看点

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>

  【2】在后台中,引入程序集或者手工引入:using system.web.configuration;

  【3】后台写入的方法为:

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

}

最新内容请见作者的github页:http://qaseven.github.io/