| | |
MySQL insert from C# App.
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 30
Reputation:
Solved Threads: 0
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:
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.
C# Syntax (Toggle Plain Text)
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.
•
•
Join Date: Sep 2009
Posts: 30
Reputation:
Solved Threads: 0
1
#2 Nov 4th, 2009
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...
Is there another way to pass these parameters in for an Odbc connection? I worked fine with a straight SQL server connection...
C# Syntax (Toggle Plain Text)
OdbcCommand myCommand = myConnection.getCommand("INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(@txt, @nm, @scrNm, @uid, @posi_score)"); myCommand.Parameters.AddWithValue("@txt", txt); myCommand.Parameters.AddWithValue("@nm", nm); myCommand.Parameters.AddWithValue("@scrNm", scrNm); myCommand.Parameters.AddWithValue("@uid", uid); myCommand.Parameters.AddWithValue("@posi_score", posiScore); myConnection.openConnection(); try { myConnection.executeNonQuery(myCommand); } catch (Exception e) { Console.Write("Insert failed on entries "+e.Message); }
Is there another way to pass these parameters in for an Odbc connection? I worked fine with a straight SQL server connection...
•
•
Join Date: Jul 2009
Posts: 956
Reputation:
Solved Threads: 199
1
#3 Nov 5th, 2009
•
•
•
•
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)
OdbcCommand myCommand = myConnection.getCommand("INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(@txt, @nm, @scrNm, @uid, @posi_score)"); myCommand.Parameters.AddWithValue("@txt", txt); myCommand.Parameters.AddWithValue("@nm", nm); myCommand.Parameters.AddWithValue("@scrNm", scrNm); myCommand.Parameters.AddWithValue("@uid", uid); myCommand.Parameters.AddWithValue("@posi_score", posiScore); myConnection.openConnection(); try { myConnection.executeNonQuery(myCommand); } catch (Exception e) { Console.Write("Insert failed on entries "+e.Message); }
Is there another way to pass these parameters in for an Odbc connection? I worked fine with a straight SQL server connection...
C# Syntax (Toggle Plain Text)
OdbcCommand myCommand = new OdbcCommand("INSERT INTO entries(text, name, screen_name, user_id, posi_score)values(?,?,?,?,?)"); myCommand.Parameters.AddWithValue("txt", txt); myCommand.Parameters.AddWithValue("nm", nm); myCommand.Parameters.AddWithValue("scrNm", scrNm); myCommand.Parameters.AddWithValue("uid", uid); 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.
![]() |
Similar Threads
- html form to php mysql db insert for image (PHP)
- wxPython, MySQL, SQLAlchemy app Problem (Python)
- php and mysql insert...where making me tear my hairs out!!! help!!! (PHP)
- MySQL INSERT not working. Why? (MySQL)
- Its really a problem on INSERT statement (MySQL)
- MySQL insert statement (MySQL)
- MySQL-Front - Graphical Interface for MySQL (MySQL)
Other Threads in the C# Forum
- Previous Thread: Field Initialiser problem
- Next Thread: SQL Query to generate a Crystal Report using Ms Access database
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





