Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么会出现此错误:ConnectionString属性尚未初始化_C#_Asp.net_Methods_Runtime Error - Fatal编程技术网

C# 为什么会出现此错误:ConnectionString属性尚未初始化

C# 为什么会出现此错误:ConnectionString属性尚未初始化,c#,asp.net,methods,runtime-error,C#,Asp.net,Methods,Runtime Error,我什么都找遍了,试遍了,但找不出这个。我试图做一些简单的事情,但似乎我做错了什么。基本上,任何已经存款的用户,我都想返回true,如果他们没有,我想返回false。我想这应该很容易,但我被难倒了 错误如下: public static bool HasDeposit(int userId) { int result = 0; //since executeScalar is intended to retreive only a single value //from a

我什么都找遍了,试遍了,但找不出这个。我试图做一些简单的事情,但似乎我做错了什么。基本上,任何已经存款的用户,我都想返回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;
}
ConnectionString属性尚未初始化

描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

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

源错误:

Line 59:         Cmd.Parameters.AddWithValue("@UserID", userId);
Line 60:         con.Open();
Line 61: 
Line 62:         result = (int)Cmd.ExecuteScalar();
这是堆栈跟踪的顶部:

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;
}
[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

下面是我返回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;
}

如果您的配置中有这样的连接字符串,我们将不胜感激

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

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


尝试检查您正在检查的
AppSettings
中的内容,正如凯恩所指出的,这不是在配置文件中放置连接字符串的常用位置。显示您的appsettings@HABO应用程序设置有什么问题?这也是代码不起作用的一个原因?@Muflix
AppSettings
没有错,只是连接字符串通常如凯恩的回答所示。只要代码在定义它的配置文件部分中查找值,那么所有内容都匹配。