944,196 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2638
  • C# RSS
Nov 4th, 2009
0

MySQL insert from C# App.

Expand Post »
I've spent a bit of time porting my database connection over to MySQL. It's taken a bit of time, but generally been fairly easy. However, one of my insert queries is currently failing due to an auto increment ID field. I have this insert query:

C# Syntax (Toggle Plain Text)
  1. INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(@txt, @nm, @scrNm, @uid, @posi_score)

This table also has an entry_id field that is auto Increment. The query runs fine when I use it in PHPMyAdmin, but when debugging my application, I get an exception telling me that entry_id cannot be null.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
benkyma is offline Offline
34 posts
since Sep 2009
Nov 4th, 2009
1
Re: MySQL insert from C# App.
Ok, I figured out that the error was actually triggered further down the code. It seems none of my parameters are being added into the query. I'm going to post the rest in case anyone can see what is wrong with it...
C# Syntax (Toggle Plain Text)
  1. OdbcCommand myCommand = myConnection.getCommand("INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(@txt, @nm, @scrNm, @uid, @posi_score)");
  2. myCommand.Parameters.AddWithValue("@txt", txt);
  3. myCommand.Parameters.AddWithValue("@nm", nm);
  4. myCommand.Parameters.AddWithValue("@scrNm", scrNm);
  5. myCommand.Parameters.AddWithValue("@uid", uid);
  6. myCommand.Parameters.AddWithValue("@posi_score", posiScore);
  7. myConnection.openConnection();
  8. try
  9. {
  10. myConnection.executeNonQuery(myCommand);
  11. }
  12. catch (Exception e)
  13. {
  14. Console.Write("Insert failed on entries "+e.Message);
  15. }

Is there another way to pass these parameters in for an Odbc connection? I worked fine with a straight SQL server connection...
Reputation Points: 9
Solved Threads: 0
Light Poster
benkyma is offline Offline
34 posts
since Sep 2009
Nov 5th, 2009
1
Re: MySQL insert from C# App.
Click to Expand / Collapse  Quote originally posted by benkyma ...
Ok, I figured out that the error was actually triggered further down the code. It seems none of my parameters are being added into the query. I'm going to post the rest in case anyone can see what is wrong with it...
C# Syntax (Toggle Plain Text)
  1. OdbcCommand myCommand = myConnection.getCommand("INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(@txt, @nm, @scrNm, @uid, @posi_score)");
  2. myCommand.Parameters.AddWithValue("@txt", txt);
  3. myCommand.Parameters.AddWithValue("@nm", nm);
  4. myCommand.Parameters.AddWithValue("@scrNm", scrNm);
  5. myCommand.Parameters.AddWithValue("@uid", uid);
  6. myCommand.Parameters.AddWithValue("@posi_score", posiScore);
  7. myConnection.openConnection();
  8. try
  9. {
  10. myConnection.executeNonQuery(myCommand);
  11. }
  12. catch (Exception e)
  13. {
  14. Console.Write("Insert failed on entries "+e.Message);
  15. }

Is there another way to pass these parameters in for an Odbc connection? I worked fine with a straight SQL server connection...
As I understand it, ODBC parameters are positional, not named. So, you would need to have something like this, making sure your parameters are added in the same order as they appear in your SQL statement:

C# Syntax (Toggle Plain Text)
  1. OdbcCommand myCommand =
  2. new OdbcCommand("INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(?,?,?,?,?)");
  3. myCommand.Parameters.AddWithValue("txt", txt);
  4. myCommand.Parameters.AddWithValue("nm", nm);
  5. myCommand.Parameters.AddWithValue("scrNm", scrNm);
  6. myCommand.Parameters.AddWithValue("uid", uid);
  7. myCommand.Parameters.AddWithValue("posi_score", posiScore);

EDIT: You may want to consider using a different library to connect to MySQL. I am using MySql.Data.dll, which you can find that and others available free on the internet.
Last edited by DdoubleD; Nov 5th, 2009 at 7:32 pm.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Nov 6th, 2009
0
Re: MySQL insert from C# App.
I moved this to the MySql library yesterday and it seems to work well. That's good to know though. Thanks
Reputation Points: 9
Solved Threads: 0
Light Poster
benkyma is offline Offline
34 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Field Initialiser problem
Next Thread in C# Forum Timeline: SQL Query to generate a Crystal Report using Ms Access database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC