| | |
MySQL insert from C# App.
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 29
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: 29
Reputation:
Solved Threads: 0
1
#2 32 Days Ago
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: 920
Reputation:
Solved Threads: 147
1
#3 30 Days Ago
•
•
•
•
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; 30 Days Ago 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 |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# cast check checkbox 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+ httpwebrequest image index input install java label list listbox listener mailmerge mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox robot save saving serialization server sleep socket sockets sql sql-server statistics stream string stringformatting sun table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





