Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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# 如果第一个查询结果=第二个查询结果_C#_Sql_Asp.net_Asp.net Mvc_Visual Studio 2010 - Fatal编程技术网

C# 如果第一个查询结果=第二个查询结果

C# 如果第一个查询结果=第二个查询结果,c#,sql,asp.net,asp.net-mvc,visual-studio-2010,C#,Sql,Asp.net,Asp.net Mvc,Visual Studio 2010,我尝试使用If语句进行两个查询。如果查询1=查询2 string select = "Select ProfileId from Project_list Where ProjectId = @ProjectId"; using (SqlConnection myConnection = new SqlConnection(connectionString)) { myConnection.Open(); SqlC

我尝试使用If语句进行两个查询。如果查询1=查询2

string select = "Select ProfileId from Project_list Where ProjectId = @ProjectId";
        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {

            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(select, myConnection);
            myCommand.Parameters.AddWithValue("@ProjectId", querystring);
            object Project_listResult = myCommand.ExecuteScalar();
        }


        string getProfileId = "SELECT ProfileId FROM User_Profile WHERE UserId = (@UserId)";
        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {
            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(getProfileId, myConnection);
            myCommand.Parameters.AddWithValue("@UserId", currentUserId);
            object User_profileResult= myCommand.ExecuteScalar();
        }


        if (Project_listResult == User_profileResult)
        {
            addFollowerButton.Visible = true;

        }
这是我的代码,但不起作用

错误18当前数据库中不存在名称“Project\u listResult”


错误19当前数据库中不存在名称“User_profileResult”

如果我了解您正在尝试比较对象。所以你得试试

 if (Project_listResult.Equals(User_profileResult))
            addFollowerButton.Visible = true;

您必须在外部定义这两个值,以便在范围内使用它们。现在,您正在使用中定义这两个值,因此这些值仅在only-under的范围内,在该范围之外不可用,因此您将得到该错误

一个简单的建议是,在使用select作为变量名时,避免使用属于不同语言的关键字。这有助于提高可读性并增加混乱

object Project_listResult = null;
object User_profileResult = null;
using (SqlConnection myConnection = new SqlConnection(connectionString))
        {

            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(select, myConnection);
            myCommand.Parameters.AddWithValue("@ProjectId", querystring);
            Project_listResult = myCommand.ExecuteScalar();
        }


        string getProfileId = "SELECT ProfileId FROM User_Profile WHERE UserId = (@UserId)";
        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {
            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(getProfileId, myConnection);
            myCommand.Parameters.AddWithValue("@UserId", currentUserId);
            User_profileResult= myCommand.ExecuteScalar();
        }


        if (Project_listResult.Equals(User_profileResult))
        {
            addFollowerButton.Visible = true;

        }