【发布时间】:2012-05-29 11:50:52
【问题描述】:
我正在尝试从 datagridview 中删除一条记录,然后将其更新到 mysql 服务器,但是我一直收到“并发冲突:DeleteCommand 影响了预期的 1 条记录中的 0 条。”。我已经用谷歌搜索并弄乱了它,无法找到解决爱情和金钱的方法。我哪里错了?
private void Form1_Load(object sender, EventArgs e)
{
try
{
// create a connection to the server
connection = new MySqlConnection("SERVER=" + Constants.SERVER + ";" + "DATABASE=" + Constants.DATABASE + ";UID=" + Constants.USERNAME + ";" + "PASSWORD=" + Constants.PASSWORD + ";");
connection.Open();
// create our default handler
adapter = new MySqlDataAdapter();
// set the default commands to do
adapter.SelectCommand = new MySqlCommand("SELECT * FROM npc_drops", connection);
MySqlCommand query = new MySqlCommand("DELETE FROM npc_drops WHERE id = @id", connection);
query.Parameters.Add("@id", MySqlDbType.Int32, 10, "id");
adapter.DeleteCommand = query;
// create a table to put our data in and fill it with the results
table = new DataTable();
adapter.Fill(table);
// bind both the data table and the datagridview togeather
BindingSource source = new BindingSource();
source.DataSource = table;
dataGridView1.DataSource = source;
dataGridView1.Columns["id"].Visible = false;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
table = table.GetChanges();
adapter.Update(table);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
表:
CREATE TABLE `npc_drops` (
`id` INT(10) NULL AUTO_INCREMENT,
`npcs` VARCHAR(250) NULL DEFAULT NULL,
`rate` INT(3) NULL DEFAULT NULL,
`item` INT(5) NULL DEFAULT NULL,
`amount` INT(10) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
【问题讨论】:
-
@BizApps 谢谢我已经更新了那行,但仍然收到同样可怕的错误!
-
- 你的表使用的是 INNODB 还是 MISAM? - 你在使用自动提交吗?
-
MySQL 不要求参数以 : 为前缀,即 :id 吗?