SQL 在 oledb 中查询(插入),在 VB.NET 中没有重复项
SQL query (insert into) in oledb without duplicates in VB.NET
我想要 oledb 中的 SQL 查询命令(插入)而不重复,有没有最好的推荐?
谢谢
杰克
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
connectionString = cn
con = New OleDbConnection(connectionString)
con.Open()
Dim sql As String = "INSERT INTO EXAMPLE2 SELECT * FROM EXAMPLE1"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
con.Close()
Me.fillDataGridView1()
Me.fillDataGridView2()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
End Sub
要仅插入 EXAMPLE1 中不存在于 EXAMPLE2 中的行,您需要比较不想重复的行中每一列的值。
INSERT INTO EXAMPLE2 SELECT * FROM EXAMPLE1 EX1
WHERE NOT EXISTS
(SELECT * FROM EXAMPLE2 EX2 WHERE
EX2.CODE = EX1.CODE AND
EX2.NOD = EX1.NOD AND
EX2.QTY = EX1.QTY AND
EX2.PRICE = EX1.PRICE AND
EX2.REMARK = EX1.REMARK
)
我根据下面的 link 得到了答案解决方案
(Avoid duplicates in INSERT INTO SELECT query in SQL Server)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
connectionString = cn
con = New OleDbConnection(connectionString)
con.Open()
Dim sql As String = "INSERT INTO EXAMPLE2 (CODE,NOD,QTY,PRICE) SELECT t1.CODE, t1.NOD,t1.QTY, t1.PRICE FROM EXAMPLE1 t1 WHERE NOT EXISTS(SELECT CODE FROM EXAMPLE2 t2 WHERE t2.CODE = t1.CODE)"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
con.Close()
Me.fillDataGridView1()
Me.fillDataGridView2()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
End Sub
我想要 oledb 中的 SQL 查询命令(插入)而不重复,有没有最好的推荐?
谢谢 杰克
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
connectionString = cn
con = New OleDbConnection(connectionString)
con.Open()
Dim sql As String = "INSERT INTO EXAMPLE2 SELECT * FROM EXAMPLE1"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
con.Close()
Me.fillDataGridView1()
Me.fillDataGridView2()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
End Sub
要仅插入 EXAMPLE1 中不存在于 EXAMPLE2 中的行,您需要比较不想重复的行中每一列的值。
INSERT INTO EXAMPLE2 SELECT * FROM EXAMPLE1 EX1
WHERE NOT EXISTS
(SELECT * FROM EXAMPLE2 EX2 WHERE
EX2.CODE = EX1.CODE AND
EX2.NOD = EX1.NOD AND
EX2.QTY = EX1.QTY AND
EX2.PRICE = EX1.PRICE AND
EX2.REMARK = EX1.REMARK
)
我根据下面的 link 得到了答案解决方案 (Avoid duplicates in INSERT INTO SELECT query in SQL Server)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
connectionString = cn
con = New OleDbConnection(connectionString)
con.Open()
Dim sql As String = "INSERT INTO EXAMPLE2 (CODE,NOD,QTY,PRICE) SELECT t1.CODE, t1.NOD,t1.QTY, t1.PRICE FROM EXAMPLE1 t1 WHERE NOT EXISTS(SELECT CODE FROM EXAMPLE2 t2 WHERE t2.CODE = t1.CODE)"
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MessageBox.Show("Successfully Updated...", "Update")
con.Close()
Me.fillDataGridView1()
Me.fillDataGridView2()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
End Sub