更新数据表但第1行未标记为已修改,因此无法更新SQL表

时间:2016-10-11 12:51:27

标签: vb.net datatable

根据史蒂夫的请求在写入数据表之前修改了行状态:

我有以下代码写入数据表,该数据表是通过点击表单上的SAVE按钮调用的。表中的更改最终通过dataadapter发送到SQL数据库,但第一行没有正确保存在数据库中,我找到了它,因为第一行的状态正在显示"不变"在数据表中。 我用控制台写入包装语句,看看发生了什么。任何人都可以向我解释为什么在写入之后它没有将第一行标记为已修改?其余的行都可以。

写入数据表的代码:

Console.WriteLine(DateTime.Now.ToString & " *************** New Entry ***************")

'update datetime in the datatable
Dim rowindex As Integer = 0

For Each row As DataRow In dt_PartData.Rows

    If Not row.RowState = DataRowState.Deleted Then

        Console.WriteLine(rowindex.ToString & " Row state Before: " & row.RowState.ToString)
        Console.WriteLine(rowindex.ToString & " Reading Before: " & dt_PartData.rows(rowindex)("LastSaved").ToString)

        dt_PartData.rows(rowindex)("LastSaved") = DateTime.Now.ToString
        Console.WriteLine(rowindex.ToString & " Writing:  " & DateTime.Now.ToString)
        Console.WriteLine(rowindex.ToString & " Reading After:  " & dt_PartData.rows(rowindex)("LastSaved").ToString)
        Console.WriteLine(rowindex.ToString & " Row state After: " & row.RowState.ToString)

    End If

    rowindex = rowindex + 1
Next row

下面的代码是在上面的代码之后立即调用的一个被调用的子例程,就在SQL语句更新数据库之前:

Console.WriteLine(DateTime.Now.ToString & " *************** Saving Entry ***************")

'update datetime in the datatable
Dim rowindex As Integer = 0

For Each row As DataRow In dt_PartData.Rows

    If Not row.RowState = DataRowState.Deleted Then
         Console.WriteLine(rowindex.ToString & ": Row state: " & row.RowState.ToString)
     End If

    rowindex = rowindex + 1
Next row

输出窗口的视图:

10/11/2016 9:44:17 AM *************** New Entry *************** In 10min
0 Row state Before: Unchanged
0 Reading Before: 10/11/2016 7:41:27 AM
0 Writing:  10/11/2016 9:44:30 AM
0 Reading After:  10/11/2016 9:44:30 AM
0 Row state After: Unchanged
1 Row state Before: Unchanged
1 Reading Before: 10/11/2016 9:39:55 AM
1 Writing:  10/11/2016 9:44:30 AM
1 Reading After:  10/11/2016 9:44:30 AM
1 Row state After: Modified
2 Row state Before: Unchanged
2 Reading Before: 10/11/2016 9:39:55 AM
2 Writing:  10/11/2016 9:44:30 AM
2 Reading After:  10/11/2016 9:44:30 AM
2 Row state After: Modified
3 Row state Before: Unchanged
3 Reading Before: 10/11/2016 9:39:55 AM
3 Writing:  10/11/2016 9:44:30 AM
3 Reading After:  10/11/2016 9:44:30 AM
3 Row state After: Modified
4 Row state Before: Unchanged
4 Reading Before: 10/11/2016 9:39:55 AM
4 Writing:  10/11/2016 9:44:30 AM
4 Reading After:  10/11/2016 9:44:30 AM
4 Row state After: Modified
5 Row state Before: Unchanged
5 Reading Before: 10/11/2016 9:39:55 AM
5 Writing:  10/11/2016 9:44:30 AM
5 Reading After:  10/11/2016 9:44:30 AM
5 Row state After: Modified
6 Row state Before: Unchanged
6 Reading Before: 10/11/2016 9:39:55 AM
6 Writing:  10/11/2016 9:44:30 AM
6 Reading After:  10/11/2016 9:44:30 AM
6 Row state After: Modified
7 Row state Before: Unchanged
7 Reading Before: 10/11/2016 9:39:55 AM
7 Writing:  10/11/2016 9:44:30 AM
7 Reading After:  10/11/2016 9:44:30 AM
7 Row state After: Modified
10/11/2016 9:44:30 AM *************** Saving Entry ***************
0: Row state: Unchanged
1: Row state: Modified
2: Row state: Modified
3: Row state: Modified
4: Row state: Modified
5: Row state: Modified
6: Row state: Modified
7: Row state: Modified

感谢您的帮助,这个论坛给了我很多帮助!

1 个答案:

答案 0 :(得分:0)

我现在正在工作。我找到了两个似乎对此有用的解决方案。

  1. 我仍然不完全明白这对我有用,但我 在这里发现这篇文章与第一行有类似的问题 不在数据表中更新并将其用作a的源 datagridview的。 Missing Row in Datatable Modified property
  2. 所以我在更改数据表之前添加了这行代码:

    BindingContext(dt_PartData).EndCurrentEdit()
    

    或者这个:

    2。  将datagridview.DataSource设置为nothing,然后对数据表进行更改,然后将.DataSource放回数据表。

    DataGridViewPartData.DataSource = Nothing
    
         (make changes to the datatable here)
    
    DataGridViewPartData.DataSource = dt_PartData