【问题标题】:VB.Net MariaDB Sql ServerVB.Net MariaDB Sql 服务器
【发布时间】:2017-03-06 07:20:52
【问题描述】:

我想询问有关将 SqlDb 中的数据插入 MariaDb 的问题。我的麻烦是当我成功插入数据时,我有相同时间戳和状态的双重数据。我很少解释插入过程。每个 AWB 都有许多状态喜欢 SD、AR、OD、UN、RT、UN、UN 和 OK。我已使用 Windows 服务计划插入数据。我每 30 秒制定一次时间表。问题是我之前插入 MariaDb 的每一个数据总是在数据库中再次绘制,所以数据库中有很多关于一个 AWB 的双重数据。如何拒绝之前插入的具有相同状态和时间戳的所有数据不能再进入我的 MariaDB?

这是我的“选择查询并插入代码”

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 pickup_Detil.AWB=@AWB "
        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 AWB=@AWB "
        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 baby.AWB=@AWB "
        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 code=@AWB "
        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 delivery.AWB=@AWB "
        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 undelivered.AWB=@AWB "
        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 received.AWB=@AWB "
        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].AWB=@AWB "
        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

请帮忙。之前谢谢你:)

【问题讨论】:

  • 看起来您每 30 秒运行一次相同的 SELECT 查询,因此它会返回相同的数据并一次又一次地在目标数据库中插入相同的数据。
  • 感谢@Chetan 的回复。我把设置时间放在我的配置文件中。所以它会自动将数据拉到我的 MariaDB 中。那么,我怎样才能忽略具有相同状态和时间戳的数据,以便不再拉到我的 MariaDB 中呢?有代码建议吗?
  • "Select @AWB=Pick_Cust.AWB from Pick_Cust inner join PICKUP_DETIL On Pick_Cust.TrNo=PICKUP_DETIL.TrNo where ComName LIKE '%Lazada%' " 将始终返回相同的单个记录。您是否尝试从该表中检索最近插入的记录?您的要求不是很清楚 - 什么是 AWB,您要选择哪些记录?
  • 您需要过滤数据。您需要跟踪直到什么时间点(例如时间戳)数据被插入 MariaDB 并存储该信息。这样下次您仅从该点选择数据。就像您为某个时间戳选择 1000 行一样。存储最后一行的时间戳。在下次运行时使用该时间戳,依此类推。如果您可以提供有关您的数据和选择标准的一些详细信息,则很容易提供具体的解决方案。
  • @ADyson 谢谢伙计,您的查询是对的,但它总是会选择带有客户 Lazada 的 AWB。我只是将客户 Lazada 的状态转移到我的 MariaDB 中。我只是想知道如何使用一个 AWB 使数据不会一次又一次地插入。

标签: sql sql-server vb.net mariadb


【解决方案1】:

在 Querysend 字符串的最后使用 except 语句,如

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 AWB=@AWB

如果服务器来自不同的服务器,您可以使用 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' ]  

然后

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

【讨论】:

  • 嗨@Pream 感谢您的回复。所以,我必须把except放在QuerySend的最后?它有一个不同的数据库人。在 QuerySend 上的选择查询中来自 SqlDB。当 api_status 是 MariaDB 时,如何从 api_status 中选择它。你能解释一下吗?
  • hi @Joinnes 是的,如果他们在同一个服务器上,你可以使用 MariaDb.dbo.api_status 和 SqlDb.dbo.PICKUP_DETIL
  • 但他们是不同的数据库人。怎么做?你能编辑上面的代码,这样我就可以看到结果了。
  • @Joinnes,请立即查看答案
  • @Joinnes 给出日期、时间和时间戳值的示例数据
猜你喜欢
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2019-10-24
相关资源
最近更新 更多