本文介绍了如何验证重复的条目VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我不知道如何防止VB.NET中的重复条目验证 我使用excel表来保存我的数据,并且我想验证ID(TxtId.Text)到防止重复输入。 这是我的代码: Imports System.Data.OleDb Imports System Imports System.Text Imports System.IO Imports System.Net Imports System.Net.Sockets Imports System.Collections.Generic Imports System.ComponentModel Imports System.Drawing Imports System.Threading Imports Microsoft.VisualBasic Imports System.Text.RegularExpressions Imports System.Windows.Forms Public 类 Form1 私有 函数 vld( ByVal ParamArray ctl()作为 对象)作为 布尔值 对于 i 作为 整数 = 0 UBound(ctl) 如果 ctl(i).text = 然后 ErrorProvider1.SetError(ctl(i),c tl(i).tag) 返回 错误 退出 对于 结束 如果 下一步 返回 True 结束 功能 Dim cn 作为 新 OleDbConnection Dim cm 作为 新 OleDbCommand Dim da As OleDbDataAdapter Dim dt As 新 DataTable 私有 Sub Form1_FormClosing( ByVal sender As 对象, ByVal e As System.Windows.Forms.FormClosingEventArgs)句柄 我 .FormClosing cn.Close() 结束 Sub 私有 Sub Form1_Load( ByVal sender As Object , ByVal e 作为 EventArgs)句柄 MyBase .Load TxtExamtime.Format = DateTimePickerFormat.Custom TxtExamtime.CustomFormat = hh:MM tt cn.ConnectionString = provider = microsoft.jet .oledb.4.0;数据源= C:\ psave \New folder \ save.xls;扩展属性= Excel 8.0; cn.Open() FillDataGridView( 选择ID,姓氏,名字,性别,DOB,考试日期,考试时间,街道名称,房子Nr,请来自 $]) 结束 Sub 私有 Sub FillDataGridView( ByVal 查询作为 字符串) da = 新 OleDbDataAdapter(Query,cn) dt.Clear() da.Fill(dt) 使用 DataGridView1 .DataSource = dt .Columns( 0 )。HeaderText = ID .Columns( 1 )。HeaderText = 系列名称 .Columns( 2 ) .HeaderText = 给定名称 .Columns( 3 )。HeaderText = 性别 .Columns( 4 )。HeaderText = DOB .Columns( 5 )。HeaderText = 考试日期 .Columns( 6 )。HeaderText = 考试时间 .Columns( 7 )。HeaderText = 街道名称 .Columns( 8 )。HeaderText = House Nr .Columns( 9 )。HeaderText = 请 .Columns( 10 )。HeaderText = City .Columns( 10 )。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 结束 使用 结束 Sub 私有 Sub BtnSearch_Click( ByVal sender As Object , ByVal e As EventArgs)句柄 BtnSearch.Click 尝试 FillDataGridView( select * from [edit $]其中ID ='& TxtId.Text& ') TxtFamilyname.Text = dt.Rows( 0 )。Item( 1 ) TxtGivenname.Text = dt.Rows( 0 )。项目( 2 ) TxtGender.Text = dt.Rows( 0 ).Item( 3 ) TxtDob.Text = dt.Rows( 0 ).Item( 4 ) TxtExamdate.Text = dt.Rows( 0 )。 ( 5 ) TxtExamtime.Text = dt.Rows( 0 )。项目( 6 ) TxtStreet.Text = dt.Rows( 0 )。Item( 7 ) TxtHouse.Text = dt.Rows( 0 )。Item( 8 ) TxtPlz.Text = dt.Rows( 0 )。Item( 9 ) TxtCity.Text = dt.Rows( 0 )。项目( 10 ) Catch ex As 异常 MsgBox(ex.Message,MsgBoxStyle .Critical,Text) 结束 尝试 结束 Sub 私有 Sub BtnSave_Click( ByVal sender As 对象, ByVal e As EventArgs)句柄 BtnSave.Click 如果 vld(TxtId,TxtFamilyname,TxtGivenname,TxtGender,TxtDob,TxtExamdate,TxtExamtime,TxtStreet,TxtHouse,TxtPlz,TxtCity)= False 然后 退出 Sub 其他 结束 如果 尝试 使用 cm .Connection = cn .CommandText = 插入[edit $] values('& TxtId.Text& ','& TxtFamilyname.Text& ','& TxtGivenname.Text& ','& TxtGender.Text& ','& TxtDob.Text& ','& TxtExamdate.Text& ','& TxtExamtime.Text& ','& TxtStreet.Text& ','& TxtHouse.Text& ','& TxtPlz.Text& ','& TxtCity.Text& ') .ExecuteNonQuery() 结束 使用 FillDataGridView( select * from [edit $]) Catch ex As 异常 MsgBox(ex.Message,MsgBoxStyle.Critical,Text) 返回 结束 尝试 MsgBox( 成功保存!,MsgBoxStyle.Information,Text) 结束 Sub 私有 Sub BtnUpdate_Click( ByVal 发件人作为 对象, ByVal e As EventArgs)句柄 Btnupdate.Click 尝试 使用 cm .Connection = cn .CommandText = 更新[edit $] set [Family Name] ='& TxtFamilyname.Text& ',[Given Name] ='& TxtGivenname.Text& ',[Gender] ='& TxtGender.Text& ',[DOB] ='& TxtDob.Text& ',[考试日期] ='& TxtExamdate.Text& ',[考试时间] ='& TxtExamtime.Text& ',[街道名称] ='& TxtStreet.Text& ',[House Nr] ='& TxtHouse.Text& ',[please] ='& TxtPlz.Text& ',[CITY] ='& TxtCity.Text& '其中ID ='& TxtId.Text& ' .ExecuteNonQuery() 结束 使用 FillDataGridView( select * from [edit $]) MsgBox( 成功更新!,MsgBoxStyle.Information,Text) Catch ex As 异常 MsgBox(ex.Message,MsgBoxStyle.Information,Text) 返回 结束 尝试 结束 Sub 私有 Sub BtnClose_Click( ByVal sender 作为 对象, ByVal e As EventArgs)句柄 BtnClose.Click 关闭() 结束 Sub 私有 Sub BtnClear_Click( ByVal sender 作为 对象, ByVal e 作为 EventArgs)句柄 BtnClear.Click TxtId.Clear() TxtFamilyname.Clear() TxtGivenname。 Clear() TxtStreet.Clear() TxtHouse.Clear() TxtPlz.Clear() TxtCity.Clear() 查看DataGridView中的所有数据 FillDataGridView( select * from [edit $]) End Sub End Class 解决方案 ) 结束 Sub 私有 Sub FillDataGridView( ByVal 查询作为 字符串) da = 新 OleDbDataAdapter(查询,cn) dt.Clear() da.Fill (dt) 使用 DataGridView1 .DataSource = dt .Columns( 0 )。HeaderText = ID .Columns( 1 )。HeaderText = 系列名称 .Columns( 2 )。HeaderText = 给定名称 .Columns( 3 ) .HeaderText = 性别 .Columns( 4 )。HeaderText = DOB .Columns( 5 )。HeaderText = 考试日期 .Columns( 6 )。HeaderText = 考试时间 .Columns( 7 )。HeaderText = 街道名称 .Columns( 8 )。HeaderText = House Nr .Columns( 9 )。HeaderText = please .Columns( 10 )。HeaderText = City .Columns( 10 )。AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill 结束 使用 结束 Sub 私有 Sub BtnSearch_Click( ByVal sender 正如 对象, ByVal e As EventArgs)句柄 BtnSearch.Click 尝试 FillDataGridView( select * from [edit where ID ='& TxtId.Text& ') TxtFamilyname.Text = dt.Rows( 0 )。Item( 1 ) TxtGivenname.Text = dt.Rows( 0 )。项目( 2 ) TxtGender.Text = dt.Rows( 0 ).Item( 3 ) TxtDob.Text = dt.Rows( 0 ).Item( 4 ) TxtExamdate.Text = dt.Rows( 0 )。 ( 5 ) TxtExamtime.Text = dt.Rows( 0 )。项目( 6 ) TxtStreet.Text = dt.Rows( 0 )。Item( 7 ) TxtHouse.Text = dt.Rows( 0 )。Item( 8 ) TxtPlz.Text = dt.Rows( 0 )。Item( 9 ) TxtCity.Text = dt.Rows( 0 )。项目( 10 ) Catch ex As 异常 MsgBox(ex.Message,MsgBoxStyle .Critical,Text) 结束 尝试 结束 Sub 私有 Sub BtnSave_Click( ByVal sender As 对象, ByVal e As EventArgs)句柄 BtnSave.Click 如果 vld(TxtId,TxtFamilyname,TxtGivenname,TxtGender,TxtDob,TxtExamdate,TxtExamtime,TxtStreet,TxtHouse,TxtPlz,TxtCity)= False 然后 退出 Sub 其他 结束 如果 尝试 使用 cm .Connection = cn .CommandText = 插入[edit values('& TxtId.Text& ','& TxtFamilyname.Text& ','& TxtGivenname.Text& ','& TxtGender.Text& ','& TxtDob.Text& ','& TxtExamdate.Text& ','& TxtExamtime.Text& ','& TxtStreet.Text& ','& TxtHouse.Text& ','& TxtPlz.Text& ','& TxtCity.Text& ') .ExecuteNonQuery() 结束 使用 FillDataGridView( select * from [edit I don't know how to prevent Duplicate entry validation in VB.NETAm using an excel sheet to save my data and there i want to Validate "ID"(TxtId.Text) to prevent duplicate entries.Here is my CODE:Imports System.Data.OleDbImports SystemImports System.TextImports System.IOImports System.NetImports System.Net.SocketsImports System.Collections.GenericImports System.ComponentModelImports System.DrawingImports System.ThreadingImports Microsoft.VisualBasicImports System.Text.RegularExpressionsImports System.Windows.FormsPublic Class Form1 Private Function vld(ByVal ParamArray ctl() As Object) As Boolean For i As Integer = 0 To UBound(ctl) If ctl(i).text = "" Then ErrorProvider1.SetError(ctl(i), ctl(i).tag) Return False Exit For End If Next Return True End Function Dim cn As New OleDbConnection Dim cm As New OleDbCommand Dim da As OleDbDataAdapter Dim dt As New DataTable Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing cn.Close() End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load TxtExamtime.Format = DateTimePickerFormat.Custom TxtExamtime.CustomFormat = "hh:MM tt" cn.ConnectionString = "provider=microsoft.jet.oledb.4.0; Data Source=C:\psave\New folder\save.xls;Extended Properties=Excel 8.0;" cn.Open() FillDataGridView("select ID, Family Name, Given Name, Gender, DOB, Exam Date, Exam Time, Street Name, House Nr, please, City from [edit$]") End Sub Private Sub FillDataGridView(ByVal Query As String) da = New OleDbDataAdapter(Query, cn) dt.Clear() da.Fill(dt) With DataGridView1 .DataSource = dt .Columns(0).HeaderText = "ID" .Columns(1).HeaderText = "Family Name" .Columns(2).HeaderText = "Given Name" .Columns(3).HeaderText = "Gender" .Columns(4).HeaderText = "DOB" .Columns(5).HeaderText = "Exam Date" .Columns(6).HeaderText = "Exam Time" .Columns(7).HeaderText = "Street Name" .Columns(8).HeaderText = "House Nr" .Columns(9).HeaderText = "please" .Columns(10).HeaderText = "City" .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill End With End Sub Private Sub BtnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnSearch.Click Try FillDataGridView("select * from [edit$] where ID='" & TxtId.Text & "'") TxtFamilyname.Text = dt.Rows(0).Item(1) TxtGivenname.Text = dt.Rows(0).Item(2) TxtGender.Text = dt.Rows(0).Item(3) TxtDob.Text = dt.Rows(0).Item(4) TxtExamdate.Text = dt.Rows(0).Item(5) TxtExamtime.Text = dt.Rows(0).Item(6) TxtStreet.Text = dt.Rows(0).Item(7) TxtHouse.Text = dt.Rows(0).Item(8) TxtPlz.Text = dt.Rows(0).Item(9) TxtCity.Text = dt.Rows(0).Item(10) Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, Text) End Try End Sub Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnSave.Click If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then Exit Sub Else End If Try With cm .Connection = cn .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )" .ExecuteNonQuery() End With FillDataGridView("select * from [edit$]") Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, Text) Return End Try MsgBox("succefully Saved!", MsgBoxStyle.Information, Text) End Sub Private Sub BtnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btnupdate.Click Try With cm .Connection = cn .CommandText = "Update [edit$] set [Family Name]= '" & TxtFamilyname.Text & "',[Given Name] = '" & TxtGivenname.Text & "',[Gender] = '" & TxtGender.Text & "',[DOB] = '" & TxtDob.Text & "',[Exam Date] = '" & TxtExamdate.Text & "',[Exam Time] = '" & TxtExamtime.Text & "',[Street Name] = '" & TxtStreet.Text & "',[House Nr] = '" & TxtHouse.Text & "',[please] = '" & TxtPlz.Text & "',[CITY] = '" & TxtCity.Text & "' where ID = '" & TxtId.Text & "' " .ExecuteNonQuery() End With FillDataGridView("select * from [edit$]") MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text) Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information, Text) Return End Try End Sub Private Sub BtnClose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnClose.Click Close() End Sub Private Sub BtnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnClear.Click TxtId.Clear() TxtFamilyname.Clear() TxtGivenname.Clear() TxtStreet.Clear() TxtHouse.Clear() TxtPlz.Clear() TxtCity.Clear() 'To see all the data in DataGridView FillDataGridView("select * from[edit$]") End SubEnd Class 解决方案 ") End Sub Private Sub FillDataGridView(ByVal Query As String) da = New OleDbDataAdapter(Query, cn) dt.Clear() da.Fill(dt) With DataGridView1 .DataSource = dt .Columns(0).HeaderText = "ID" .Columns(1).HeaderText = "Family Name" .Columns(2).HeaderText = "Given Name" .Columns(3).HeaderText = "Gender" .Columns(4).HeaderText = "DOB" .Columns(5).HeaderText = "Exam Date" .Columns(6).HeaderText = "Exam Time" .Columns(7).HeaderText = "Street Name" .Columns(8).HeaderText = "House Nr" .Columns(9).HeaderText = "please" .Columns(10).HeaderText = "City" .Columns(10).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill End With End Sub Private Sub BtnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnSearch.Click Try FillDataGridView("select * from [editwhere ID='" & TxtId.Text & "'") TxtFamilyname.Text = dt.Rows(0).Item(1) TxtGivenname.Text = dt.Rows(0).Item(2) TxtGender.Text = dt.Rows(0).Item(3) TxtDob.Text = dt.Rows(0).Item(4) TxtExamdate.Text = dt.Rows(0).Item(5) TxtExamtime.Text = dt.Rows(0).Item(6) TxtStreet.Text = dt.Rows(0).Item(7) TxtHouse.Text = dt.Rows(0).Item(8) TxtPlz.Text = dt.Rows(0).Item(9) TxtCity.Text = dt.Rows(0).Item(10) Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical, Text) End Try End Sub Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnSave.Click If vld(TxtId, TxtFamilyname, TxtGivenname, TxtGender, TxtDob, TxtExamdate, TxtExamtime, TxtStreet, TxtHouse, TxtPlz, TxtCity) = False Then Exit Sub Else End If Try With cm .Connection = cn .CommandText = "insert into [editvalues('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity.Text & "' )" .ExecuteNonQuery() End With FillDataGridView("select * from [edit 这篇关于如何验证重复的条目VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 04:30