C# 如果第一个查询结果=第二个查询结果
我尝试使用If语句进行两个查询。如果查询1=查询2C# 如果第一个查询结果=第二个查询结果,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
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;
}