处理重复值 [英] Dealing with duplicate values

查看:48
本文介绍了处理重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我向数据库插入了15个不同的用户详细信息,该表格看起来像一个Excel工作表,我能够处理重复的键冲突错误,为此我正在执行以下操作

Hi, in my application i am inserting 15 different user details to the DB, the form looks like an excel sheet, i am able to handle duplicate key violation error, and to that i am doing the following

Private Sub btnSubmit_CrtMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit_CrtMul.Click
        Dim cnt As Integer = 0
        Try
            If chkBox1_Crtusr.Checked And IsNumeric(txtEmpID_1.Text) Then
                rtrnQry(txtUsrNm_1.Text, txtPswd_1.Text, txtFName_1.Text, txtLName_1.Text, txtEmpID_1.Text, cmbProNm1.SelectedItem, cmbAdmin1.SelectedItem, 1)
                If empIdSts(txtEmpID_1) = True Then
                    Call ClearData(1)
                    cnt = cnt + 1
                End If
            ElseIf Not IsNumeric(txtEmpID_1.Text) And txtEmpID_1.TextLength > 0 Then
           Call isNumSts(lblsts1, chkBox1_Crtusr)
            End If
If chkBox2_Crtusr.Checked And IsNumeric(txtEmpID_2.Text) Then rtrnQry(txtUsrNm_2.Text, txtPswd_2.Text, txtFName_2.Text,txtLName_2.Text, txtEmpID_2.Text,cmbProNm2.SelectedItem, cmbAdmin2.SelectedItem, 2)
If empIdSts(txtEmpID_2) = True Then
      Call ClearData(2)
      cnt = cnt + 1
End If
ElseIf Not IsNumeric(txtEmpID_2.Text) And txtEmpID_2.TextLength > 0 Then
Call isNumSts(lblsts2, chkBox2_Crtusr)
End If


我正在使用以下代码为所有15个用户执行此操作,我将使用以下代码来处理重复的键值


I am doing this for all 15 users, using the following code i m handing duplicate key value

Public Sub chkEmpIDSts(ByVal txtbox As TextBox, ByVal lblSts As Label, ByVal chkBox As CheckBox)
       Try
           Dim empIDLngth As String = ""
           Using conn As New SqlConnection(sqlConnStr)
               conn.Open()
               Using cmd As New SqlCommand("rtnLngth", conn)
                   With cmd
                       .CommandType = CommandType.StoredProcedure
                       .Parameters.AddWithValue("@empid", txtbox.Text)
                       empIDLngth = .ExecuteScalar()
                   End With
                   If txtbox.TextLength > 0 Then
                       If empIDLngth = 0 Then
                           lblSts.Text = "Available"
                           lblSts.ForeColor = Color.LimeGreen
                           If chkBox.Enabled = False Then chkBox.Enabled = True
                       Else

                           lblSts.Text = "Not available"
                           lblSts.ForeColor = Color.Red
                           If chkBox.Checked Then
                               chkBox.CheckState = CheckState.Unchecked
                               chkBox.Enabled = False
                           End If


到目前为止,一切都进展顺利,但是我的问题是,如果有人为两个不同的用户两次输入相同的新empId,那么我的代码将无法处理它,因为它检查了Empid是否已经在数据库和用户中使用了.将能够插入它-因此将再次得到重复的密钥冲突错误.请向我发布解决此问题的想法.谢谢


Every thing is going good till now, but my question is,if some one enters the same new empId twice for two different user, than my code wont be able to handle it as it checks if the empid is already thr in DB, and user will be able to insert it - hence will again get a duplicate key violation error. Please post me an idea to resolve the problem.Thanks

推荐答案

请参见,
有两种方法.您可以通过前端处理,也可以在数据库中插入数据时进行处理.

如果存在(选择*从TABNAME WHERE empid =''e01'')

在您的CRUD中,您可以在插入员工之前指定此名称.

在前端,您还可以检查此ID是否在数据库中已经存在的员工列表中.

只需select empid from TABNAME并在用户尝试从UI输入数据时从创建的列表中查找.
See,
There are two way. Either you handle through front end or you do it while inserting data in the database.

If Exists(select * From TABNAME WHERE empid=''e01'')

In your CRUD, you can specify this before you insert the employee.

In your frontend, you can also check whether the this id is in the employee list already in the database.

Just select empid from TABNAME and find from the list that you create while user try to enter data from the UI.


这篇关于处理重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