使用 Visual Basic 在文本框中显示来自 MySql 的自动编号

发布于 2024-12-08 06:03:14 字数 1788 浏览 0 评论 0原文

这是针对我正在创建的帮助台票务程序:

当用户单击开始票证时,我希望能够显示来自 mysql 的自动编号,该编号与当前填写的票证相对应,以便用户稍后可以跟踪他们的票证在。我已完成所有设置,但不太确定如何在文本框中显示票证 ID。

现在我已经有了它,以便在表单加载时运行查询以获取下一个票证 ID 号,但我不确定如何将其显示为文本框中的文本。

    'QUERY FOR NEW ID TICKET NUMBER   
Private Sub Suggestions_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim idQuery As String = "SELECT MAX(itID)+1 AS Expr1 FROM(TableName)"

    With cmd
        .Connection = conn
        .CommandText = idQuery
        .CommandType = CommandType.Text
    End With

    Try
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        Me.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
        conn.Close()
    End Try

现在我设法将其更改为这样,但我仍然挂断,因为我不确定它是否正确:

Dim conn As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand()
Dim Reader As MySqlDataReader
Dim submitteddate As String

'QUERY FOR NEW ID TICKET NUMBER   
Private Sub Suggestions_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Try
        Dim idQ As String = "INSERT INTO tblit (itID, ersBugIssue, ersSubject, ersBugCategory, ersBugDate, ersBugSubmittedby, ersBugOrHelp )" & _
                                "VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL);"
        Dim cmd As new SQLCommand()
        With cmd
            .Connection = conn
            .CommandText = idQ
        End With

        conn.Open()
        cmd.ExecuteNonQuery()
        cmd.CommandText = "SELECT LAST_INSERT_ID() as NewTicketID"
        Dim Reader As SqlDataReader = cmd.ExecuteReader()
        Reader.Read()
        MsgBox(Reader("NewTicketID"))
        Reader.Close()

        conn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub

This is for a helpdesk ticketing program I'm creating:

When the user clicks to start a ticket I want to be able to display an autonumber from mysql that corresponds with the current ticket being filled out, so that the user can track their ticket later on. I have everything set up but I'm not too sure how I can display the Ticket ID in a textbox.

Right now I have it so that the query is being ran to get the next Ticket Id number when the form loads, but I'm not sure how to display it as text in the textbox.

    'QUERY FOR NEW ID TICKET NUMBER   
Private Sub Suggestions_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim idQuery As String = "SELECT MAX(itID)+1 AS Expr1 FROM(TableName)"

    With cmd
        .Connection = conn
        .CommandText = idQuery
        .CommandType = CommandType.Text
    End With

    Try
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        Me.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
        conn.Close()
    End Try

Now I managed to change it to this but I'm still getting hung up cause I'm not sur eif it's correct:

Dim conn As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand()
Dim Reader As MySqlDataReader
Dim submitteddate As String

'QUERY FOR NEW ID TICKET NUMBER   
Private Sub Suggestions_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Try
        Dim idQ As String = "INSERT INTO tblit (itID, ersBugIssue, ersSubject, ersBugCategory, ersBugDate, ersBugSubmittedby, ersBugOrHelp )" & _
                                "VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL);"
        Dim cmd As new SQLCommand()
        With cmd
            .Connection = conn
            .CommandText = idQ
        End With

        conn.Open()
        cmd.ExecuteNonQuery()
        cmd.CommandText = "SELECT LAST_INSERT_ID() as NewTicketID"
        Dim Reader As SqlDataReader = cmd.ExecuteReader()
        Reader.Read()
        MsgBox(Reader("NewTicketID"))
        Reader.Close()

        conn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-12-15 06:03:14

“SELECT MAX(itID)+1 AS Expr1 FROM(TableName)”

这是一个坏主意,您很快就会遇到并发问题。
您应该运行以下 SQL 语句。

INSERT INTO tablename (ID, morefields) 
  VALUES (NULL, real_values_or_place_holders);
SELECT LAST_INSERT_ID() as NewTicketID;
-- After the ticket is complete
UPDATE tablename SET morefields = <thedata> WHERE id = NewTicketID 

-- If the ticket gets cancelled:
DELETE FROM tablename WHERE id = NewTicketID

如果您这样做,您将永远不会遇到重复或错误 TicketID 发出的问题。

如果您执行select,则不能说它是非查询。

使用以下代码

Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT LAST_INSERT_ID() AS NewTicketID"
Dim reader As SqlDataReader = cmd.ExecuteReader()
reader.Read
MsgBox(reader("NewTicketID"))

"SELECT MAX(itID)+1 AS Expr1 FROM(TableName)"

Is a bad idea, you will run into concurrency issues very quickly.
You should run the following SQL statements instead.

INSERT INTO tablename (ID, morefields) 
  VALUES (NULL, real_values_or_place_holders);
SELECT LAST_INSERT_ID() as NewTicketID;
-- After the ticket is complete
UPDATE tablename SET morefields = <thedata> WHERE id = NewTicketID 

-- If the ticket gets cancelled:
DELETE FROM tablename WHERE id = NewTicketID

If you do it like that you will never run into issues with duplicate or wrong TicketID being issued.

If you do a select, you cannot say it's a nonQuery.

Use the following code

Dim cmd As New SqlCommand()
cmd.Connection = conn
cmd.CommandText = "SELECT LAST_INSERT_ID() AS NewTicketID"
Dim reader As SqlDataReader = cmd.ExecuteReader()
reader.Read
MsgBox(reader("NewTicketID"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文