error in my SQL syntax

Posted on 2011-03-16
Last Modified: 2012-05-11
Hi Guys,

I am at a loss if i run the following statement in MySQLworkbench it works fine. However when I try using the ODBC connection in c# i get the following error;

ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.56-community]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 'SELECT @@IDENTITY' at line 1

the SQL i am using is very simple;

connection = new OdbcConnection(connectionString);

            String oQRY = String.Format("INSERT INTO `message` (fromID,subject,message,date,important) VALUES ({0},'{1}','{2}','{3}',{4}); SELECT @@IDENTITY AS `identity`;", userID, SubjectTextBox.Text, MessageMemo.Text, timestamp, ImportantCheckBox.Value);
            //Response.Write(oQry + "<BR>")
            OdbcCommand com = new OdbcCommand(oQRY, connection);
            com.CommandText = "SELECT Last_Insert_ID()";
            int messageID = (int) com.ExecuteScalar();

the value of oQRY is

"INSERT INTO `message` (fromID,subject,message,date,important) VALUES (0,'test','test','2011-03-16 00:00:00',True); SELECT @@IDENTITY AS `identity`;"

any ideas whats going on here please?

Thanks in advance,

Question by:flynny
LVL 12

Accepted Solution

enachemc earned 500 total points
ID: 35145787
@@IDENTITY is for MS SQL only
you are using mysql
LVL 17

Expert Comment

ID: 35147037
Replace with this query

            String oQRY = String.Format("INSERT INTO `message` (fromID,subject,message,date,important) VALUES (SELECT @@IDENTITY AS `identity`,'{0}','{1}','{2}',{3});", userID, SubjectTextBox.Text, MessageMemo.Text, timestamp, ImportantCheckBox.Value);

Hope this helps

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now