在web.Config文件中添加数据库连接配置
-
新建一个网站,打开web.config文件,在connectionString配置节点添加add节点进行数据库进行数据库连接配置代码如下:
<connectionStrings>
<add name="SqlConnection" providerName="System.Data.SqlClient"
connectionString="server=.;uid=sa;pwd=accp;database=pubs"/>
</connectionStrings>
或者
<appSettings>
<add key="SqlServer2012" value="Password=123456;User ID=sa;Initial Catalog=PlayVideo;Data Source=3UQMLLPJRYXNWYT;Packet Size=4096"/>
</appSettings> -
在设计器中双击Default.aspx页面空白处,并 Page_load事件处理函数中,添加访问配置连接字符串代码:
引入命名空间using System.Configuration;
protected void Page_load(object sender,EventArgs e)
{
string name="SqlConnection";
string connectionString=ConfigurationManager.ConnectionStrings
["SqlConnection"].ConnectionString;
string providerName=ConfigurationManager.ConnerctionStrings
string providerName=ConfigurationManager.ConnerctionStrings
["SqlConnection"].ProviderName;
StringBuilder builder=new StringBuilder(string.Empty);
builder.AppendFormat("<b>连接字符串键名:{0}</b><br/>",name);
builder.AppendFormat("<b>数据库连接字符串:{0}</b><br/>",connectionString);
builder.AppendFormat("<b>数据t提供程序名称:{0}</b><br/>,providerName");
this.Response.Write(builder.ToString());
}
-
运行网站,在Default.aspx页面输出了配置完成连接字符串