GetCountry查询中我的SQL语法错误

时间:2015-05-29 08:24:39

标签: c# mysql visual-studio syntax-error

我的代码中出现语法错误。 adapter.Fill(dt);行后面的行adapter.SelectCommand.CommandText = @"SELECT c.*,(Select Initials FROM users....出现错误。该错误表示检查与您的MySQL服务器版本对应的手册,以便使用正确的语法,以便在附近使用c.CountryName'在第1行,但我不确定导致此错误的原因

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;
}

1 个答案:

答案 0 :(得分:0)

试试这个,您在=

时遗失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";