ホームページ  >  記事  >  データベース  >  数据库连接

数据库连接

WBOY
WBOYオリジナル
2016-06-07 14:59:351169ブラウズ

appSettings add key="sqlCon" value="Data Source=(local);Database=Northwind;Uid=sa;Pwd=;"/ /appSettings protected void Page_Load(object sender, EventArgs e) { string con_sqlserver = ConfigurationManager.AppSettings["sqlCon"].ToString();//获



protected void Page_Load(object sender, EventArgs e)
{
string con_sqlserver = ConfigurationManager.AppSettings["sqlCon"].ToString();//获取配置文件中的数据库字符串信息
Response.Write("SQL Server数据库连接字符串:"+con_sqlserver);//输出配置文件中的数据库字符串信息
SqlConnection con = new SqlConnection(con_sqlserver);//创建数据库连接对象
try
{
con.Open();//打开数据库连接
Response.Write("<script>alert('连接成功')</script>");
con.Close();//关闭数据库连接
}
catch (Exception ex)
{
Response.Write("<script>alert(" + ex.ToString() + ")</script>");
}
}

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。