我试图用数据集中的表更新sql server。 当我正在加载2个表时,如下面的例子,它的工作非常好,表1首先 - 然后是表2。 但如果我在表1之前加载表2 - 我收到一条错误消息。 似乎update命令只影响加载到数据集的最后一个表。
解决方案是什么?
错误消息: "其他信息:缺少DataColumn' ProjectName'在DataTable' Cities'对于SourceColumn' ProjectName'。"
Public da As New SqlDataAdapter
Public ds As DataSet = New DataSet()
Public cn As SqlConnection
Public SQLCommand As SqlCommand
Private Sub loadPart()
Dim strConnection As String = "Server=MY-PC\SQLEXPRESS;Database=BooKKeeping;Trusted_Connection=True;"
cn = New SqlConnection(strConnection)
Dim strSelect As String
Dim CommandBuilder As New SqlCommandBuilder(da)
'// PART 1 - LOAD TABLES TO DATASET
cn.Open()
'// LOAD TABLE 1
strSelect = "SELECT * FROM Projects"
SQLCommand = New SqlCommand(strSelect, cn)
da.SelectCommand = SQLCommand
da.Fill(ds, "Projects")
'// LOAD TABLE 2
strSelect = "SELECT * FROM Cities"
SQLCommand = New SqlCommand(strSelect, cn)
da.SelectCommand = SQLCommand
da.Fill(ds, "Cities")
cn.Close()
'// PART 2 - ADD NEW RECORDS
Dim row As DataRow = ds.Tables("Cities").NewRow()
row("City") = "A"
ds.Tables("Cities").Rows.Add(row)
'// PART 3 - UPDATE SERVER
cn.Open()
da.Update(ds, "Cities")
cn.Close()
End Sub
答案 0 :(得分:0)
请勿使用一个数据适配器尝试使用多个数据库表。如果要使用两个不同的表,请使用两个不同的数据适配器。这意味着两个命令构建器。