Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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# 尚未初始化连接属性调用ExecuteScalar时出错_C#_Sql Server - Fatal编程技术网

C# 尚未初始化连接属性调用ExecuteScalar时出错

C# 尚未初始化连接属性调用ExecuteScalar时出错,c#,sql-server,C#,Sql Server,C代码: SqlConnection c; string str = "Data Source =(LocalDB)\\MSSQLLocalDB;"; str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;"; str += "Integrated Security= True"; c = new SqlConnection(str); SqlCommand getxp = new SqlCommand("SELECT xp FROM

C代码:

SqlConnection c;

string str = "Data Source =(LocalDB)\\MSSQLLocalDB;";
str += "AttachDbFilename=|DataDirectory|\\DinoData.mdf;";
str += "Integrated Security= True";
c = new SqlConnection(str);

SqlCommand getxp = new SqlCommand("SELECT xp FROM [User] Where Username = @username");
getxp.Parameters.AddWithValue("@username", (string)Session["CurentUserid"]);

c.Open();
Session["xp"] = (int)getxp.ExecuteScalar();
c.Close();

if ((int)Session["xp"]<200)
{
    Response.Redirect("Must-Had.aspx", true);
}
错误:

ExecuteScalar:尚未初始化连接属性

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

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


您需要将连接分配给SqlCommand。像这样:

SqlCommand getxp = new SqlCommand("SELECT xp FROM [User] Where Username = @username", c);
或:


@OzCohen你的错误是没有阅读邮件。它就是这么说的。
getxp.Connection = c;