connectionString属性尚未初始化
我用的是动软代码生成器生成的三层。现在欲往数据库添加数据。
可老是提示connectionString属性尚未初始化
我在web.config里配置了<add name="constr" connectionString="server=.\sqlexpress;database=BBSDB;integrated security=sspi"/>
数据访问层里也写了
public abstract class DbHelperSQL
{
//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings
求解! --------------------编程问答-------------------- public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
上面的写错了。 --------------------编程问答-------------------- ConfigurationManager.AppSettings["constr"]; --------------------编程问答-------------------- 你看System.Configuration.ConfigurationManager.ConnectionStrings 取到值了吗 --------------------编程问答--------------------
1. website 网站要引用 数据访问层类库,就是要添加引用
2.在数据访问层类库中添加引用System.Configuration --------------------编程问答-------------------- 引用在生成代码的时候就已经添加了的。
3楼的办法我也试了。
还是没有初始化 --------------------编程问答-------------------- Web.config中
<appSettings>
<add key="ConnectString" value="server=.\sqlexpress;database=BBSDB;integrated security=sspi"/>
</appSettings>
cs文件中
--------------------编程问答-------------------- 配置:
protected void Button1_Click1(object sender, EventArgs e)
{
string m_str = System.Configuration.ConfigurationManager.AppSettings["ConnectString"];
}
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="constr" connectionString="server=.\sqlexpress;database=BBSDB;integrated security=sspi" providerName="System.Data.SqlClient" />
</connectionStrings>
<!--
............
-->
</configuration>
读取:
string connectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
配置:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="constr" value="server=.\sqlexpress;database=BBSDB;integrated security=sspi" />
</appSettings>
<!--
............
-->
</configuration>
读取:
string connectionString = ConfigurationManager.AppSettings["constr"];
--------------------编程问答--------------------
<connectionStrings>
<add name="sqlCon" connectionString="Server=.\SQLEXPRESS;Database=BBSDB;Trusted_Connection=true;"/>
</connectionStrings>
--------------------编程问答-------------------- 支持8楼 --------------------编程问答--------------------
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString);
System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ToString();
补充:.NET技术 , ASP.NET