数据表合并后,tableadapter.update 方法 returns 0
after datatable merge, the tableadapter.update method returns 0
我尝试了很多方法让我的数据库更新我添加的行,但没有成功。这是我的:
- 2 个数据库(本地和远程)
- 2x 数据 tables 在他们自己的数据集中
- 本地数据库有 3 个额外的列(1 个 nchar(15) 和 2 位全部为 NULLABLE)
- 2x 单独的 table 适配器和绑定源链接到数据网格视图 (dgv
仅供参考)
- 1 次极度头痛
我需要做的是用 localDB table 填充 dgv1,然后查看 table 中的最后一个发票编号作为 remoteDB 中填充 dgv2 的起点。使用在两个数据 table 中添加的数据,然后我将它们合并到 localDB 数据集中并调用 me.MasterInvoiceTableAdapter.Update(me.MasterInvoiceDataSet.MasterInvoices)
应该将添加的行保存到数据库中...
然后我再次填充数据table。它在 dgv 中看起来一切正常,但退出表单并再次加载显示没有完成实际更新。更新方法中的 return 代码也 returns 0 验证没有行受到影响。代码如下:
Try
If stats2 = False Then
Me.MasterInvoicesTableAdapter.Fill(Me.MasterInvoicesDataSet.MasterInvoices)
Else
Me.MasterInvoicesTableAdapter.GetData()
End If
Dim rowcount As Integer = DataGridView1.RowCount - 1
Dim lookup As String = Trim(DataGridView1.Item(0, rowcount - 1).Value)
MsgBox(lookup)
Me.SorMasterTableAdapter.Fill(Me.InvoicesFDataSet.SorMaster, lookup)
MasterInvoicesDataSet.MasterInvoices.Merge(InvoicesFDataSet.SorMaster, True)
InvoicesFDataSet.SorMaster.Clear()
Catch ex As Exception
MsgBox(ex.Message)
TextBox1.Text = ex.Message
End Try
Try
Me.Validate()
Me.MasterInvoicesBindingSource.EndEdit()
MsgBox(Me.MasterInvoicesTableAdapter.Update(Me.MasterInvoicesDataSet.MasterInvoices))
stats = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
仅供参考。 stats
变量用于控制刷新按钮的行为。
我试过循环遍历数据集并逐个添加数据行(newrow with row.add、importrow、item.add),我试过使用 linkedDB 查询一次完成所有这些(我发现数据库上的排序规则不一样),还有一些其他方法甚至不值得一提。
有什么建议吗?
我通过对合并方法进行一些研究解决了我的问题,发现行状态并不总是在额外的行上更改为 Added
,这是通过在合并表之前添加 For Each drow As DataRow In InvoicesFDataSet.SorMaster
drow.SetAdded()
Next
解决的.
希望这对一直在寻找类似问题的答案的任何人有所帮助。
我尝试了很多方法让我的数据库更新我添加的行,但没有成功。这是我的:
- 2 个数据库(本地和远程)
- 2x 数据 tables 在他们自己的数据集中
- 本地数据库有 3 个额外的列(1 个 nchar(15) 和 2 位全部为 NULLABLE)
- 2x 单独的 table 适配器和绑定源链接到数据网格视图 (dgv 仅供参考)
- 1 次极度头痛
我需要做的是用 localDB table 填充 dgv1,然后查看 table 中的最后一个发票编号作为 remoteDB 中填充 dgv2 的起点。使用在两个数据 table 中添加的数据,然后我将它们合并到 localDB 数据集中并调用 me.MasterInvoiceTableAdapter.Update(me.MasterInvoiceDataSet.MasterInvoices)
应该将添加的行保存到数据库中...
然后我再次填充数据table。它在 dgv 中看起来一切正常,但退出表单并再次加载显示没有完成实际更新。更新方法中的 return 代码也 returns 0 验证没有行受到影响。代码如下:
Try
If stats2 = False Then
Me.MasterInvoicesTableAdapter.Fill(Me.MasterInvoicesDataSet.MasterInvoices)
Else
Me.MasterInvoicesTableAdapter.GetData()
End If
Dim rowcount As Integer = DataGridView1.RowCount - 1
Dim lookup As String = Trim(DataGridView1.Item(0, rowcount - 1).Value)
MsgBox(lookup)
Me.SorMasterTableAdapter.Fill(Me.InvoicesFDataSet.SorMaster, lookup)
MasterInvoicesDataSet.MasterInvoices.Merge(InvoicesFDataSet.SorMaster, True)
InvoicesFDataSet.SorMaster.Clear()
Catch ex As Exception
MsgBox(ex.Message)
TextBox1.Text = ex.Message
End Try
Try
Me.Validate()
Me.MasterInvoicesBindingSource.EndEdit()
MsgBox(Me.MasterInvoicesTableAdapter.Update(Me.MasterInvoicesDataSet.MasterInvoices))
stats = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
仅供参考。 stats
变量用于控制刷新按钮的行为。
我试过循环遍历数据集并逐个添加数据行(newrow with row.add、importrow、item.add),我试过使用 linkedDB 查询一次完成所有这些(我发现数据库上的排序规则不一样),还有一些其他方法甚至不值得一提。
有什么建议吗?
我通过对合并方法进行一些研究解决了我的问题,发现行状态并不总是在额外的行上更改为 Added
,这是通过在合并表之前添加 For Each drow As DataRow In InvoicesFDataSet.SorMaster
drow.SetAdded()
Next
解决的.
希望这对一直在寻找类似问题的答案的任何人有所帮助。