如果存在则要更新记录,如果不存在则插入

时间:2016-04-27 07:30:09

标签: vb.net

想要更新记录(如果存在)并插入(如果不存在)vsolv_trn_tsalesregdump tsalesregdump_name在表中是唯一的vsolv_trn_tsalesregdump

当我点击复选框时应检查其他表上的特定行以及如何在表单循环中使用它进行循环我可以使用" tsalesdump_date,tsalesdump_name,executivename_executive"这三个如果值是相同的去更新,否则它不相同它的插入移动我怎么能写它可以任何1帮我

如果我使用两个表一个临时和其他1是原始\ temp table = vsolv_tmp_tsalesdump orginal table = vsolv_trn_tsalesregdump。

如果我已经将数据保存在原始表中,再次如果我为临时表和radgrid中的视图分配数据如果选择并提交按钮,那么去检查原始表,如果值已经存在则更新它否则插入如何使用它

Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_Submit.Click
    lblerrmsg.Text = ""
    Dim chk As CheckBox
    Dim lbtn As LinkButton
    Dim Res As Long
    Dim lobjrow As DataRow
    Dim lObjErrTable As New DataTable
    Dim a As Integer = 0
    Dim dtcarbooking As New DataTable
    Dim dtDumpBooking As New DataTable
    Dim lsRefid As String = String.Empty
    Dim dtdumpsample As New DataTable
    Dim name As String = String.Empty

    If Check_Validation() = True Then
        Exit Sub
    End If
    Gobjdbconn.OpenConn()

    lObjErrTable = Error_TabelFill()

    For i As Integer = 0 To GVUpload.Items.Count - 1
        chk = GVUpload.Items(i).FindControl("chkupload")
        If chk.Checked = True Then

                lsRefid = GVUpload.Items(i).GetDataKeyValue("tsalesdump_gid").ToString()


            MsSql = ""
            MsSql &= "Select tsalesdump_gid,tsalesdump_date,tsalesdump_name,executivename_executive,tsalesdump_vch_no,tsalesdump_debit,tsalesdump_credit"
            MsSql &= " from vsolv_tmp_tsalesdump as a"
            MsSql &= " left join vsolv_trn_executivename as b on a.tsalesdump_name = b.executivename_particulars"
            MsSql &= " where tsalesdump_gid = '" & lsRefid & "' and tsalesdump_isremoved = 'N'"
            dtDumpBooking = Gobjdbconn.GetDataTable(MsSql)


            MsSql = ""
            MsSql &= "Insert into vsolv_trn_tsalesregdump(tsalesregdump_date,tsalesregdump_name,tsalesregdump_executive,tsalesregdump_vch_no"
            MsSql &= " ,tsalesregdump_debit,tsalesregdump_credit,tsalesregdump_importby)"
            MsSql &= " Values ('" & Format(CDate(dtDumpBooking.Rows(0).Item("tsalesdump_date")), "yyyy-MMM-dd").ToString() & "'"
            MsSql &= ",'" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "'"
            MsSql &= ",'" & dtDumpBooking.Rows(0).Item("executivename_executive").ToString() & "'"
            MsSql &= " ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_vch_no").ToString() & "'"
            MsSql &= " ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_debit").ToString() & "','" & dtDumpBooking.Rows(0).Item("tsalesdump_credit").ToString() & "','SIVA')"

            MnResult = Gobjdbconn.ExecuteNonQuerySQL(MsSql)

            If MnResult = 1 Then
                MsSql = ""
                MsSql &= " Delete from vsolv_tmp_tsalesdump where tsalesdump_gid = '" & lsRefid & "'"
                Gobjdbconn.ExecuteNonQuerySQL(MsSql)
            Else
                lobjrow = lObjErrTable.NewRow()
                a = a + 1
                lobjrow("Sno") = a
                lobjrow("Booking Ref No") = dtcarbooking.Rows(0).Item("tsalesregdump_name").ToString
                lobjrow("Description") = "Duplicate Record"
                lObjErrTable.Rows.Add(lobjrow)
            End If

        End If
    Next


    POPSummary()

    If Not lObjErrTable Is Nothing Then
        If lObjErrTable.Rows.Count > 0 Then
            Call Pop_Data(lObjErrTable)
        End If
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

如果你想更新记录,如果已经存在,那么在SQL中插入则可以通过以下方式实现:你需要为插入命令更改MsSql字符串

MsSql = "" + _
"IF EXISTS(Select tsalesregdump_name From vsolv_trn_tsalesregdump Where tsalesregdump_name = '" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "') " + _
"BEGIN " + _
"   Update vsolv_trn_tsalesregdump " + _
"   Set tsalesregdump_date = '" & Format(CDate(dtDumpBooking.Rows(0).Item("tsalesdump_date")), "yyyy-MMM-dd").ToString() & "' " + _
"       ,tsalesregdump_executive = '" & dtDumpBooking.Rows(0).Item("executivename_executive").ToString() & "' " + _
"       ,tsalesregdump_vch_no = '" & dtDumpBooking.Rows(0).Item("tsalesdump_vch_no").ToString() & "' " + _
"       ,tsalesregdump_debit = '" & dtDumpBooking.Rows(0).Item("tsalesdump_debit").ToString() & "' " + _
"       ,tsalesregdump_credit = '" & dtDumpBooking.Rows(0).Item("tsalesdump_credit").ToString() & "' " + _
"       ,tsalesregdump_importby = 'SIVA' " + _
"   Where tsalesregdump_name = '" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "' " + _
"END " + _
"ELSE " + _
"BEGIN " + _
"   Insert into vsolv_trn_tsalesregdump(tsalesregdump_date,tsalesregdump_name,tsalesregdump_executive,tsalesregdump_vch_no " + _
"       ,tsalesregdump_debit,tsalesregdump_credit,tsalesregdump_importby) " + _
"   Values ('" & Format(CDate(dtDumpBooking.Rows(0).Item("tsalesdump_date")), "yyyy-MMM-dd").ToString() & "' " + _
"   ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "' " + _
"   ,'" & dtDumpBooking.Rows(0).Item("executivename_executive").ToString() & "' " + _
"   ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_vch_no").ToString() & "' " + _
"   ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_debit").ToString() & "','" & dtDumpBooking.Rows(0).Item("tsalesdump_credit").ToString() & "','SIVA') " + _
"END "

MnResult = Gobjdbconn.ExecuteNonQuerySQL(MsSql)

我假设tsalesregdump_name是唯一的。