0
我想問一下關於使用Vb.Net編碼的問題。我想從我舊的Foxpro數據庫導入數據到我的SQL Server數據庫。我選擇了需要從Foxpro導入到SQL Server數據庫的所有列。原因是當我嘗試導入數據時,我總是得到錯誤Vb.Net,SQL Server和Foxpro
違反PRIMARY KEY約束'PK_MsDetail'。不能在對象'dbo.MsDetail'中插入重複鍵。
這是我的VB.NET代碼:
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
If txtFile.Text = "" Then
MsgBox("Cannot be Empty")
Else
dgData.DataSource = Nothing
path = "Q:\umum" & txtFile.Text.Trim & ".DBF"
Dim MyConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection
DtSet = New System.Data.DataSet
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=vfpoledb.1; " &
"data source='" & "Q:\" & "'; Collating Sequence=general")
MyCommand = New OleDbCommand("select Cd_png,cabang,subcabang,noncs,User_i,tgl_pkp,Cycle,Nama,Company,Kota,kota1,kota2,NamaKota,Telepon,Cd_post,User_ti,noncs,report3,report2,report1,report4 from [" & path & "] where cd_png ='" & txtPng.Text & "' and report3 is not null", MyConnection)
da = New OleDbDataAdapter(MyCommand)
da.Fill(DtSet, "a")
End If
dgData.DataSource = DtSet.Tables("a")
End Sub
Private Sub btnExec_Click(sender As Object, e As EventArgs) Handles btnExec.Click
_thread = New Thread(New ThreadStart(AddressOf Ribakkon))
_thread.Start()
End Sub
Private Function cek_ada_ga(ByRef field As String, ByRef TABLE As String, ByRef AWB As String) As Boolean
If ConnImport.State = ConnectionState.Closed Then
ConnImport.Open()
End If
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim Nil As Boolean
Try
cmd = New SqlCommand("select " & field & " from " & TABLE & " where " & field & " = '" & AWB & "' ", ConnImport)
dr = cmd.ExecuteReader
If dr.Read Then
Nil = False
Else
Nil = True
End If
dr.Close()
cmd.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
End Try
Return Nil
End Function
Private Sub Ribakkon()
If ConnImport.State = ConnectionState.Closed Then
ConnImport.Open()
End If
Dim i As Integer
For i = 0 To DtSet.Tables("a").Rows.Count - 1
If cek_ada_ga("PuNo", "MsData", Microsoft.VisualBasic.Trim(DtSet.Tables("a").Rows(i).Item(1))) Then
With DtSet.Tables("a").Rows(i)
Dim Juliya As String
Juliya = Microsoft.VisualBasic.Trim(.Item(0))
fetch = "INSERT INTO MsData (Puno,Refno,MdName,MdCompany,MdAdd1,MdAdd2,MdAdd3,MdAdd4,MdCity,MdZip,MsDatein,MdPhone1,IdUser)"
fetch += " values ('" & .Item(1) & "','" & .Item(1) & "','" & Regex.Replace(.Item(7).ToString, "[',]", " ") & "','" & Regex.Replace(.Item(8).ToString, "[',]", " ") & "','" & Regex.Replace(.Item(9).ToString, "[',]", " ") & "','" & Regex.Replace(.Item(10).ToString, "[',]", " ") & "'"
fetch += ",'" & Regex.Replace(.Item(11).ToString, "[',]", " ") & "','" & Regex.Replace(.Item(12).ToString, "[',]", " ") & "','-','" & Regex.Replace(.Item(14).ToString, "[',]", " ") & "'"
fetch += ",getdate(),'" & Regex.Replace(.Item(13).ToString, "[',]", " ") & "','JOINNES')"
End With
Dim cmd As New SqlCommand(fetch, ConnImport)
cmd.ExecuteNonQuery()
fetch = ""
End If
If cek_ada_ga("PuNo", "MsDetail", Microsoft.VisualBasic.Trim(DtSet.Tables("a").Rows(i).Item(1))) Then
With DtSet.Tables("a").Rows(i)
Dim Juliya As String
Juliya = Microsoft.VisualBasic.Trim(.Item(0))
sql += "INSERT INTO MsDetail (Puno,AWB,Periode,Code,BCode,PuDate,Noresi,FileName,Field1,Field2,MsChannel,Pudatein,IdUser)"
sql += " values ('" & .Item(1) & "','" & .Item(3) & "','" & Mid(.Item(4), 6, 2) & Mid(.Item(4), 3, 2) & "','" & .Item(0) & "','CGK'"
sql += ",'" & .Item(5) & "','-','" & txtPng.Text & "','" & .Item(6).ToString & "','" & .Item(2).ToString & "','FOXPRO','" & CDate(.Item(4)).ToString("MM/dd/yyyy") + " " + .Item(15).ToString & "','JOINNES')"
End With
Dim cmd As New SqlCommand(sql, ConnImport)
cmd.ExecuteNonQuery()
sql = ""
End If
Next
For i = 0 To DtSet.Tables("a").Rows.Count - 1
If cek_ada_ga("AWB", "Closed", Microsoft.VisualBasic.Trim(DtSet.Tables("a").Rows(i).Item(16))) Then
With DtSet.Tables("a").Rows(i)
Dim Juliya, STT As String
If .Item(1).ToString.Trim = "BERHASIL" Then
STT = "OK"
Else
STT = "RT"
End If
Juliya = Microsoft.VisualBasic.Trim(.Item(0))
sql += "INSERT INTO Closed (AWB,ScStt,ScDate,ScPenerima,ScRel,ScDatein,ScNote,IdUser) "
sql += " values ('" & .Item(16) & "','" & STT & "','" & .Item(19) & "','" & Regex.Replace(.Item(18).ToString, "[',]", " ") & "','" & .Item(20).ToString & "'"
sql += ",getdate(),'-','JOINNES')"
End With
Dim cmd As New SqlCommand(sql, ConnImport)
cmd.ExecuteNonQuery()
sql = ""
End If
Next
End Sub
我很困惑如何解決。我需要你們大家的幫助。非常感謝您的幫助:)
感謝您的答案兄弟,我向你解釋一點。我有兩個表在SQL DB它是MsDetail和MsData。在MsData上,我將PuNo設置爲主鍵,在MsDetail上我將AWB設置爲主鍵,將MsData上的PuNo設置爲MsDetail表中的FK。我得到的問題是當我插入數據時,它總是顯示像我上面解釋的錯誤。我的主要問題是我怎麼可以從我的foxpro插入數據到我的SQL,雖然有相同的數據呢?你能給我一個更好的問題嗎?謝謝兄弟:) – Joinnes
如果SQL Server中的字段是一個PK,或者存在唯一的約束,那麼您可以刪除VFP數據中的副本,或者刪除SQL Server中的約束。編碼不會解決這個問題。 –