im trying to insert some data into my database by using dataGridView and filling it cells but i get the following error System.ArgumentException: "Keyword not supported: "charset"."
Here is my code:
public string connect = "Server=localhost;Database=hardlight;Uid=root;pwd=platon6993;charset=utf8";
string StrQuery;
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i )
{
StrQuery = @"INSERT INTO rabota VALUES ("
dataGridView1.Rows[i].Cells["id"].Value ", "
dataGridView1.Rows[i].Cells["name"].Value ", "
dataGridView1.Rows[i].Cells["dadada"].Value ");";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
what can i do? Please help
CodePudding user response:
Remove charset, your connection string should be like this
public string connect = "Server=localhost;Database=hardlight;Uid=root;Pwd=platon6993;";
you query string should contain ' ' for string values
StrQuery = $"INSERT INTO rabota VALUES ( {dataGridView1.Rows[i].Cells["id"].Value},'{dataGridView1.Rows[i].Cells["name"].Value}', '{dataGridView1.Rows[i].Cells["dadada"].Value}')";
