Help with insert using MySql

T

trant

I have having difficulty figuring out how to insert into a MySql database
using C# and MySql Connector.

I looked over several tutorials now I I seem to have everything correct but
for some reason values never get inserted into my table, everything is NULL!

here is my code:

MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = new MySqlCommand("INSERT INTO mytable (name) VALUES
(@name)", connection);
command.Parameters["@name"].Value = "test";
command.ExecuteNonQuery();


It inserts a row into my table but everything is NULL.

If I run this same query right into a mysql console (substituting the
parameter of course_ it works fine.

What did I do wrong?
 
A

Arne Vajhøj

I have having difficulty figuring out how to insert into a MySql database
using C# and MySql Connector.

I looked over several tutorials now I I seem to have everything correct but
for some reason values never get inserted into my table, everything is NULL!

here is my code:

MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = new MySqlCommand("INSERT INTO mytable (name) VALUES
(@name)", connection);
command.Parameters["@name"].Value = "test";
command.ExecuteNonQuery();

It inserts a row into my table but everything is NULL.

If I run this same query right into a mysql console (substituting the
parameter of course_ it works fine.

What did I do wrong?

Try:

MySqlCommand command = new MySqlCommand("INSERT INTO mytable (name)
VALUES (?name)", connection);
command.Parameters.Add("?name", MySqlDbType.VarChar, 50);
command.Parameters["?name"].Value = "test";

That is ?xxx instead of @xxx.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top