在 vb.net 中使用 tableadapter 更新旧数据并添加新数据
Posted
技术标签:
【中文标题】在 vb.net 中使用 tableadapter 更新旧数据并添加新数据【英文标题】:Update old data and add new data using tableadapter in vb.net 【发布时间】:2013-02-02 23:16:44 【问题描述】:我的代码应该更新旧记录,同时如果找到新记录,它应该同样将其插入数据库中...我在执行此方法时使用 table adpater。
代码如下:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim pta As New PHDSTableAdapters.productdatabaseTableAdapter
pta.Updateproduct(TextBox1.Text, ComboBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)
pta.Fill(myds.productdatabase)
Dim lta As New PHDSTableAdapters.lotnoTableAdapter
Dim lt = lta.GetDataBylotno(TextBox5.Text)
Dim l As phaccess.PHDS.lotnoRow = lt.Rows(0)
Dim i As Integer
For i = 0 To DGV.Rows.Count - 1
For Each l In myds.lotno
Dim lot As String = DGV.Rows(i).Cells(1).Value
Dim del As Date = DGV.Rows(i).Cells(2).Value
Dim exp As Date = DGV.Rows(i).Cells(3).Value
Dim quantity As Integer = DGV.Rows(i).Cells(4).Value
Dim sup = DGV.Rows(i).Cells(5).Value
Dim disc = DGV.Rows(i).Cells(6).Value
If l.productid = TextBox5.Text Then
Dim lotnumber As String = l.lotnumber
If l.lotnumber <> lot Then
'the error occurs in the insert statement as it would create duplicates 'of the index...the index of the table is the lot number
lta.Insert(TextBox5.Text, lot, del, exp, quantity, sup, disc)
Else
lta.Updateedit(del, exp, quantity, sup, disc, lot)
lta.Fill(myds.lotno)
End If
End If
If lot = "" Then
closeform()
lta.Fill(myds.lotno)
Button3.Enabled = False
Button1.Visible = True
Button3.Visible = False
Button1.Enabled = False
Exit Sub
End If
Next
Next
End Sub
如果您需要其他任何东西来帮助我解决这个问题,请务必询问。 谢谢
【问题讨论】:
【参考方案1】:您可以使用“DataTable”来更新表格中的 DataGridView
用于显示数据:
Dim sql As String = "SELECT * FROM table_name"
sCommand = New SqlCommand(sql, conn)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "table_name")
sTable = sDs.Tables("table_name")
DataGridView1.DataSource = sTable
更新:
sAdapter.Update(sTable)
希望对你有帮助
【讨论】:
以上是关于在 vb.net 中使用 tableadapter 更新旧数据并添加新数据的主要内容,如果未能解决你的问题,请参考以下文章
在SQL TableAdapter上使用相同的参数2(或更多)次但仅传递1