gpt4 book ai didi

c# - 并发冲突 : the DeleteCommand affected 0 of the expected 1 records

转载 作者:可可西里 更新时间:2023-11-01 06:30:34 24 4
gpt4 key购买 nike

我正在尝试从 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`)
)

最佳答案

我知道这是一个旧线程,但我遇到了同样的问题并找到了解决方案。也许其他人会在这里看到它。

使用 Visual Studio,在 .xsd 文件中,数据适配器中有一个 DELETE 命令。该命令的形式为 SQL“DELETE from table where column1 = @Original_column1 AND column2 = @Original_column2 AND...”默认情况下,它将所有列与您读取数据库时的值进行比较,并且不会删除记录,除非它们都匹配。这是为了避免在您阅读记录和尝试更改记录之间覆盖其他人所做的更改。

问题来了。如果这些列中的任何一个在数据库中为 NULL,它将不匹配并按照您的描述出错。

PS:在使用 VS db 库时,除了在 DB 中允许 NULL 之外,我什么都没有。 LINQ2SQL 或其他名称...

关于c# - 并发冲突 : the DeleteCommand affected 0 of the expected 1 records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10711342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com