<i id='mpeWd'><tr id='mpeWd'><dt id='mpeWd'><q id='mpeWd'><span id='mpeWd'><b id='mpeWd'><form id='mpeWd'><ins id='mpeWd'></ins><ul id='mpeWd'></ul><sub id='mpeWd'></sub></form><legend id='mpeWd'></legend><bdo id='mpeWd'><pre id='mpeWd'><center id='mpeWd'></center></pre></bdo></b><th id='mpeWd'></th></span></q></dt></tr></i><div id='mpeWd'><tfoot id='mpeWd'></tfoot><dl id='mpeWd'><fieldset id='mpeWd'></fieldset></dl></div>

      1. <small id='mpeWd'></small><noframes id='mpeWd'>

      2. <tfoot id='mpeWd'></tfoot>

        <legend id='mpeWd'><style id='mpeWd'><dir id='mpeWd'><q id='mpeWd'></q></dir></style></legend>
          <bdo id='mpeWd'></bdo><ul id='mpeWd'></ul>
      3. 防止重复进入数据库

        时间:2023-09-17

          <tbody id='pedDr'></tbody>
        <tfoot id='pedDr'></tfoot>
        • <small id='pedDr'></small><noframes id='pedDr'>

          <i id='pedDr'><tr id='pedDr'><dt id='pedDr'><q id='pedDr'><span id='pedDr'><b id='pedDr'><form id='pedDr'><ins id='pedDr'></ins><ul id='pedDr'></ul><sub id='pedDr'></sub></form><legend id='pedDr'></legend><bdo id='pedDr'><pre id='pedDr'><center id='pedDr'></center></pre></bdo></b><th id='pedDr'></th></span></q></dt></tr></i><div id='pedDr'><tfoot id='pedDr'></tfoot><dl id='pedDr'><fieldset id='pedDr'></fieldset></dl></div>
              <bdo id='pedDr'></bdo><ul id='pedDr'></ul>

              <legend id='pedDr'><style id='pedDr'><dir id='pedDr'><q id='pedDr'></q></dir></style></legend>

                • 本文介绍了防止重复进入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想防止使用 vb.net 和 MySQL 作为数据库在我的库存表单中重复输入,这是我的代码:

                  I want to prevent duplicate entries to my inventory form using vb.net and MySQL as the database, here is my code:

                   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
                      Dim myCommand As New MySqlCommand
                      Dim conn As MySqlConnection
                      Dim i As String
                      conn = New MySqlConnection
                      conn.ConnectionString = "server = localhost;username= root;password= a;database= secret"
                      Try
                          conn.Open()
                      Catch mali As MySqlException
                          MsgBox("connot establish connection")
                      End Try
                  
                      Dim intReturn As Integer
                      Dim strSql As String = " select * from personnel where pcode = @pcode"
                  
                      Dim sqlcmd As New MySqlCommand(strSql, conn)
                      With sqlcmd.Parameters
                          .AddWithValue("@pcode", CType(pcode.Text, String))
                      End With
                  
                      intReturn = sqlcmd.ExecuteScalar
                  
                      If (intReturn > 0) Then
                          cmd = New MySqlCommand("Insert into personnel values('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.Text & "','" & designation.Text & "')")
                          i = cmd.ExecuteNonQuery
                  
                  
                          If pcode.Text <> "" Then
                          ElseIf i > 0 Then
                              MsgBox("Save Successfully!", MessageBoxIcon.Information, "Success")
                              mrClean()
                              ListView1.Tag = ""
                              Call objLocker(False)
                              Call LVWloader()
                              Call calldaw()
                          Else
                              MsgBox("Save Failed!", MessageBoxIcon.Error, "Error!")
                          End If
                      Else
                          MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")
                  
                      End If
                  

                  结束子

                  我在搜索答案时发现了这个,但是当我尝试运行它时,它不会读取插入命令,而是直接进入 msbox人员 ID 已经存在",即使没有相同的人员 ID.

                  i found this while i search for answer, but when i tried to run it, it does not read the insert command but rather it goes directly to the msbox "Personnel ID Already Exist" even if theres no thesame Personnel ID.

                  有人可以检查为什么它不读取插入内容,

                  can someone check why it does not read the insert please,

                  我的数据库表值:

                  pcode = 主键

                  lname = 长文本

                  lname = longtext

                  fname = 长文本

                  fname = longtext

                  办公室 = 长文本

                  指定 = 长文本

                  非常感谢任何帮助,谢谢,

                  any help will be much appreciated, thanks,

                  推荐答案

                  很抱歉,这是错误的方法.

                  Sorry to say this is the wrong approach.

                  数据库有一个内置系统来防止数据被复制.那是通过主键或唯一键约束.在您的情况下,您已经创建了一个主键.因此,您绝对不需要执行该 SELECT COUNT(*) 查询.

                  Databases have a built in system to prevent data being duplicated. That's through primary keys or unique key constraints. In your case, you have already created a primary key. So there is absolutely no need for you to do that SELECT COUNT(*) query.

                  相反,当pcode已经存在时,直接插入表并捕获完整性错误.

                  Instead, just directly insert into the table and catch the integrity error when the pcode already exists.

                  Try
                      cmd = New MySqlCommand("Insert into personnel values('" & pcode.Text & "','" & lname.Text & "','" & fname.Text & "','" & office.Text & "','" & designation.Text & "')")
                  
                      i = cmd.ExecuteNonQuery
                  
                  
                      If pcode.Text <> "" Then
                      ElseIf i > 0 Then
                          MsgBox("Save Successfully!", MessageBoxIcon.Information, "Success")
                          mrClean()
                          ListView1.Tag = ""
                          Call objLocker(False)
                          Call LVWloader()
                          Call calldaw()
                      Else
                          MsgBox("Save Failed!", MessageBoxIcon.Error, "Error!")
                      End If
                  Catch ex As MySqlException
                      MsgBox("Personnel ID Already Exist!", MessageBoxIcon.Error, "Error!")
                  End Try
                  

                  另请参阅 MySQL 手册页 PRIMARYKEY 和 UNIQUE 索引约束

                  Please also refer to the MySQL Manual Page PRIMARY KEY and UNIQUE Index Constraints

                  这篇关于防止重复进入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何检查 DataReader 值是否为空? 下一篇:如何使用 vb.net 和 adodb 连接在 mysql 数据库中插入图像

                  相关文章

                • <i id='1My0i'><tr id='1My0i'><dt id='1My0i'><q id='1My0i'><span id='1My0i'><b id='1My0i'><form id='1My0i'><ins id='1My0i'></ins><ul id='1My0i'></ul><sub id='1My0i'></sub></form><legend id='1My0i'></legend><bdo id='1My0i'><pre id='1My0i'><center id='1My0i'></center></pre></bdo></b><th id='1My0i'></th></span></q></dt></tr></i><div id='1My0i'><tfoot id='1My0i'></tfoot><dl id='1My0i'><fieldset id='1My0i'></fieldset></dl></div>

                • <legend id='1My0i'><style id='1My0i'><dir id='1My0i'><q id='1My0i'></q></dir></style></legend>

                    <small id='1My0i'></small><noframes id='1My0i'>

                    • <bdo id='1My0i'></bdo><ul id='1My0i'></ul>

                    <tfoot id='1My0i'></tfoot>