2017-03-06 97 views
0

我想問一下從我的SqlDb插入數據到MariaDb。我的麻煩是,當我成功插入數據,我有相同的時間戳和狀態的雙重數據。我對插入過程很少解釋。每個AWB都有許多狀態喜歡SD,AR,OD,UN,RT,UN,UN和OK。我已經使用Windows服務計劃插入數據。我每30秒製作一次時間表。麻煩的是,我之前插入MariaDb的每個數據總是在數據庫中再次繪製,因此在數據庫中存在大量有關AWB的雙重數據。我怎麼能否認以前插入的數據與之前插入的相同的狀態和時間戳不能再進入我的MariaDB?VB.Net MariaDB Sql Server

這是我的 '選擇查詢和插入代碼'

Private Sub getStatus() 
    Try 
     Dim con As New SqlConnection(DesktopGeneral.koneksiTTM) 
     buka2(con) 
     Querysend = "Declare @AWB as varchar(50) " 
     Querysend += "Select @AWB=Pick_Cust.AWB from Pick_Cust inner join PICKUP_DETIL On Pick_Cust.TrNo=PICKUP_DETIL.TrNo where ComName LIKE '%Lazada%' " 
     Querysend += "Select PICKUP_DETIL.AWB,PICKUP_DETIL.RefNo,PICKUP_DETIL.Date,PICKUP_DETIL.time,PICKUP_DETIL.Origin As Station,PICKUP_DETIL.CP2 As Status,'' as Recipient,'' as Relation,'' as Remarks,PICKUP_DETIL.Login,'' as ReasonCode,'' as ReasonNote,'Data Entry' as Comment from PICKUP_DETIL " 
     Querysend += "inner join RECEIVED r on PICKUP_DETIL.AWB=r.AWB where [email protected] " 
     Querysend += "union " 
     Querysend += "Select PICKUP_DETIL.AWB,PICKUP_DETIL.RefNo,PICKUP_DETIL.PUdate as Date,PICKUP_DETIL.PUtime as Time,PICKUP_DETIL.Origin as Station,PICKUP_DETIL.CP as Status,'' as Recipient,'' as Relation,'' as Remarks, c.CouName,'' as ReasonCode,'' as ReasonNote,'Pick-Up' as Comment from PICKUP_DETIL " 
     Querysend += "inner join COURIER c on PICKUP_DETIL.Pic=c.CouRoute where [email protected] " 
     Querysend += "union " 
     Querysend += "Select BABY.AWB,p.RefNo,BABY.DATE,BABY.TIME,BABY.STN as Station,BABY.CP as Status,'' as Recipient,'' as Relation,BABY.BABY as Remarks,BABY.LOGIN,'' as ReasonCode,'' as ReasonNote,'Sotir Departement/Destination' as Comment from BABY " 
     Querysend += "inner join PICKUP_DETIL p on BABY.AWB=p.AWB where [email protected] " 
     Querysend += "union " 
     Querysend += "Select ARRIVAL.Code,p.RefNo,CONVERT(char(10),arrival.Date,120) as Date,CONVERT(char(5),arrival.Date,114) as Time,STN as Station,arrival.CP as Status,'' as Recipient,'' as Relation,'' as Remarks, arrival.PIC,'' as ReasonCode,'' as ReasonNote,'Arrival at Destination HUB' as Comment from ARRIVAL " 
     Querysend += "inner join PICKUP_DETIL p on ARRIVAL.Code=p.AWB where [email protected] " 
     Querysend += "union " 
     Querysend += "Select DELIVERY.AWB,p.RefNo,CONVERT(char(10),delivery.Date,120) as Date,CONVERT(char(5),delivery.Date,114) as Time, STN as Station,delivery.CP as Status,'' as Recipient,'' as Relation,'Kurir:' +COURIER.CouName + '.Note:'+Note + ' Cycle:'+CYCLE as Remarks,delivery.PIC,'' as ReasonCode,'' as ReasonNote,'On Delivery' as Comment from Delivery " 
     Querysend += "inner join COURIER on DELIVERY.CouRoute=COURIER.CouRoute inner join PICKUP_DETIL p on DELIVERY.AWB=p.AWB where [email protected] " 
     Querysend += "union " 
     Querysend += "Select UNDELIVERED.AWB,pd.RefNo,CONVERT(char(10),undelivered.date,120) as Date,CONVERT(char(5),undelivered.date,114) as Time,STN as Station,undelivered.CP as Status,'' as Recipient,'' as Relation,Note+'>' as Remarks,c.CouName,Other as ReasonCode,a.Remarks as ReasonNote,'Undelivered' as Comment from UNDELIVERED " 
     Querysend += "inner join COURIER c on UNDELIVERED.PIC=c.CouRoute inner join RMK_RET a on UNDELIVERED.Other=a.Code inner join PICKUP_DETIL pd on UNDELIVERED.AWB=pd.AWB where [email protected] " 
     Querysend += "union " 
     Querysend += "Select RECEIVED.AWB,pd.RefNo,CONVERT(char(10),received.Date,120) as Date,CONVERT(char(5),received.Date,114) as Time,STN as Station,received.CP as Status,Recipient as Recipient,Relation as Relation,Note as Remarks,received.PIC,'' as ReasonCode,'' as ReasonNote,'Delivery Success' as Comment from RECEIVED " 
     Querysend += "inner join PICKUP_DETIL pd on RECEIVED.AWB=pd.AWB where [email protected] " 
     Querysend += "union " 
     Querysend += "Select [RETURN].AWB,pd.RefNo,CONVERT(char(10),[return].date,120) as Date,CONVERT(char(5),[return].date,114) as Time,STN as Station,[return].CP as Status,'' as Recipient,'' as Relation,Note as Remarks,[return].PIC,returnCode as ReasonCode,a.Remarks as ReasonNote,'Return Shipper' as Comment from [RETURN] " 
     Querysend += "inner join RMK_RET a on [RETURN].returnCode=a.Code inner join PICKUP_DETIL pd on [RETURN].AWB=pd.AWB where [return][email protected] " 
     Dim ds As New DataSet 
     Dim cmd As New SqlCommand(Querysend, con) 
     Dim da As New SqlDataAdapter(cmd) 
     da.Fill(ds, "joe") 
     tutup2(con) 

     If ds.Tables(0).Rows.Count > 0 Then 
      Dim cn As New MySqlConnection(DesktopGeneral.koneksiAPI) 
      buka(cn) 
      For i As Integer = 0 To ds.Tables(0).Rows.Count - 1 
       Querysend = "Insert into api_status(AWB,TrackingNumber,Status,TimeStamp,Comment,Station,Recipient,Relation,ReasonCode,ReasonNote,Attempt,Username,RequestID,Flag) " 
       Querysend += "VALUES(@AWB,@TrackingNumber,@Status,@TimeStamp,@Comment,@Station,@Recipient,@Relation,@ReasonCode,@ReasonNote,@Attempt,@Username,@RequestID,@Flag) " 
       Dim mycmd As New MySqlCommand(Querysend, cn) 
       mycmd.Parameters.Add("AWB", MySqlDbType.VarChar, 50).Value = ds.Tables(0).Rows(i).Item("AWB").ToString 
       mycmd.Parameters.Add("TrackingNumber", MySqlDbType.VarChar, 50).Value = ds.Tables(0).Rows(i).Item("Refno").ToString 
       mycmd.Parameters.Add("Status", MySqlDbType.VarChar, 50).Value = ds.Tables(0).Rows(i).Item("Status").ToString 
       mycmd.Parameters.Add("TimeStamp", MySqlDbType.DateTime).Value = CDate(ds.Tables(0).Rows(i).Item("Date").ToString).ToString("yyyy-MM-dd") & " " & ds.Tables(0).Rows(i).Item("Time").ToString 
       mycmd.Parameters.Add("Comment", MySqlDbType.VarChar, 100).Value = ds.Tables(0).Rows(i).Item("Comment").ToString 
       mycmd.Parameters.Add("Station", MySqlDbType.VarChar, 50).Value = ds.Tables(0).Rows(i).Item("Station").ToString 
       mycmd.Parameters.Add("Recipient", MySqlDbType.VarChar, 150).Value = ds.Tables(0).Rows(i).Item("Recipient").ToString 
       mycmd.Parameters.Add("Relation", MySqlDbType.VarChar, 10).Value = ds.Tables(0).Rows(i).Item("Relation").ToString 
       mycmd.Parameters.Add("ReasonCode", MySqlDbType.VarChar, 10).Value = ds.Tables(0).Rows(i).Item("ReasonCode").ToString 
       mycmd.Parameters.Add("ReasonNote", MySqlDbType.VarChar, 100).Value = ds.Tables(0).Rows(i).Item("ReasonNote").ToString 
       mycmd.Parameters.Add("Attempt", MySqlDbType.Int16).Value = 1 
       mycmd.Parameters.Add("Username", MySqlDbType.VarChar, 50).Value = "Joe" 
       mycmd.Parameters.Add("RequestID", MySqlDbType.VarChar, 50).Value = "" 
       mycmd.Parameters.Add("Flag", MySqlDbType.Int16).Value = 0 
       mycmd.ExecuteNonQuery() 
       mycmd.Dispose() 
      Next 
      tutup(cn) 
     End If 
    Catch ex As Exception 
     Dim err As String 
     err = ex.Message 
    End Try 
