Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# with MySQL INSERT parameters

Good day to all, I'm using Visual C# 2010 and MySQL Version 5.1.48-community. I hope you can help me with this code. I don't find it working on me. What am I missing?

string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; MySqlConnection conn = new MySqlConnection(connString); conn.Open(); MySqlCommand comm = conn.CreateCommand(); comm.CommandText = "INSERT INTO room(person,address) VALUES(@person, @address)"; comm.Parameters.Add("@person", "Myname"); comm.Parameters.Add("@address", "Myaddress"); comm.ExecuteNonQuery(); conn.Close(); 

And when I try to compile it. It says:

Person column cannot be null

EDITED:

But when I try this code.

comm.CommandText = "INSERT INTO room(person,address) VALUES('Myname', 'Myaddress')"; 

But this code is prone to sql injection attack but it works, doesn't gives me an error.

EDITED:

I tried to use this. I found it here so I thought It would work but gives me this error

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Any idea?

    string a = "myname";     MySqlCommand cmd = new MySqlCommand();     cmd.Connection = conn;     cmd.CommandText = "INSERT INTO room(person,address) VALUES(?,?)";     //cmd.Prepare();      cmd.Parameters.Add("person", MySqlDbType.VarChar).Value = a;     cmd.Parameters.Add("address", MySqlDbType.VarChar).Value = "myaddress";     cmd.ExecuteNonQuery(); // HERE I GOT AN EXCEPTION IN THIS LINE 

Any help would be much appreciated.

EDITED: SOLVED I used this code:

cmd.CommandText = "INSERT INTO room(person,address) VALUES(?person,?address)"; cmd.Parameters.Add("?person", MySqlDbType.VarChar).Value = "myname"; cmd.Parameters.Add("?address", MySqlDbType.VarChar).Value = "myaddress"; cmd.ExecuteNonQuery(); 

Thanks SO!

like image 271
Boy Karton Avatar asked Apr 23 '13 11:04

Boy Karton


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

You may use AddWithValue method like:

string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; MySqlConnection conn = new MySqlConnection(connString); conn.Open(); MySqlCommand comm = conn.CreateCommand(); comm.CommandText = "INSERT INTO room(person,address) VALUES(@person, @address)"; comm.Parameters.AddWithValue("@person", "Myname"); comm.Parameters.AddWithValue("@address", "Myaddress"); comm.ExecuteNonQuery(); conn.Close(); 

OR

Try with ? instead of @, like:

string connString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; MySqlConnection conn = new MySqlConnection(connString); conn.Open(); MySqlCommand comm = conn.CreateCommand(); comm.CommandText = "INSERT INTO room(person,address) VALUES(?person, ?address)"; comm.Parameters.Add("?person", "Myname"); comm.Parameters.Add("?address", "Myaddress"); comm.ExecuteNonQuery(); conn.Close(); 

Hope it helps...

like image 81
Rahul Avatar answered Sep 20 '22 04:09

Rahul


Use the AddWithValue method:

comm.Parameters.AddWithValue("@person", "Myname"); comm.Parameters.AddWithValue("@address", "Myaddress"); 
like image 24
Hossein Narimani Rad Avatar answered Sep 21 '22 04:09

Hossein Narimani Rad