这是我一直在尝试的。问题似乎在于将数据集提交到数据库,因为我可以在控制台中看到数据表。
我在更新之前和之后尝试了AcceptChanges,当我查看状态时,它显示为已添加。
以下是代码:
Dim ds As DataSet = New DataSet()
Dim tblPicture As DataTable
Dim tblNewPics As DataTable
Dim tblPictureCount As Integer
Dim i As Integer
Dim conn As SqlConnection = New SqlConnection("Server=R850-PC\SQLEXPRESS2012;database=NewParts;Integrated Security=True;Persist Security Info=False")
Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from Picture Where IsNew = 1", conn)
da.Fill(ds, "Picture")
tblPicture = ds.Tables("Picture")
tblPictureCount = tblPicture.Rows.Count
tblPictureCount = tblPictureCount - 1
'SOURCE Picture table to the screen.
Console.WriteLine("Source Table tblPicture has " & tblPictureCount.ToString & " Rows")
For i = 0 To tblPictureCount
Console.WriteLine("Row(" & i.ToString & ") = " & tblPicture.Rows(i)(3))
Next
'********************* ADD TO NEW TABLE DATA **************************************************************************************
Dim da2 As SqlDataAdapter = New SqlDataAdapter("Select * from Picture_Temp", conn)
da2.Fill(ds, "Picture_Temp")
tblNewPics = ds.Tables("Picture_Temp")
tblNewPics = tblPicture.Clone
For i = 0 To tblPictureCount
tblNewPics.ImportRow(tblPicture.Rows(i))
Dim dr2 As DataRow = tblNewPics.Rows(i)
dr2.SetAdded()
Next
da2.Update(ds.Tables("Picture_Temp"))
tblNewPics.AcceptChanges()
'******************* DEBUG ... check new database table
Console.WriteLine()
Console.WriteLine("Destination TABLE has " & tblNewPics.ToString & " Rows")
For i = 0 To tblPictureCount
Console.WriteLine("Row(" & i.ToString & ") = " & tblNewPics.Rows(i)(3))
Next
Console.WriteLine("RowState = " & tblNewPics.Rows(i - 1).RowState.ToString)
Console.ReadLine()
txtMessages.Text = "Done"
End Sub
任何想法??
答案 0 :(得分:0)
您应该删除该克隆行。制作克隆意味着您正在创建新的DataTable。因此,当您调用更新时,旧的仍然是空的(或填充时的任何内容) 如果当前表已经具有正确的模式
,则无需调用Clone您还需要将SqlCommandBuilder链接到DataAdapter。该对象将负责创建更新表所需的相应sql语句
Dim builder = new SqlCommandBuilder(da2)
da2.Update(ds.Tables("Picture_Temp"))