如何在ID中保存没有重复值的记录 [英] How do I save a record without duplicate values in ID

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

问题描述

 私有  Sub  cmdsave_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs) Handles  cmdsave.Click 
< span class =code-keyword>如果 txtid.Text = txtname.Text = txtmn.Text = txtln.Text = cbogender.Text = cbosub.Text = cbopos.Text = 然后
MsgBox( 完成所需的所有字段,MsgBoxStyle.Information,
txtid.Focus()
ElseIf IsNumeric(txtid.Text)= False 然后
MsgBox( ID号码应仅为数字
ElseIf sb = True 然后
mycmd.Connection = mycon
mycmd.CommandText = 插入`tbluseradmin`(`ID`,`Name`,`MiddleName`, `LastName`,`Gender`,`Subject`,`Position`)值('& txtid.Text& ','& txtname.Text& ','& txtmn.Text& ','& txtln.Text& ','& cbogender.Text& ','& cbosub.Text& ','& cbopos.Text& ')
mycmd.ExecuteNonQuery()
mycon.Close()
My.Computer.FileSystem.CopyFile(ofd.FileName,Application.StartupPath& \ images \\& txtid.Text& 。jpg
MsgBox( Record Save,MsgBoxStyle.Information,
调用 connect()
致电 display()
致电 textboxlocked()
调用 buttonunlock()
调用 cmdlock()
调用 cmd2unlock()
否则
mycmd.Connection = mycon
mycmd.CommandText = update`tbluseradmin` set`ID` ='& txtid.Text& ',`Name` ='& txtname.Text& ',`MiddleName` ='& txtmn.Text& ',`LastName` ='& txtln.Text& ',`Gender` ='& cbogender.Text& 'where`ID` ='& idnum& '
mycmd.ExecuteNonQuery()
mycon.Close()
My.Computer.FileSystem.CopyFile(ofd.FileName,Application.StartupPath& \ imagesm & txtid.Text& 。jpg
MsgBox ( 记录更新,MsgBoxStyle.Information,
致电 connect()
调用 display()
调用 textboxlocked()
调用 buttonunlock()
调用 cmdlock()
致电 cmd2unlock()


结束 如果
结束 Sub

解决方案

如果您的 ID 列是自动递增标识,那么就不要插入它(在中INSERT 语句)并且数据库引擎将自动提供下一个值。



如果没有,那么尝试将其设置为标识并自动加入第一个你的桌子:

http://stackoverflow.com/questions/17291680/how-to-alter-a-table-for-identity-specification-is-identity-sql-server [ ^

Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
        If txtid.Text = "" Or txtname.Text = "" Or txtmn.Text = "" Or txtln.Text = "" Or cbogender.Text = "" Or cbosub.Text = "" Or cbopos.Text = "" Then
            MsgBox("Complete All Fields Needed", MsgBoxStyle.Information, "")
            txtid.Focus()
        ElseIf IsNumeric(txtid.Text) = False Then
            MsgBox("ID number should be numbers only")
        ElseIf sb = True Then
            mycmd.Connection = mycon
            mycmd.CommandText = "insert into `tbluseradmin`(`ID`,`Name`,`MiddleName`,`LastName`,`Gender`,`Subject`,`Position`)values('" & txtid.Text & "','" & txtname.Text & "','" & txtmn.Text & "','" & txtln.Text & "','" & cbogender.Text & "','" & cbosub.Text & "','" & cbopos.Text & "')"
            mycmd.ExecuteNonQuery()
            mycon.Close()
            My.Computer.FileSystem.CopyFile(ofd.FileName, Application.StartupPath & "\images\" & txtid.Text & ".jpg")
            MsgBox("Record Save", MsgBoxStyle.Information, "")
            Call connect()
            Call display()
            Call textboxlocked()
            Call buttonunlock()
            Call cmdlock()
            Call cmd2unlock()
        Else
            mycmd.Connection = mycon
            mycmd.CommandText = "update `tbluseradmin` set`ID`='" & txtid.Text & "',`Name`='" & txtname.Text & "',`MiddleName`='" & txtmn.Text & "',`LastName`='" & txtln.Text & "',`Gender`='" & cbogender.Text & "'where`ID`='" & idnum & "'"
            mycmd.ExecuteNonQuery()
            mycon.Close()
            My.Computer.FileSystem.CopyFile(ofd.FileName, Application.StartupPath & "\images\" & txtid.Text & ".jpg")
            MsgBox("Record Update", MsgBoxStyle.Information, "")
            Call connect()
            Call display()
            Call textboxlocked()
            Call buttonunlock()
            Call cmdlock()
            Call cmd2unlock()


        End If
    End Sub

解决方案

If your ID column is an auto incrementing identity, then just don't insert it (in the INSERT statement) and the database engine will supply the next value automatically.

If not then try setting it to identity and auto incrmenting first in your table with :
http://stackoverflow.com/questions/17291680/how-to-alter-a-table-for-identity-specification-is-identity-sql-server[^]


这篇关于如何在ID中保存没有重复值的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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