0

I’m getting this error in Visual Studio 2010 C# project. I used the Mysql driver version 6.8.3.0 to made the connection to mysql server(which created via mysql workbench 6.1) and the connection to mysql server established successful(which created via visual studio) but error comes in this line when my software runs. I search in the internet found that remote connection problem is the issue. But if my connection established fine this cannot be the issue and according to the error its saying that some issue in sql query. But this is a simple select statement(SELECT * FROM case).Any solution or suggestion is appreciated.

using (MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader()) in the following code section.

String connectionString = "server = localhost; user id = XXX; password = XXX;  database = XXXX";

        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        String searchString = "SELECT * FROM case" ;
         using (MySqlCommand mySqlCommand = new MySqlCommand(searchString, connection))
            {
                // mySqlCommand.Parameters.AddWithValue("@caseId", textBox1.Text);
                using (MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
                {
                    String sb;
                    sb = mySqlDataReader.GetInt32(0).ToString();
                    MessageBox.Show(sb);

The part of error i'm getting is this.

 MySql.Data.MySqlClient.MySqlException was unhandled
 Message=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 'case' at line 1 
 Source=MySql.Data
ErrorCode=-2147467259 Number=1064 StackTrace:
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at CaseFinder.Form1.button1_Click(Object sender, EventArgs e) in
  • 1
    case is a keyword in mysql – Sajeetharan Jul 05 '14 at 07:41
  • Thanx Sajeetharan I did not know that. That could be the reason for error to be occur. – user2474358 Jul 05 '14 at 07:43
  • That and your sql query is missing a terminating semicolon. MySql requires ; at the end of each operation. Change to String searchString = "SELECT * FROM mycase;"; //renamed case to mycase change table name – Ryan Mann Jul 05 '14 at 07:47
  • 1
    @Ryios semicolon isn't mandatory in single SQL command sent from code, related discussion : [Can you form legal statement in MySQL without using semicolon?](http://stackoverflow.com/questions/14764492/can-you-form-legal-statement-in-mysql-without-using-semicolon) – har07 Jul 05 '14 at 08:06
  • Good to know, I don't know that I've ever sent a single command, hince why I thought it was required. – Ryan Mann Jul 06 '14 at 03:49

1 Answers1

0

CASE is a keyword in MySQL. Try to wrap it with backticks (`) if you meant to select from a table named "case", for example :

String searchString = "SELECT * FROM `case`" ;
har07
  • 88,338
  • 12
  • 84
  • 137