End Sub 

請幫助。謝謝你之前:)

+1

看起來你每隔30秒運行一次SELECT查詢,所以它會返回相同的數據並一次又一次地在目標數據庫中插入相同的數據。 –

+0

感謝@ Chetan的迴應。我把設置時間放在我的配置文件中。所以它會自動將數據提交給我的MariaDB。那麼,如何忽略具有相同狀態和時間戳的數據以便不再拉到我的MariaDB?任何建議與代碼? – Joinnes

+0

'「從Pick_Cust內部加入PICKUP_DETIL選擇@ AWB = Pick_Cust.AWB On Pick_Cust.TrNo = PICKUP_DETIL.TrNo其中ComName LIKE'%Lazada%'」'將始終返回相同的單個記錄。你是否試圖從該表中檢索最近插入的記錄?您的要求不是很清楚 - 什麼是AWB,您想要選擇什麼記錄? – ADyson

回答

0

使用除了在聲明中的最後一個Querysend串像

except 
select AWB,TrackingNumber,Station,Status,CONVERT(char(10),TimeStamp,120) as 
Date,CONVERT(char(5),TimeStamp,114) as Time,Station,Status,Recipient,Relation, 
ReasonCode,ReasonNote,Comment from [MariaDb].[dbo].[api_status] where [email protected] 

您可以鏈接使用服務器的sp_addlinkedserver(),如果它是從不同的服務器

http://msdn.microsoft.com/en-us/library/ms190479.aspx

實施例:

exec sp_addlinkedserver [ @server= ] 'server' [ , [ @srvproduct= ] 'product_name' ] 
[ , [ @provider= ] 'provider_name' ] 
[ , [ @datasrc= ] 'data_source' ] 
[ , [ @location= ] 'location' ] 
[ , [ @provstr= ] 'provider_string' ] 
[ , [ @catalog= ] 'catalog' ] 

的n

to use your MariaDb Table [server].[MariaDb].[dbo].[api_status] 
+0

Hi @Pream感謝您的回覆。所以,我必須把除了最後的QuerySend?它有一個不同的數據庫人。在QuerySend上的select查詢來自SqlDB。如何從api_status中選擇,而api_status是MariaDB。你能解釋我嗎? – Joinnes

+0

hi @Joinnes是的,如果他們在同一臺服務器上,你可以使用MariaDb.dbo.api_status和SqlDb.dbo.PICKUP_DETIL – Pream

+0

但他們是不同的數據庫管理員。怎麼做?你可以編輯上面的代碼,所以我可以看到結果。 – Joinnes