GetCountry 查询中我的 SQL 语法错误

error in my SQL syntax in GetCountry query

我的代码中出现语法错误。在 adapter.SelectCommand.CommandText = @"SELECT c.*,(Select Initials FROM users.... 行之后的 adapter.Fill(dt); 行出现错误。该错误说检查与您的 MySQL 服务器版本相对应的手册,以了解在第 1 行的“237 ORDER BY c.CountryName”附近使用的正确语法,但我不确定是什么导致了此错误

public static string GetCountry(int CountryID)
{
    string Country = "";

    string sql = "proc_GetCountries";

    DataTable dt = new DataTable();

    using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
    {
        adapter.SelectCommand.CommandType = CommandType.Text;
        adapter.SelectCommand.CommandText = @"SELECT * FROM countries WHERE ID = ?countryID ORDER BY CountryName";
        adapter.SelectCommand.Parameters.Add(new MySqlParameter("countryID", CountryID));
        adapter.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Country = dt.Rows[0]["CountryName"].ToString();
        }
    }
    return Country;
}
public static DataSet GetCountry(int Company_ID, int ID)
{
    DataSet ds = new DataSet();

    string sql = "proc_GetCountry";

    DataTable dt = new DataTable(), dtRates = new DataTable(), dtDepots = new DataTable();

    using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
    {
        adapter.SelectCommand.CommandType = CommandType.Text;
        adapter.SelectCommand.CommandText = @"SELECT c.*,(Select Initials FROM users WHERE User_ID = c.CreatedByUser) AS CreatedBy, (SELECT Initials FROM users WHERE User_ID = c.ModifiedByUser) AS ModifiedBy " +
            "FROM countries c WHERE c.Company_ID = ?company_ID AND c.ID ?iD ORDER BY c.CountryName";
        adapter.SelectCommand.Parameters.Add(new MySqlParameter("company_ID", Company_ID));
        adapter.SelectCommand.Parameters.Add(new MySqlParameter("iD", ID));
        adapter.Fill(dt);
        ds.Tables.Add(dt);
    }
    //Rates
    using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
    {
        adapter.SelectCommand.CommandType = CommandType.Text;
        adapter.SelectCommand.CommandText = @"SELECT cc.* FROM country_rates cc WHERE cc.CountryID ?iD";
        adapter.SelectCommand.Parameters.Add(new MySqlParameter("iD", ID));
        adapter.Fill(dtRates);
        ds.Tables.Add(dtRates);
    }
    //Depots
    using (MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(sql, DataUtils.ConnectionStrings["TAT"]))
    {
        adapter.SelectCommand.CommandType = CommandType.Text;
        adapter.SelectCommand.CommandText = @"SELECT cc.* FROM country_depot cc WHERE cc.CountryID = ?iD";
        adapter.SelectCommand.Parameters.Add(new MySqlParameter("iD", ID));
        adapter.Fill(dtDepots);
        ds.Tables.Add(dtDepots);
    }
    return ds;
}

试试这个,您在 c.ID ?iD 缺少 =:

adapter.SelectCommand.CommandText = @"SELECT c.*,(Select Initials FROM users WHERE User_ID = c.CreatedByUser) AS CreatedBy, (SELECT Initials FROM users WHERE User_ID = c.ModifiedByUser) AS ModifiedBy " +
                   "FROM countries c WHERE c.Company_ID = ? company_ID AND c.ID = ? iD ORDER BY c.CountryName";