怎么在asp.net读取Web.config的<appSettings></appSettings>里面的东西
这是我定义的Web.config;<appSettings><addkey="sqlCon"value="DataSource=jing;InitialCatalog=j...
这是我定义的Web.config;<appSettings >
<add key="sqlCon " value="Data Source=jing;Initial Catalog=jing;User ID=sa;Password=123456;"/>
</appSettings>在Default.aspx页面中读取连接字符串
protected void Page_Load(object sender, EventArgs e)
{
string con_sqlserver = ConfigurationManager.AppSettings["sqlCon"].ToString ();
Response.Write("SQL Server数据库连接字符串:" + con_sqlserver);
SqlConnection conn = new SqlConnection(con_sqlserver );
try
{
conn.Open();
Response.Write("<script>alert('连接成功')</script>");
conn.Close();
}
catch (Exception ex)
{
Response.Write("<script>alert("+ex.ToString ()+")</script>");
}
}
运行之后出现
未将对象引用设置到对象的实例。
string con_sqlserver = ConfigurationManager.AppSettings["sqlCon"].ToString ();提示是这句话有问题,
把ToString ()去掉怎么就可以了。
请问怎么解决啊,不胜感激。。。 展开
<add key="sqlCon " value="Data Source=jing;Initial Catalog=jing;User ID=sa;Password=123456;"/>
</appSettings>在Default.aspx页面中读取连接字符串
protected void Page_Load(object sender, EventArgs e)
{
string con_sqlserver = ConfigurationManager.AppSettings["sqlCon"].ToString ();
Response.Write("SQL Server数据库连接字符串:" + con_sqlserver);
SqlConnection conn = new SqlConnection(con_sqlserver );
try
{
conn.Open();
Response.Write("<script>alert('连接成功')</script>");
conn.Close();
}
catch (Exception ex)
{
Response.Write("<script>alert("+ex.ToString ()+")</script>");
}
}
运行之后出现
未将对象引用设置到对象的实例。
string con_sqlserver = ConfigurationManager.AppSettings["sqlCon"].ToString ();提示是这句话有问题,
把ToString ()去掉怎么就可以了。
请问怎么解决啊,不胜感激。。。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询