gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:15:15 27 4
gpt4 key购买 nike

我是 c# 新手,尝试将 datagridview 绑定(bind)到 visual studio 2010 中的 mssql 数据库。数据绑定(bind)正常,一切似乎都正常。除了一些奇怪的错误:

我在以下之后得到了主题中的错误:更新同一行 2 次,删除新插入的行,在删除另一行时更新一行之后(单词更改为 DeleteCommand)

我在 Google 上找到的所有解决方案都不适合我。我希望有人可以帮助我。这是代码:

    private void fillDatagrid()
{
//fill datagrid ADO.NET
conn = new SqlConnection(TestApp.Properties.Settings.Default.TestdatabaseConnectionString);
cmd = conn.CreateCommand();
conn.Open();
cmd.CommandText = "SelectFrom";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@table", SqlDbType.NVarChar, 50).Value = "Countries";
cmd.Parameters.Add("@filters", SqlDbType.NVarChar, 300).Value = "";

adapt = new SqlDataAdapter(cmd);
dt = new DataTable();
adapt.Fill(dt);
dt.TableName = "Countries";

conn.Close();

BindingSource src = new BindingSource();
src.DataSource = dt;
dt.RowChanged += new DataRowChangeEventHandler(dt_RowChanged);

dgDatabaseGrid.DataSource = src;
dgDatabaseGrid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
//dgDatabaseGrid.RowValidating += new DataGridViewCellCancelEventHandler(dgDatabaseGrid_RowValidating);

//disable columns:
dgDatabaseGrid.Columns[0].Visible = false;
dgDatabaseGrid.Columns["date_insert"].Visible = false;
dgDatabaseGrid.Columns["user_insert"].Visible = false;
dgDatabaseGrid.Columns["date_change"].Visible = false;
dgDatabaseGrid.Columns["user_change"].Visible = false;
dgDatabaseGrid.Columns["deleted"].Visible = false;

//auto size last column
dgDatabaseGrid.Columns["remarks"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


SqlCommandBuilder cb = new SqlCommandBuilder(adapt);
}

void dt_RowChanged(object sender, DataRowChangeEventArgs e)
{
try
{
adapt.Update(dt);
}
catch (SqlException ex)
{
Debug.WriteLine(ex.Message);
}
}

private void dgDatabaseGrid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
if (!e.Row.IsNewRow)
{


DialogResult response = MessageBox.Show("Are you sure?", "Delete row?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);

if (response == DialogResult.Yes)
{

//ipv delete --> deleted=1
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "DeleteFrom";
cmd.Parameters.Add("@table", SqlDbType.NVarChar, 50).Value = "Countries";
cmd.Parameters.Add("@id", SqlDbType.Int).Value = e.Row.Cells[0].Value;
cmd.ExecuteNonQuery();
conn.Close();


//delete from datagrid:
dt.Rows[dgDatabaseGrid.SelectedCells[0].RowIndex].Delete();

}

//always cancel!
e.Cancel = true;

}
}

最佳答案

我知道现在已经很晚了,但也许它会对某人有所帮助。

对您的代码进行了以下更改:

try
{
adapt.Update(dt);

将这些行放在此处并使用您的变量

    Me.yourTableAdapter.Update(Me.yourDataSet.yourTable)
Me.yourDataSet.youTable.AcceptChanges()
Me.yourTableAdapter.Fill(Me.yourDataSet.yourTable)

它对我来说就像一个魅力,希望它对你有用。

}
catch (SqlException ex)
{
Debug.WriteLine(ex.Message);
}

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

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