0

我有这段代码,但它只插入了 54 条记录中的 7 条记录。我确信数据表中的记录很好。

Dim dt As DataTable
Dim sc As SQLiteCommand
Dim Script As String = Nothing
Dim Script2 As String = Nothing
Dim Script3 As String = Nothing
Script = "insert into paperdate(dtime,papernum,paperstat,user) select " & "'" & dt.Rows(0)(0) & "'" & " as " & "dtime" & "," & dt.Rows(0)(1) & " as " & "papernum" & "," & "'" & dt.Rows(0)(2) & "'" & " as " & "paperstat" & "," & "'" & dt.Rows(0)(3) & "'" & " as " & "user"
For i As Integer = 1 To dt.Rows.Count - 1
Script2 = " union select " & "'" & dt.Rows(i)(0) & "'" & "," & dt.Rows(i)(1) & "," & "'" & dt.Rows(i)(2) & "'" & "," & "'" & dt.Rows(i)(3) & "'"
Script3 = Script3 & Script2
Next
sc = New SQLiteCommand(Script & Script3, mycon)
sc.ExecuteNonQuery()
sc.Dispose()

希望有大神解答,谢谢

4

1 回答 1

1

可能是您在第二次选择中有重复的记录。如果是这种情况,那么解决方案是使用UNION ALL而不是UNION.

使用 UNION ALL 运算符创建的复合 SELECT 返回 UNION ALL 运算符左侧的 SELECT 中的所有行,以及右侧的 SELECT 中的所有行。UNION 运算符的工作方式与 UNION ALL 相同,只是从最终结果集中删除重复行。

参考:http ://www.sqlite.org/lang_select.html

如果不是这样,那么您可能想要跟踪 SQL,并SELECT直接在 sqlite 客户端中运行这些部分。

更新

根据您的评论,这是您将对代码进行的更改:

Script2 = " union select " & "'" & dt.Rows(i)(0) & "'" & "," & dt.Rows(i)(1) & "," & "'" & dt.Rows(i)(2) & "'" & "," & "'" & dt.Rows(i)(3) & "'"

变成

Script2 = " union all select " & "'" & dt.Rows(i)(0) & "'" & "," & dt.Rows(i)(1) & "," & "'" & dt.Rows(i)(2) & "'" & "," & "'" & dt.Rows(i)(3) & "'"
于 2012-06-02T17:14:58.737 回答