日期:2014-05-18  浏览次数:20968 次

C#初学者的问题
在web.config中,
一个为:
<configuration>
<appSettings>
<add key="Connection" value="Server=.;database=.;uid=sa;pwd=123"/>
  <add key="StrConn" value="Server=.;database=.;uid=sa;pwd=123"/>
</appSettings>
<connectionStrings/>。
另外一个为:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=Managementsystem;User ID=sa;Password=123"/>
<add name="ManagementsystemConnectionString" connectionString="Data Source=.;Initial Catalog=Managementsystem;Persist Security Info=True;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>。
把第一种中的一下代码考到第二种中使用,出现了
异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化

源错误:


行 67: {
行 68: SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
行 69: con.Open();
行 70: string sql = "select * from [Check] where userName='" + Session["UserName"].ToString() + "'";
行 71: SqlDataAdapter da = new SqlDataAdapter(sql, con); 
是和web.config中此处有关吗?
该如何解决?




------解决方案--------------------
ConfigurationManager.AppSettings["Connection"]
哪有Connection节点,读不到数据
------解决方案--------------------
C# code
var connstr = ConfigurationManager.AppSettings["Connection"];
SqlConnection con = new SqlConnection(connstr);
con.Open();
string sql = "select * from [Check] where userName='" + Session["UserName"].ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql, con);

------解决方案--------------------
C# code
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();

------解决方案--------------------
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString()
------解决方案--------------------
ConfigurationManager----> ConfigurationSettings
试验一下