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

ConnectionString 属性尚未初始化。求大神帮助~~
ConnectionString 属性尚未初始化。?
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。?

异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化

源错误:?


行 42: try
行 43: {
行 44: sda.Fill(ds);
行 45: }
行 46: catch (SqlException e)
?

源文件: c:\Inetpub\wwwroot\GD_Test\App_Code\Db.cs 行: 44?

堆栈跟踪:?


[InvalidOperationException: ConnectionString 属性尚未初始化。]
? System.Data.SqlClient.SqlConnection.PermissionDemand() +941579
? System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) +22
? System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +105
? System.Data.SqlClient.SqlConnection.Open() +111
? System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +121
? System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +137
? System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +86
? GD.Db.ExecuteSelectSql(String sqlSelect) in c:\Inetpub\wwwroot\GD_Test\App_Code\Db.cs:44
? GD.AdminTeacher.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\GD_Test\AdminTeacher.aspx.cs:24
? System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
? System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
? System.Web.UI.Control.OnLoad(EventArgs e) +99
? System.Web.UI.Control.LoadRecursive() +47
? System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

?


--------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.1891; ASP.NET 版本:2.0.50727.1879




web.config里面的连接代码是:
<appSettings/>
? <!--?
? 数据库连接字符串
? -->
? <connectionStrings>
? <add name="ConnectionString" connectionString="data source=.\SQLEXPRESS;initial catalog=GD;persist security info=True;user id=sa;pwd=sa;packet size=4096"/>
? </connectionStrings>
? ?
<system.web>?



Db.cs中的代码是:
?protected static string connString = ConfigurationManager.AppSettings["ConnectionString"];

? public Db()
? {
? }

? /// <summary>
? /// 执行select类型的sql语句,返回select得到的数据集
? /// </summary>
? /// <param name="sqlSelect">select语句</param>
? /// <returns>返回select得到的数据集</returns>
? public static DataSet ExecuteSelectSql(string sqlSelect)
? {
? SqlConnection conn = new SqlConnection(connString);
? SqlDataAdapter sda = new SqlDataAdapter(sqlSelect, conn);
? DataSet ds = new DataSet();
? try
? {
? sda.Fill(ds);
? }
? catch (SqlException e)
? {
? throw new Exception(e.Message);
? }
? return ds;
? }




调试了很久~~ 新手求帮助啊~!!!

------解决方案--------------------
很明显,你的连接字符串是写在<connectionStrings></connectionStrings>之间的,应该改成这样:

protected static string connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionStri