简单的 VB 语法显示数据库中的一些值

发布于 2024-11-09 02:22:13 字数 1917 浏览 0 评论 0原文

我对 Visual Basic 非常陌生(使用 Visual Studio 2010)。我只是做一些测试来连接到 mysql 数据库。

一旦我进行了 sql 查询,我不知道如何调用这些值。

我该如何处理这个问题,即在表单上的标签上显示值?

代码:

Imports MySql.Data.MySqlClient
Public Class Form1

    Dim ServerString As String = "Server = localhost; User Id = root; database = CALIBRA"
    Dim SQLConnection As MySqlConnection = New MySqlConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SQLConnection.ConnectionString = ServerString

        Try
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                MsgBox("Successfully connected to MySQL database.")
            Else
                SQLConnection.Close()
                MsgBox("Connection is closed.")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub calibra_query(ByRef SQLStatement As String)
        Dim cmd As MySqlCommand = New MySqlCommand

        With cmd
            .CommandText = SQLStatement
            .CommandType = CommandType.Text
            .Connection = SQLConnection
            .ExecuteNonQuery()
        End With

        SQLConnection.Close()
        MsgBox("Records Successfully Retrieved")
        SQLConnection.Dispose()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim SQLStatement As String = "SELECT Auto1, Auto2, TotalWeight FROM txticket WHERE TicketCode = '12210'"
        calibra_query(SQLStatement)

        Dim Automobile1, Automobile2, TotalWgt As Long

    SOMETHING MISSING HERE
    SOMETHING MISSING HERE

    Label2.Text = Automobile1.ToString()
    Label2.Text = Automobile2.ToString()
    Label2.Text = TotalWgt.ToString()

    End Sub
End Class

我应该在“这里缺少什么”中输入什么?非常感谢。

Im very new to Visual Basic (using visual studio 2010). Im just doing some tests to connect to a mysql database.

I do not know how to call these values once I have made the sql query.

How do I go about this, i.e. to show the values on the labels on a form?

Code:

Imports MySql.Data.MySqlClient
Public Class Form1

    Dim ServerString As String = "Server = localhost; User Id = root; database = CALIBRA"
    Dim SQLConnection As MySqlConnection = New MySqlConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SQLConnection.ConnectionString = ServerString

        Try
            If SQLConnection.State = ConnectionState.Closed Then
                SQLConnection.Open()
                MsgBox("Successfully connected to MySQL database.")
            Else
                SQLConnection.Close()
                MsgBox("Connection is closed.")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub calibra_query(ByRef SQLStatement As String)
        Dim cmd As MySqlCommand = New MySqlCommand

        With cmd
            .CommandText = SQLStatement
            .CommandType = CommandType.Text
            .Connection = SQLConnection
            .ExecuteNonQuery()
        End With

        SQLConnection.Close()
        MsgBox("Records Successfully Retrieved")
        SQLConnection.Dispose()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim SQLStatement As String = "SELECT Auto1, Auto2, TotalWeight FROM txticket WHERE TicketCode = '12210'"
        calibra_query(SQLStatement)

        Dim Automobile1, Automobile2, TotalWgt As Long

    SOMETHING MISSING HERE
    SOMETHING MISSING HERE

    Label2.Text = Automobile1.ToString()
    Label2.Text = Automobile2.ToString()
    Label2.Text = TotalWgt.ToString()

    End Sub
End Class

What do I put in the "SOMETHING MISSING HERE"? Much appreciation.

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

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

发布评论

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

评论(1

九公里浅绿 2024-11-16 02:22:13

您将需要一个数据读取器来读取 sql 查询返回的内容。您的 Sub calibra_query 正在执行非读取器,它不会执行您需要的操作。您只想将executeNonReader 用于不需要结果的事情。 (例如,像 Update 语句)

您想要更像这样的东西:

 Dim cmd As MySqlCommand = New MySqlCommand

With cmd
    .CommandText = SQLStatement
    .CommandType = CommandType.Text
    .Connection = SQLConnection
End With

  Dim myReader as MySqlDataReader = myCommand.ExecuteReader

  If myReader.Read Then
     TextBox1.Text = myReader.GetString(0)
     TextBox2.Text = myReader.Getstring(1)
     TextBox3.Text = myReader.GetInt32(2)
  End If

  myReader.Close()
  SQLConnection.Close()
  MsgBox("Records Successfully Retrieved")
  SQLConnection.Dispose()

我假设查询中 3 个字段的类型,然后将其输出到文本框以便为您提供想法。这还假设您最终只会得到一条记录。

(编辑:修复格式)

You will need a data reader in order to read the content of what is returned from the sql query. Your Sub calibra_query, is executing a nonreader, that isn't going to do what you need. You only want to use executeNonReader for things that you don't need a result from. (Like an Update statement for example)

You want something a bit more like this:

 Dim cmd As MySqlCommand = New MySqlCommand

With cmd
    .CommandText = SQLStatement
    .CommandType = CommandType.Text
    .Connection = SQLConnection
End With

  Dim myReader as MySqlDataReader = myCommand.ExecuteReader

  If myReader.Read Then
     TextBox1.Text = myReader.GetString(0)
     TextBox2.Text = myReader.Getstring(1)
     TextBox3.Text = myReader.GetInt32(2)
  End If

  myReader.Close()
  SQLConnection.Close()
  MsgBox("Records Successfully Retrieved")
  SQLConnection.Dispose()

I am assuming the types of the 3 fields in your query, and just outputting it to text boxes to give you the idea. That also assumes that you are only going to get one record as a result.

(Edit: to fix formatting)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文