我已经搜索并尝试了所有方法,但无法弄清楚这一点。我正在尝试做一些简单的事情,但似乎我做错了什么。基本上,任何已经存款的用户,我想返回true,如果他们没有,我想返回false。我想这应该很容易,但我对此感到困惑。
这是错误:
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, DbConnectionFactory connectionFactory) +117 System.Data.SqlClient.SqlConnection.Open() +122
这是我返回真假的方法:
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;
}
对此的任何帮助/指导将不胜感激。