Skip to content

Instantly share code, notes, and snippets.

@arsho
Last active November 10, 2016 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsho/fdbd3f384fd4517701b703a11b1786d3 to your computer and use it in GitHub Desktop.
Save arsho/fdbd3f384fd4517701b703a11b1786d3 to your computer and use it in GitHub Desktop.
C# MySQL Data insertion using mysql-connector-net-5.1.7-noinstall, Visual Studio 2015.
string connetionString = null;
connetionString = "server=localhost;database=device_db;uid=root;pwd=123;";
using (MySqlConnection cn = new MySqlConnection(connetionString))
{
try
{
string query = "INSERT INTO test_table(user_id, user_name) VALUES (?user_id,?user_name);";
cn.Open();
using (MySqlCommand cmd = new MySqlCommand(query, cn))
{
cmd.Parameters.Add("?user_id", MySqlDbType.Int32).Value = 123;
cmd.Parameters.Add("?user_name", MySqlDbType.VarChar).Value = "Test username";
cmd.ExecuteNonQuery();
}
}
catch (MySqlException ex)
{
MessageBox.Show("Error in adding mysql row. Error: "+ex.Message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment