如何防止重复数据输入? [英] how to prevent from Duplicate data entry?

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

问题描述

我还是新手,所以请帮忙。我不知道如何防止数据重复。这是我的代码。



 私有  Sub  Button1_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 addbtn.Click 

如果 myconn.State = ConnectionState.Open 然后
myconn.Close()
结束 如果

myconn.Open()
Dim i As < span class =code-keyword> Integer = mycom.ExecuteNonQuery()
Dim Q 正如 字符串
' '------------------------- -------------------------------------------------- ----------
' '------ -------------------------------------------------- -----------------------------

Q = INSERT INTO tblelogin(用户名,名称,密码,帐户类型)VALUES('
Q = Q + txtuname.Text + ',' + txtname.Text + ',' + txtpass.Text + ',' + accountbox.Text + ')

如果 txtpass.Text<> txtconfirm.Text 然后
MsgBox( password不匹配。请再次输入。
txtpass.Text =
txtconfirm.Text =

结束 如果

如果 txtuname .Text = _
txtpass.Text = _
txtname.Text = _
txtconfirm.Text = _
accountbox.Text = 然后
MsgBox( 字段上的输入数据。

退出 Sub
Else

mycom.CommandText = Q

i = mycom.ExecuteNonQuery()
如果(i> 0 然后
MessageBox.Show( 记录成功插入
否则
MessageBox.Show ( 记录未插入

myconn.Close()

结束 如果
结束 如果

解决方案


u可以在表中使用约束。对用户名进行唯一约束。每当你尝试插入重复的条目时,数据库都会显示错误。在代码中处理该异常


在表中创建用户名字段主键;因此,当您添加现有用户名时,它将生成异常,您可以捕获并执行所需的...



请参阅此链接以执行此操作SQL Server管理工作室



http://sqldud.blogspot.ae/2010/01/assign-primary-key-in-sql-server.html

I'm still new to this so please help. i have no idea how to prevent data from duplicating. this is my code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click

    If myconn.State = ConnectionState.Open Then
        myconn.Close()
    End If
    
    myconn.Open()
    Dim i As Integer = mycom.ExecuteNonQuery()
    Dim Q As String
    ''-------------------------------------------------------------------------------------
    ''-------------------------------------------------------------------------------------
    
    Q = "INSERT INTO tblelogin(Username,name,Password,Accounttype) VALUES('"
    Q = Q + txtuname.Text + "','" + txtname.Text + "','" + txtpass.Text + "','" + accountbox.Text + "')"
    
    If txtpass.Text <> txtconfirm.Text Then
        MsgBox("password mismatch. please enter again.")
        txtpass.Text = ""
        txtconfirm.Text = ""
    
    End If
    
    If txtuname.Text = "" Or _
        txtpass.Text = "" Or _
        txtname.Text = "" Or _
        txtconfirm.Text = "" Or _
        accountbox.Text = "" Then
        MsgBox("input data on the field.")
        
        Exit Sub
    Else
    
        mycom.CommandText = Q
        
        i = mycom.ExecuteNonQuery()
        If (i > 0) Then
            MessageBox.Show("Record is Successfully Inserted")
        Else
            MessageBox.Show("Record is not Inserted")
            
            myconn.Close()
        
        End If
    End If

解决方案

Hi,
u can use constraints in table. Make a unique constraint on username. whenever u try to insert duplicate entry then database will show error. Handle that exception in code


Make the Username field Primary key in Table; So that when you add the existing user name, it will generate an exception, you can catch it and do the needful...

See this link to do it in SQL Server Management Studio

http://sqldud.blogspot.ae/2010/01/assign-primary-key-in-sql-server.html


这篇关于如何防止重复数据输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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