Editing datagridview. Visual C# 2008
本文关键字:2008 Visual datagridview Editing | 更新日期: 2023-09-27 17:50:20
我有以下代码:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
string cellid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); //to działa, ale nie przenosi na %s
MySqlConnection mysqlCon = new MySqlConnection("SERVER = naven.com.pl; DATABASE = naven1_terminarz; User ID = naven1_terminarz; PASSWORD = mydbpassword;");
MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = '@VALUE',`Tekst` = 'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 'dsa' WHERE `Terminarz`.`id` = '@ID' LIMIT 1 ;", mysqlCon);
//MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = '@VALUE',`Tekst` = 'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` = 'dsa' WHERE `Terminarz`.`id` = '@ID' LIMIT 1 ;", mysqlCon);
cmd.Parameters.AddWithValue("@ID", MySqlDbType.Int32).Value = Int32.Parse(cellid);
cmd.Parameters.AddWithValue("@VALUE", MySqlDbType.VarChar).Value = value;
try
{
mysqlCon.Open();
cmd.ExecuteNonQuery();
mysqlCon.Close();
//MessageBox.Show(cmd.ToString());
//MessageBox.Show("@ID @VALUE"); //this one displays @ID @VALUE instead of variable value.
MessageBox.Show(cellid); //this one is working fine, but i don't know how to input this value into MySql query
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MySql代码本身是工作的,但是@ID和@VALUE没有正确显示,它只是说@ID而不是整数和@VALUE而不是我的字符串值。我重写了这段代码:naven.com.pl/cellenddit .jpg它看起来是一样的(或者它是吗?),这对作者来说是有效的,对我来说不是。
可以在代码中读取MessageBox.Show (cellid);正在工作,但它不工作与@ID @VALUE变量
我通过输入正确的值而不是变量来检查查询,并且它正在使用此代码。
原因是什么?
Try
MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.
`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = @VALUE,`Tekst` =
'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` =
'dsa' WHERE `Terminarz`.`id` = @ID LIMIT 1 ;", mysqlCon);
更正:从@VALUE
和@ID
中删除引号''
问题:你把你的命名参数用单引号括起来了
解决方案:你需要去掉下面两个参数的单引号
@VALUE
@ID
替换:
MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.
`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = '@VALUE',`Tekst` =
'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` =
'dsa' WHERE `Terminarz`.`id` = '@ID' LIMIT 1 ;", mysqlCon);
与这个: MySqlCommand cmd = new MySqlCommand("UPDATE `naven1_terminarz`.
`Terminarz` SET `Data` = '2014-03-06 00:00:00',`Autor` = @VALUE,`Tekst` =
'dadsssldhkashdjasjkdkasjdhasdkas',`Użytkownik` = 'Marcina', `Klient` =
'dsa' WHERE `Terminarz`.`id` = @ID LIMIT 1 ;", mysqlCon);