本文介绍了如何使用我的代码防止重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Imports MySql.Data.MySqlClient
Public Class UserAdmin
Public mycon As New MySqlConnection
Public myadap As New MySqlDataAdapter
Public mycmd As New MySqlCommand
Public myds As New DataSet
Public rec As Integer
Public sb As Boolean
Public idnum As String
Public trec As Integer

Sub connect()
        mycon = New MySqlConnection
        mycon.ConnectionString = "server=localhost;user id=root;password=;database=dbase"
        mycon.Open()
        mycmd.Connection = mycon
        mycmd.CommandText = "select*from tbluseradmin"
        myds = New DataSet
        myadap.SelectCommand = mycmd
        myadap.Fill(myds, "tbl")
        trec = myds.Tables("tbl").Rows.Count - 1
        rec = 0
End Sub

Private Sub UserAdmin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Call connect()
       Call display()
end sub

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

推荐答案

Dim result() As DataRow = myds.Tables(0).select("ID = '"+ txtid.Text +"'")
If (result.Length == 0) 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, "")
Else
	MsgBox("ID already exists")
End





希望上面的代码有帮助。



Hope the above code helps.


这篇关于如何使用我的代码防止重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:56