防止sqlserver2008中的重复记录条目

时间:2015-11-25 05:46:05

标签: vb.net sql-server-2008

我使用VB.NET在VSAT应用程序上编码。我收到的字符串存储在数据库当前表中。我还希望在历史表中存储相同的字符串,但在15分钟的间隔内,只有一条记录必须保存在历史表中。怎么可能呢?请帮我。 我的代码是

Dim DecodeString As String         Dim DataSplit As String()         Dim date1split As String()

    DecodeString = strTempString.Substring(1)
    DecodeString = strTempString
    DataSplit = DecodeString.Split(",")
    date1split = DataSplit(2).Split("/")

    DataSplit(2) = date1split(1) & "/" & date1split(0) & "/" & date1split(2)


    DataSplit(3) = Format(Now, "hh:mm:00")

    If Now.Minute Mod 1 = 0 Then
        If Now.Minute >= 0 And Now.Minute <= 14 Then
            DataSplit(3) = Format(Now, "hh:00:00")

        ElseIf Now.Minute >= 15 And Now.Minute <= 29 Then
            DataSplit(3) = Format(Now, "hh:15:00")

        ElseIf Now.Minute >= 30 And Now.Minute <= 44 Then
            DataSplit(3) = Format(Now, "hh:30:00")

        ElseIf Now.Minute >= 45 And Now.Minute <= 59 Then
            DataSplit(3) = Format(Now, "hh:45:00")
        End If
    End If




    If strTempString.StartsWith("^") And strTempString.EndsWith("$") And strTempString.Length = 285 Then

        Try

            Using sqlconn As New SqlConnection("Server=YOGESHMSPL-PC; DataBase=MSPLKBJNL; User Id=sa; Pwd=mspl123")

                sqlconn.Open()

                Dim str1 As String = ""
                str1 = "begin tran;"
                ds.Clear()
                da = New SqlDataAdapter("Select * From CurrentDataCR Where mst_remote_station_id= '" & DataSplit(1) & "'", sqlconn)
                da.Fill(ds)




                If ds.Tables(0).Rows.Count = 0 Then


                    str1 &= ("Insert Into CurrentDataCR (mst_remote_station_id,date1,time1,UpStreamLevel,UpStreamLevelMin,UpStreamLevelMax, UpStreamSensorFail,UpStreamLevelAlarm,DownStreamLevel,DownStreamLevelMin,DownStreamLevelMax,DownStreamSensorFail,DownStreamLevelAlarm,TotalDischarge,TotalGates,Gate1Pos,Gate1SetPoint,Gate1Opening,Gate1Alarm,Gate1ElectricalAlarm,Gate1Mode,Gate2Pos,Gate2SetPoint,Gate2Opening,Gate2Alarm,Gate2ElectricalAlarm,Gate2Mode,Gate3Pos,Gate3SetPoint,Gate3Opening,Gate3Alarm,Gate3ElectricalAlarm,Gate3Mode,Gate4Pos,Gate4SetPoint,Gate4Opening,Gate4Alarm,Gate4ElectricalAlarm,Gate4Mode,InvtGenSignals,GeneratorSignals,OtherSignals,SupplyVoltage) values ('" & DataSplit(1) & "','" & DataSplit(2) & "','" & DataSplit(3) & "','" & DataSplit(5) & "','" & DataSplit(6) & "','" & DataSplit(7) & "','" & DataSplit(8) & "','" & DataSplit(9) & "','" & DataSplit(10) & "','" & DataSplit(11) & "','" & DataSplit(12) & "','" & DataSplit(13) & "','" & DataSplit(14) & "','" & DataSplit(15) & "','" & DataSplit(22) & "','" & DataSplit(23) & "','" & DataSplit(24) & "','" & DataSplit(25) & "','" & DataSplit(26) & "','" & DataSplit(27) & "','" & DataSplit(28) & "','" & DataSplit(29) & "','" & DataSplit(30) & "','" & DataSplit(31) & "','" & DataSplit(32) & "','" & DataSplit(33) & "','" & DataSplit(34) & "','" & DataSplit(35) & "','" & DataSplit(36) & "','" & DataSplit(37) & "','" & DataSplit(38) & "','" & DataSplit(39) & "','" & DataSplit(40) & "','" & DataSplit(41) & "','" & DataSplit(42) & "','" & DataSplit(43) & "','" & DataSplit(44) & "','" & DataSplit(45) & "','" & DataSplit(46) & "','" & DataSplit(60) & "','" & DataSplit(61) & "','" & DataSplit(62) & "','" & DataSplit(63) & "')")
                Else
                    str1 &= ("Update CurrentDataCR set mst_remote_station_id= '" & DataSplit(1) & "',date1='" & DataSplit(2) & "',time1='" & DataSplit(3) & "',UpStreamLevel='" & DataSplit(5) & "',UpStreamLevelMin='" & DataSplit(6) & "',UpStreamLevelMax='" & DataSplit(7) & "', UpStreamSensorFail='" & DataSplit(8) & "',UpStreamLevelAlarm='" & DataSplit(9) & "',DownStreamLevel='" & DataSplit(10) & "',DownStreamLevelMin='" & DataSplit(11) & "',DownStreamLevelMax='" & DataSplit(12) & "',DownStreamSensorFail='" & DataSplit(13) & "',DownStreamLevelAlarm='" & DataSplit(14) & "',TotalDischarge='" & DataSplit(15) & "',TotalGates='" & DataSplit(22) & "',Gate1Pos='" & DataSplit(23) & "',Gate1SetPoint='" & DataSplit(24) & "',Gate1Opening='" & DataSplit(25) & "',Gate1Alarm='" & DataSplit(26) & "',Gate1ElectricalAlarm='" & DataSplit(27) & "',Gate1Mode='" & DataSplit(28) & "',Gate2Pos='" & DataSplit(29) & "',Gate2SetPoint='" & DataSplit(30) & "',Gate2Opening='" & DataSplit(31) & "',Gate2Alarm='" & DataSplit(32) & "',Gate2ElectricalAlarm='" & DataSplit(33) & "',Gate2Mode='" & DataSplit(34) & "',Gate3Pos='" & DataSplit(35) & "',Gate3SetPoint='" & DataSplit(36) & "',Gate3Opening='" & DataSplit(37) & "',Gate3Alarm='" & DataSplit(38) & "',Gate3ElectricalAlarm='" & DataSplit(39) & "',Gate3Mode='" & DataSplit(40) & "',Gate4Pos='" & DataSplit(41) & "',Gate4SetPoint='" & DataSplit(42) & "',Gate4Opening='" & DataSplit(43) & "',Gate4Alarm='" & DataSplit(44) & "',Gate4ElectricalAlarm='" & DataSplit(45) & "',Gate4Mode='" & DataSplit(46) & "',InvtGenSignals='" & DataSplit(60) & "',GeneratorSignals='" & DataSplit(61) & "',OtherSignals='" & DataSplit(62) & "',SupplyVoltage='" & DataSplit(63) & "'")

                End If


                If ds.Tables(0).Rows.Count = 0 Then

                    str1 &= ("Insert Into CurrentDataEscape (mst_remote_station_id,date1,time1,UpStreamLevel,UpStreamLevelMin,UpStreamLevelMax, UpStreamSensorFail,UpStreamLevelAlarm,DownStreamLevel,DownStreamLevelMin,DownStreamLevelMax,DownStreamSensorFail,DownStreamLevelAlarm,TotalDischarge,TotalGates,Gate1Pos,Gate1SetPoint,Gate1Opening,Gate1Alarm,Gate1ElectricalAlarm,Gate1Mode,Gate2Pos,Gate2SetPoint,Gate2Opening,Gate2Alarm,Gate2ElectricalAlarm,Gate2Mode, InvtGenSignals,GeneratorSignals,OtherSignals,SupplyVoltage) values ('" & DataSplit(1) & "','" & DataSplit(2) & "','" & DataSplit(3) & "','" & DataSplit(5) & "','" & DataSplit(6) & "','" & DataSplit(7) & "','" & DataSplit(8) & "','" & DataSplit(9) & "','" & DataSplit(10) & "','" & DataSplit(11) & "','" & DataSplit(12) & "','" & DataSplit(13) & "','" & DataSplit(14) & "','" & DataSplit(21) & "','" & DataSplit(41) & "','" & DataSplit(48) & "','" & DataSplit(49) & "','" & DataSplit(50) & "','" & DataSplit(51) & "','" & DataSplit(52) & "','" & DataSplit(53) & "','" & DataSplit(54) & "','" & DataSplit(55) & "','" & DataSplit(56) & "','" & DataSplit(57) & "','" & DataSplit(58) & "','" & DataSplit(59) & "','" & DataSplit(60) & "','" & DataSplit(61) & "','" & DataSplit(62) & "','" & DataSplit(63) & "')")

                Else
                    str1 &= ("Update CurrentDataEscape set mst_remote_station_id='" & DataSplit(1) & "',date1='" & DataSplit(2) & "',time1='" & DataSplit(3) & "',UpStreamLevel='" & DataSplit(5) & "',UpStreamLevelMin='" & DataSplit(6) & "',UpStreamLevelMax='" & DataSplit(7) & "', UpStreamSensorFail='" & DataSplit(8) & "',UpStreamLevelAlarm='" & DataSplit(9) & "',DownStreamLevel='" & DataSplit(10) & "',DownStreamLevelMin='" & DataSplit(11) & "',DownStreamLevelMax='" & DataSplit(12) & "',DownStreamSensorFail='" & DataSplit(13) & "',DownStreamLevelAlarm='" & DataSplit(14) & "',TotalDischarge='" & DataSplit(21) & "',TotalGates='" & DataSplit(41) & "',Gate1Pos='" & DataSplit(48) & "',Gate1SetPoint='" & DataSplit(49) & "',Gate1Opening='" & DataSplit(50) & "',Gate1Alarm='" & DataSplit(51) & "',Gate1ElectricalAlarm='" & DataSplit(52) & "',Gate1Mode='" & DataSplit(53) & "',Gate2Pos='" & DataSplit(54) & "',Gate2SetPoint='" & DataSplit(55) & "',Gate2Opening='" & DataSplit(56) & "',Gate2Alarm='" & DataSplit(57) & "',Gate2ElectricalAlarm='" & DataSplit(58) & "',Gate2Mode='" & DataSplit(59) & "', InvtGenSignals='" & DataSplit(60) & "',GeneratorSignals='" & DataSplit(61) & "',OtherSignals='" & DataSplit(62) & "',SupplyVoltage='" & DataSplit(63) & "'")

                End If



                'da = New SqlDataAdapter("Select * From HistoryDataCR Where time1 = '" & DataSplit(3) & "'", sqlconn)


                'If ds.Tables(0).Rows.Count = "" Then


                'str1 &= ("Insert Into HistoryDataCR (mst_remote_station_id,date1,time1,UpStreamLevel,UpStreamLevelMin,UpStreamLevelMax, UpStreamSensorFail,UpStreamLevelAlarm,DownStreamLevel,DownStreamLevelMin,DownStreamLevelMax,DownStreamSensorFail,DownStreamLevelAlarm,TotalDischarge,TotalGates,Gate1Pos,Gate1SetPoint,Gate1Opening,Gate1Alarm,Gate1ElectricalAlarm,Gate1Mode,Gate2Pos,Gate2SetPoint,Gate2Opening,Gate2Alarm,Gate2ElectricalAlarm,Gate2Mode,Gate3Pos,Gate3SetPoint,Gate3Opening,Gate3Alarm,Gate3ElectricalAlarm,Gate3Mode,Gate4Pos,Gate4SetPoint,Gate4Opening,Gate4Alarm,Gate4ElectricalAlarm,Gate4Mode,InvtGenSignals,GeneratorSignals,OtherSignals,SupplyVoltage) values ('" & DataSplit(1) & "','" & DataSplit(2) & "','" & DataSplit(3) & "','" & DataSplit(5) & "','" & DataSplit(6) & "','" & DataSplit(7) & "','" & DataSplit(8) & "','" & DataSplit(9) & "','" & DataSplit(10) & "','" & DataSplit(11) & "','" & DataSplit(12) & "','" & DataSplit(13) & "','" & DataSplit(14) & "','" & DataSplit(15) & "','" & DataSplit(22) & "','" & DataSplit(23) & "','" & DataSplit(24) & "','" & DataSplit(25) & "','" & DataSplit(26) & "','" & DataSplit(27) & "','" & DataSplit(28) & "','" & DataSplit(29) & "','" & DataSplit(30) & "','" & DataSplit(31) & "','" & DataSplit(32) & "','" & DataSplit(33) & "','" & DataSplit(34) & "','" & DataSplit(35) & "','" & DataSplit(36) & "','" & DataSplit(37) & "','" & DataSplit(38) & "','" & DataSplit(39) & "','" & DataSplit(40) & "','" & DataSplit(41) & "','" & DataSplit(42) & "','" & DataSplit(43) & "','" & DataSplit(44) & "','" & DataSplit(45) & "','" & DataSplit(46) & "','" & DataSplit(60) & "','" & DataSplit(61) & "','" & DataSplit(62) & "','" & DataSplit(63) & "')")

                'Else
                'str1 &= ("Update HistoryDataCR set mst_remote_station_id= '" & DataSplit(1) & "',date1='" & DataSplit(2) & "',time1='" & DataSplit(3) & "',UpStreamLevel='" & DataSplit(5) & "',UpStreamLevelMin='" & DataSplit(6) & "',UpStreamLevelMax='" & DataSplit(7) & "', UpStreamSensorFail='" & DataSplit(8) & "',UpStreamLevelAlarm='" & DataSplit(9) & "',DownStreamLevel='" & DataSplit(10) & "',DownStreamLevelMin='" & DataSplit(11) & "',DownStreamLevelMax='" & DataSplit(12) & "',DownStreamSensorFail='" & DataSplit(13) & "',DownStreamLevelAlarm='" & DataSplit(14) & "',TotalDischarge='" & DataSplit(15) & "',TotalGates='" & DataSplit(22) & "',Gate1Pos='" & DataSplit(23) & "',Gate1SetPoint='" & DataSplit(24) & "',Gate1Opening='" & DataSplit(25) & "',Gate1Alarm='" & DataSplit(26) & "',Gate1ElectricalAlarm='" & DataSplit(27) & "',Gate1Mode='" & DataSplit(28) & "',Gate2Pos='" & DataSplit(29) & "',Gate2SetPoint='" & DataSplit(30) & "',Gate2Opening='" & DataSplit(31) & "',Gate2Alarm='" & DataSplit(32) & "',Gate2ElectricalAlarm='" & DataSplit(33) & "',Gate2Mode='" & DataSplit(34) & "',Gate3Pos='" & DataSplit(35) & "',Gate3SetPoint='" & DataSplit(36) & "',Gate3Opening='" & DataSplit(37) & "',Gate3Alarm='" & DataSplit(38) & "',Gate3ElectricalAlarm='" & DataSplit(39) & "',Gate3Mode='" & DataSplit(40) & "',Gate4Pos='" & DataSplit(41) & "',Gate4SetPoint='" & DataSplit(42) & "',Gate4Opening='" & DataSplit(43) & "',Gate4Alarm='" & DataSplit(44) & "',Gate4ElectricalAlarm='" & DataSplit(45) & "',Gate4Mode='" & DataSplit(46) & "',InvtGenSignals='" & DataSplit(60) & "',GeneratorSignals='" & DataSplit(61) & "',OtherSignals='" & DataSplit(62) & "',SupplyVoltage='" & DataSplit(63) & "'")

                'End If

                'If ds.Tables(0).Rows.Count = '' Then
                'str1 &= ("Insert Into HistoryDataEscape (mst_remote_station_id,date1,time1,UpStreamLevel,UpStreamLevelMin,UpStreamLevelMax, UpStreamSensorFail,UpStreamLevelAlarm,DownStreamLevel,DownStreamLevelMin,DownStreamLevelMax,DownStreamSensorFail,DownStreamLevelAlarm,TotalDischarge,TotalGates,Gate1Pos,Gate1SetPoint,Gate1Opening,Gate1Alarm,Gate1ElectricalAlarm,Gate1Mode,Gate2Pos,Gate2SetPoint,Gate2Opening,Gate2Alarm,Gate2ElectricalAlarm,Gate2Mode, InvtGenSignals,GeneratorSignals,OtherSignals,SupplyVoltage) values ('" & DataSplit(1) & "','" & DataSplit(2) & "','" & DataSplit(3) & "','" & DataSplit(5) & "','" & DataSplit(6) & "','" & DataSplit(7) & "','" & DataSplit(8) & "','" & DataSplit(9) & "','" & DataSplit(10) & "','" & DataSplit(11) & "','" & DataSplit(12) & "','" & DataSplit(13) & "','" & DataSplit(14) & "','" & DataSplit(21) & "','" & DataSplit(41) & "','" & DataSplit(48) & "','" & DataSplit(49) & "','" & DataSplit(50) & "','" & DataSplit(51) & "','" & DataSplit(52) & "','" & DataSplit(53) & "','" & DataSplit(54) & "','" & DataSplit(55) & "','" & DataSplit(56) & "','" & DataSplit(57) & "','" & DataSplit(58) & "','" & DataSplit(59) & "','" & DataSplit(60) & "','" & DataSplit(61) & "','" & DataSplit(62) & "','" & DataSplit(63) & "') ")
                'End If


                str1 &= "commit tran;"

                Dim cmd As New SqlCommand

                If (sqlconn.State = ConnectionState.Open) Then
                    cmd.Connection = sqlconn
                    cmd.CommandType = CommandType.Text
                    cmd.CommandText = str1
                    cmd.ExecuteNonQuery()
                    MessageBox.Show("record inserted successfully")

                    sqlconn.Close()

                End If


            End Using

1 个答案:

答案 0 :(得分:0)

为什么不创建一个Insert触发器来做同样的事情?在触发器中,您可以检查该时间段是否存在历史记录,如果不存在,请插入,否则更新它。它应该比在服务器上来回执行多个数据操作更快。