我将尝试通过帐户创建方法创建一些规则。并查看您的SQL语法错误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的“ END”附近使用
string query1 = "IF account.char_count_max > 0 THEN INSERT INTO
playercharacter(id, connection_ID) VALUES((SELECT id FROM account WHERE
connection_ID='" + connectionID + "'), (SELECT connection_ID FROM account
WHERE
connection_ID='" + connectionID + "')) END";
MySqlCommand cmd = new MySqlCommand(query1,
MySQL.mySQLsettings.connection);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
答案 0 :(得分:0)
SqlCommand.ExecuteNonQuery通常用于直接执行INSERT,UPDATE或DELETE语句,而不是作为IF的一部分。考虑到这一点,我建议您将代码更改为:
if (accountCharCount > 0)
{
string query1 = "INSERT INTO playercharacter(id, connection_ID) VALUES((SELECT id FROM account WHERE connection_ID='" + connectionID + "'), (SELECT connection_ID FROM account WHERE connection_ID='" + connectionID + "'))";
using (MySqlCommand cmd = new MySqlCommand(query1, MySQL.mySQLsettings.connection))
{
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
}
}
因此,您需要先前对数据库的调用才能获取accountCharCount(account.char_count_max)。或者,您可以将所有SQL放入存储过程中,然后使用单个SqlCommand.ExecuteNonQuery调用它。
顺便说一句,您实际上应该对连接ID使用MySqlParameter,而不是文字字符串;这可以保护您免受SQL注入攻击,并且是一个很好的习惯。