epiphany
epiphany

Reputation: 766

MySQL syntax error using MySqlCommand in ASP.NET

Below I have the SQL query which I have tested is working fine

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'

Now I am trying to convert the working SQL into asp.net MySqlCommand, but it fails to work and show syntax error

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

Upvotes: 0

Views: 94

Answers (1)

Liam
Liam

Reputation: 29704

You’re combining the strings together without spaces in crucial areas, e.g. life_area.type_id' + 'FROM data_info' gives you life_area.type_idFROM data_info; this is making your SQL invalid. Using a @(verbatim string) string to honour the carriage returns is likely the best solution:

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

Upvotes: 1

Related Questions