c# - 我在我的应用程序中更改了一个值,但没有在我的数据库中更新

标签 c# database

我在我的项目中编写了这段代码,但是当我在我的应用程序中更改一个值时,出现异常并且不更新我的数据库,这段代码适用于新数据但不适用于删除和编辑数据。

代码:

private void btnLecOk_Click(object sender, EventArgs e)
    {
            DataRow dr = (lecturesBindingSource.Current as DataRowView).Row;
            dr["Section_ID"] = Convert.ToInt32(cmbLec_Section_Name.SelectedValue);
            dr["Lecture_Type_ID"] = Convert.ToInt32(cmbLec_Lecture_Type.SelectedValue);
            dr["Practicable_Unit"] = numLec_Practicable_Unit.Value;
            dr["theoretical_Unit"] = numLec_Theoretical_Unit.Value;
            dr["Term"] = numLec_Term.Value;

            lecturesBindingSource.EndEdit();
            lecturesTableAdapter.Update(dsSchedule.Lectures);
            EnableLectureControls();
    }

异常:

System.Data.DBConcurrencyException was unhandled Message="Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." Source="System.Data" RowCount=1

SQL 查询敌人更新:

UPDATE Lectures SET Lecture_Code = @Lecture_Code, Section_ID = @Section_ID, Lecture_Type_ID = @Lecture_Type_ID, Lecture_Name = @Lecture_Name, Practicable_Unit = @Practicable_Unit, theoretical_Unit = @theoretical_Unit, Lecture_Capacity = @Lecture_Capacity, Term = @Term WHERE (Lecture_Code = @Original_Lecture_Code) AND (Section_ID = @Original_Section_ID) AND (Lecture_Type_ID = @Original_Lecture_Type_ID) AND (Lecture_Name = @Original_Lecture_Name) AND (Practicable_Unit = @Original_Practicable_Unit) AND (theoretical_Unit = @Original_theoretical_Unit) AND (Lecture_Capacity = @Original_Lecture_Capacity) AND (Term = @Original_Term)

最佳答案

我曾经遇到过同样的问题,我记得它在更改受影响的数据库表中的关系属性后起作用了。我将 Insert and Update Specification 更改为 Cascade,问题再也没有出现过。尽管它可能不是完美的解决方案。

关于c# - 我在我的应用程序中更改了一个值,但没有在我的数据库中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/834646/

相关文章:

c# - 在 silverlight 中使用 WCF CloseAsync

php - 使用 PHP 在 Mysql 中一次执行多个查询

c# - 符号 "=>"在 C# 中是什么意思?

c# - 向后移植 C# 内插字符串运算符 $

c# - 正确填充多维数组

mysql - 使用MySQL批量插入数据库表数据

sql - 使用触发器 SQL 自动填充列

c# - 如何获取合并单元格的值?

mysql - 数据库设计: storing hourly analytics for thousands of seperate apps

mysql - 未知列,即使它存在