gpt4 book ai didi

mysql - 如何在 vb.net 中检索 mysql 数据?

转载 作者:行者123 更新时间:2023-11-29 01:41:29 26 4
gpt4 key购买 nike

我正在尝试检索具有特定列的 mysql 数据并显示到 vb.net 中的文本框。我应该如何取回它?

Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
connect.Open()

Dim sqladapter As New MySqlDataAdapter
Dim sqlcmd As New MySqlCommand
Dim dr As MySqlDataReader
Dim dt As New DataTable

sqlcmd = New MySqlCommand("SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'")
**THEN? WHAT SHOULD I DO TO DISPLAY DATA? PLEASE HELP**

connect.Close()

最佳答案

您只是缺少 Execution 方法。这取决于你想要什么样的结果。如果您只想要查询的第一个结果(第一行和第一列),则使用 sqlcmd.ExecuteScalar()

如果你想要所有的结果,你必须使用 sqlcmd.ExecuteReader() 方法将其加载到 MySqlDataReader

使用 ExecuteReader() :

Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
connect.Open()

Dim sqladapter As New MySqlDataAdapter
Dim sqlcmd As New MySqlCommand
Dim dr As MySqlDataReader
Dim dt As New DataTable

sqlcmd = New MySqlCommand("SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'")
dr = sqlcmd.ExecuteReader()
dt.Load(dr)
'Useable datatable in dt variable...

connect.Close()

使用 ExecuteScalar() :

Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
connect.Open()

Dim sqladapter As New MySqlDataAdapter
Dim sqlcmd As New MySqlCommand
Dim dr As String
Dim dt As New DataTable

sqlcmd = New MySqlCommand("SELECT [COLUMN NAME] complaint WHERE tran_no='" & lbltranno.Text & "'")
dr = sqlcmd.ExecuteScalar()
'dr now contains the value of [COLUMN NAME] for the first returned row.

connect.Close()

关于mysql - 如何在 vb.net 中检索 mysql 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com