2013-08-17 147 views
2

我已經搜索並嘗試了一切,但無法找出這一個。我試圖做一些簡單的事情,但好像我做錯了什麼。基本上,任何已經存款的用戶,我想返回true,如果他們沒有,我想返回false。這應該很容易,我想,但我難住這個。爲什麼我得到這個錯誤:ConnectionString屬性尚未初始化

以下是錯誤:

ConnectionString屬性尚未初始化。

描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.InvalidOperationException:ConnectionString屬性尚未初始化。

源錯誤:

Line 59:   Cmd.Parameters.AddWithValue("@UserID", userId); 
Line 60:   con.Open(); 
Line 61: 
Line 62:   result = (int)Cmd.ExecuteScalar(); 

這裏是堆棧跟蹤的頂部:

[InvalidOperationException異常:ConnectionString屬性尚未被初始化] System.Data.SqlClient.SqlConnection。 PermissionDemand()+4879939 System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)+20 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbCon nectionFactory的connectionFactory)+117 System.Data.SqlClient.SqlConnection.Open()+122

這是我返回true或false方法:

public static bool HasDeposit(int userId) 
{ 
    int result = 0; 
    //since executeScalar is intended to retreive only a single value 
    //from a query, we select the number of results instead of the email address 
    //of each matching result. 
    string queryTransaction = "SELECT COUNT(UserID) FROM Transaction WHERE TransactionTypeID = 6 AND UserID = @UserID"; 
    string constr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; 
    SqlConnection con = new SqlConnection(constr); 

    SqlCommand Cmd = new SqlCommand(queryTransaction, con); 

    Cmd.Parameters.AddWithValue("@UserID", userId); 
    con.Open(); 

    result = (int)Cmd.ExecuteScalar(); 

    //returning a boolean comparator works like this : 
    //will return true if the result is greater than zero, but false if it is not. 
    con.Close(); 
    return result > 0; 
} 

任何幫助/指導會非常感激。

+1

嘗試檢查什麼是變量構造 –

+0

你檢查'AppSettings'其中,凱恩指出,是不是把連接字符串在老地方配置文件。 – HABO

+0

顯示您的appsettings –

回答

8

如果你有你的連接字符串在配置這樣

<connectionStrings> 
    <add name="ConnectionString" connectionString="data source=.;Initial Catalog=MyDatabase;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

然後,你將需要使用此方法來訪問它System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString

你也應該包裝在using語句使你的數據訪問你不會泄漏連接並淹沒游泳池。這是使用語句的更新示例。

public static bool HasDeposit(int userId) 
{ 
    //since executeScalar is intended to retreive only a single value 
    //from a query, we select the number of results instead of the email address 
    //of each matching result. 
    const string queryTransaction = "SELECT COUNT(UserID) FROM Transaction WHERE TransactionTypeID = 6 AND UserID = @UserID"; 

    var constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 

    using (var con = new SqlConnection(constr)) 
    { 
     using (var cmd = new SqlCommand(queryTransaction, con)) 
     { 
      cmd.Parameters.AddWithValue("@UserID", userId); 
      con.Open(); 

      var result = (int)cmd.ExecuteScalar(); 

      //returning a boolean comparator works like this : 
      //will return true if the result is greater than zero, but false if it is not. 
      return result > 0; 
     } 
    } 
} 
+0

這工作!優秀!感謝您的幫助球員,非常感謝。 – thenextmogul

1

在web.config中:

<appSettings> 
    <add key="ConnectionString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"/> 
</appSettings> 
相關問題