C# 使用MySqlDataReader更正查询不起作用

C# 使用MySqlDataReader更正查询不起作用,c#,mysql,C#,Mysql,所以我想弄明白这一点,特别是我有一个使用PhpMyAdmin可以完美工作的查询: 我以这种方式设置查询: command.CommandText = "SELECT tt.team_id, " + "(CASE WHEN t.id IS NULL THEN 0 ELSE 1 END) as exist " + "FROM(SELECT @first as team_id @others) tt LEFT JOIN team t on t.id = tt.team_id " +

所以我想弄明白这一点,特别是我有一个使用PhpMyAdmin可以完美工作的查询:

我以这种方式设置查询:

command.CommandText = "SELECT tt.team_id, " +
    "(CASE WHEN t.id IS NULL THEN 0 ELSE 1 END) as exist " +
    "FROM(SELECT @first as team_id @others) tt LEFT JOIN team t on t.id = tt.team_id " +
    "WHERE t.id IS NULL OR " +
    "t.update_at < DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)";

command.Parameters.Add("@first", MySqlDbType.Int32).Value = teams.First().Id;
command.Parameters.Add("@others", MySqlDbType.String).Value = string.Concat(teams.Skip(1).Select(c => " UNION ALL SELECT " + c.Id));

有人能帮我吗?

这就是我如何构建一个动态参数列表以传递给您的查询的方法。 警告,未测试,但应产生预期输出

// Command text with a placeholder where we insert the dynamic text
string cmd = @"SELECT tt.team_id, 
                      (CASE WHEN t.id IS NULL THEN 0 ELSE 1 END) as exist 
               FROM (SELECT {texttoreplace}) tt  
               LEFT JOIN team t on t.id = tt.team_id WHERE t.id IS NULL 
                     OR t.update_at < DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)";

int prmCounter = 1;

// Where we keep the text to insert at the appropriate place
StringBuilder unions = new StringBuilder();
// Where we keep the parameters to add at the MySqlCommand
List<MySqlParameter> prms = new List<MySqlParameter>();

// First parameter
MySqlParameter pr = new MySqlParameter("@first", MySqlDbType.Int32) { Value = teams.First().id};
prms.Add(pr);
unions.Append($" @first as team_id ");

// Loop over your IDs and build parameters and text
foreach (var t in teams.Skip(1))
{
    // Giving an unique name to the parameter
    string placeholder = "@p" + prmCounter;
    unions.Append($" UNION ALL SELECT {placeholder}");
    pr = new MySqlParameter(placeholder, MySqlDbType.Int32) { Value = t.id};
    prms.Add(pr);
    prmCounter++;
}
// Add all the required parameters
command.Parameters.AddRange(prms.ToArray());
// Replace the placeholder with the built text
cmd = cmd.Replace("{texttoreplace}",  unions.ToString());

你想干什么?您的@first和@others参数没有意义。@Progman参数不能替代sql文本,它们只能用于将值传递给数据库引擎。您不能将参数化查询用作随机字符串替代。该参数是一个类型为的变量,而不是一段SQL。错误是因为@others被替换为字符串,所以在其周围加了引号。@iakobski噢,糟糕,我应该在UNION ALL SELECT周围加上反勾号吗?
command.CommandText = "SELECT tt.team_id, " +
    "(CASE WHEN t.id IS NULL THEN 0 ELSE 1 END) as exist " +
    "FROM(SELECT @first as team_id @others) tt LEFT JOIN team t on t.id = tt.team_id " +
    "WHERE t.id IS NULL OR " +
    "t.update_at < DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)";

command.Parameters.Add("@first", MySqlDbType.Int32).Value = teams.First().Id;
command.Parameters.Add("@others", MySqlDbType.String).Value = string.Concat(teams.Skip(1).Select(c => " UNION ALL SELECT " + c.Id));
// Command text with a placeholder where we insert the dynamic text
string cmd = @"SELECT tt.team_id, 
                      (CASE WHEN t.id IS NULL THEN 0 ELSE 1 END) as exist 
               FROM (SELECT {texttoreplace}) tt  
               LEFT JOIN team t on t.id = tt.team_id WHERE t.id IS NULL 
                     OR t.update_at < DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)";

int prmCounter = 1;

// Where we keep the text to insert at the appropriate place
StringBuilder unions = new StringBuilder();
// Where we keep the parameters to add at the MySqlCommand
List<MySqlParameter> prms = new List<MySqlParameter>();

// First parameter
MySqlParameter pr = new MySqlParameter("@first", MySqlDbType.Int32) { Value = teams.First().id};
prms.Add(pr);
unions.Append($" @first as team_id ");

// Loop over your IDs and build parameters and text
foreach (var t in teams.Skip(1))
{
    // Giving an unique name to the parameter
    string placeholder = "@p" + prmCounter;
    unions.Append($" UNION ALL SELECT {placeholder}");
    pr = new MySqlParameter(placeholder, MySqlDbType.Int32) { Value = t.id};
    prms.Add(pr);
    prmCounter++;
}
// Add all the required parameters
command.Parameters.AddRange(prms.ToArray());
// Replace the placeholder with the built text
cmd = cmd.Replace("{texttoreplace}",  unions.ToString());