2017-03-06 2 views
0

Ich möchte über das Einfügen von Daten von meinem SqlDb in MariaDb fragen. Mein Problem ist, wenn ich die Daten einfüge, habe ich doppelte Daten mit demselben TimeStamp und Status. Ich erkläre wenig über den Einfügeprozess. Jeder AWB hat viele Status Likes SD, AR, OD, UN, RT, UN, UN und OK. Ich habe die Daten unter Verwendung des Windows-Dienstplans eingefügt. Ich mache den Zeitplan alle 30 Sekunden. Das Problem ist, dass alle Daten, die ich zuvor in MariaDb eingefügt habe, immer wieder in der Datenbank gezeichnet werden, so dass es viele doppelte Daten über einen AWB in der Datenbank gibt. Wie kann ich es ablehnen, dass alle Daten, die zuvor mit demselben Status und Zeitstempel eingefügt wurden, nicht länger in meine MariaDB gelangen können?VB.Net MariaDB Sql Server

das mein 'Select Query und einfügen Code'

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 

Bitte um Hilfe. Vielen Dank, dass Sie vor :)

+1

Sieht aus wie Sie die gleiche SELECT-Abfrage alle 30 Sekunden ausführen, so wird es die gleichen Daten zurückgeben und die gleichen Daten in die Ziel-db immer wieder einfügen. –

+0

Danke für die Antwort @Chetan. Ich lege die Zeit in meine Konfigurationsdatei. also zieht es automatisch die Daten zu meiner MariaDB. Also, wie kann ich die Daten mit dem gleichen Status und Zeitstempel ignorieren, um nicht mehr zu meiner MariaDB zu ziehen? Irgendwelche Vorschläge mit Code? – Joinnes

+0

'" Wählen Sie @ AWB = Pick_Cust.AWB von Pick_Cust inneren Join PICKUP_DETIL On Pick_Cust.TrNo = PICKUP_DETIL.TrNo wo ComName wie '% Lazada%' "' wird immer den gleichen einzelnen Datensatz zurückgeben. Versuchen Sie, den zuletzt eingefügten Datensatz aus dieser Tabelle abzurufen? Ihre Anforderung ist nicht wirklich klar - Was ist ein AWB und welche Datensätze möchten Sie auswählen? – ADyson

Antwort

0

Verwenden except-Anweisung in der letzten in der Querysend String wie

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] 

Sie können den Server mit sp_addlinkedserver Link(), wenn es von einem anderen Server

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

ist

Beispiel:

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

die n

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

Hallo @Pream danke für Ihre Antwort. Also, ich muss die Ausnahme in letzter des QuerySend setzen? Es hat einen anderen Datenbankmann. in Select-Abfrage auf QuerySend stammt aus SqlDB. wie es aus api_status ausgewählt werden könnte, während api_status MariaDB ist. Kannst du mir erklären? – Joinnes

+0

hi @Joinnes Ja, wenn sie auf dem gleichen Server sind, können Sie einfach MariaDb.dbo.api_status und SqlDb.dbo.PICKUP_DETIL – Pream

+0

verwenden, aber sie sind andere Datenbank Mann. wie geht das? kannst du den obigen Code bearbeiten, damit ich das Ergebnis sehen kann. – Joinnes