Taco2
Taco2

Reputation: 439

C# Mysql create a table name with spaces

I have searched quite a bit for this without luck, my question is simple, I have a class that looks like this:

    public static void Mysql_CreateTable(string tableSegment, string ConString)
    {
        string strCreate = "CREATE TABLE " + tableSegment + " (CVR text,Navn text, Firma text, Nummer text, Addresse text, Postnr text, Bynavn text, Noter text, Email text, LastCallDato text, NextCallDato text, CallStatus text, MailSendt text, UniqueID text);";
        using (MySqlConnection conDatabase = new MySqlConnection(ConString))
        {
            using (MySqlCommand cmdDatabase = new MySqlCommand(strCreate, conDatabase))
            {
                conDatabase.Open();
                cmdDatabase.ExecuteNonQuery();
                conDatabase.Close();
            }
        }
    }

When tableSegment contains a space like this "table 1" I get a Mysql error saying:

you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax......

So, what am I doing wrong? Why can't I enter space into the table name?

Upvotes: 0

Views: 175

Answers (1)

Amit Joshi
Amit Joshi

Reputation: 16389

Do this:

string strCreate = "CREATE TABLE `" + tableSegment + "` (CVR..............

Upvotes: 2

Related Questions