Ke Vin
Ke Vin

Reputation: 3750

Why inserting DateTime to MySQL in C# is getting an error?

I Try to insert to MySQL database in C#, but what i got is this error

"You have an error in your SQL syntax; check the manual that corresponds to your Mysql server version for the right syntax to use near "insert (NoNota, Nama, Tanggal, Tipe, Keterangan) VALUES ('1111', 'Kickass', '201" at line 1

i think the problem is the DateTime, in my database datatype i set it to DATETIME, Here is my code

        string sqlQuery;

        sqlQuery = "INSERT INTO insert (id, Name, Date, Type, Notes) VALUES ('1111', 'Kickass', '2013-09-09', 'Cash', 'Nothing')";


        if (this.OpenConnection() == true)
        {
            cmd = new MySqlCommand(sqlQuery, connect);
            cmd.ExecuteNonQuery();
            CloseConnection();
            MessageBox.Show("Operation INSERT is SUCCESS!!");
        }

What's wrong with it? i try excute my SQL Queries it work very FINE in MySQL Workbench, it automatically convert the DateTime and insert it into the table. Any clue?

Upvotes: 0

Views: 296

Answers (2)

juergen d
juergen d

Reputation: 204766

You need to escape reserved words in MySQL like INSERT with backticks

INSERT INTO `insert` (id, Name, ...
            ^------^---------------------here

But it would be way better to rename your table. insert does not say anything about your data. Try to think of its content. When you have hundreds of tables in a database you need to name every one very carefully to keep track what it contains.

When you name your column, name it after the single word that finishes this sentance:

The data of my table holds ...

Upvotes: 0

Kevin Seifert
Kevin Seifert

Reputation: 3572

insert is a reserved word (INSERT INTO insert). Rename the table or escape with backticks. I'd highly recommend renaming.

Upvotes: 3

Related Questions