在ASP.NET中使用MySqlCommand的MySQL语法错误

时间:2018-06-01 14:56:36

标签: c# mysql asp.net

下面我已经测试过的SQL查询工作正常

Select 
    data_info.user_id, data_info.type_id, data_info.specific_title,
    data_info.content, life_area.type_id, life_area.type_id
From
    data_info 
Left Join
    life_area On data_info.type_id = life_area.id
Where 
    data_info.user_id = '0001'

现在我正在尝试将工作SQL转换为asp.net MySqlCommand,但它无法正常工作并显示语法错误

MySqlCommand cmd = new MySqlCommand(
    "Select data_info.user_id,data_info.type_id,data_info.specific_title,data_info.content,"+
    "life_area.type_id,life_area.type_id" +
    "FROM data_info LEFT JOIN life_area ON data_info.type_id = life_area.id" +
    "Where data_info.user_id='0001'" , conn);

1 个答案:

答案 0 :(得分:1)

你在关键区域没有空格的情况下将字符串组合在一起,例如life_area.type_id' + 'FROM data_info'为您提供life_area.type_idFROM data_info;这使你的SQL无效。使用@verbatim string)字符串来表示回车可能是最佳解决方案:

MySqlCommand cmd = new MySqlCommand(
    @"Select data_info.user_id,data_info.type_id,data_info.specific_title,data_info.content,
    life_area.type_id,life_area.type_id
    FROM data_info LEFT JOIN life_area ON data_info.type_id = life_area.id 
    Where data_info.user_id='0001'" , conn);