使用 VB.NET 从 MySql 表中读取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13168630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading value from MySql table using VB.NET
提问by blacblu
I'm having trouble trying to read from a MySQL databse using VB.NET, the error message I get is "ArgumentExcpetion was unhandled"
我在尝试使用 VB.NET 从 MySQL 数据库读取时遇到问题,我收到的错误消息是“ArgumentExcpetion 未处理”
Also, the reading should return 'F', since this is the value allocated on that specific place on the table.
此外,读数应返回“F”,因为这是分配在桌子上特定位置的值。
Imports MySql.Data.MySqlClient
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim query As String = "SELECT DO FROM klein WHERE COMMAND='DELETE'"
Dim connStr As String = "server=" & TextBox1.Text & ";" _
& "user id=" & TextBox2.Text & ";" _
& "password=" & TextBox3.Text & ";" _
& "database=hidro201_liberato"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
connection.Open()
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader()
While reader.Read()
TextBox4.Text = (reader.GetChar(1)) '<- **problem is here**
End While
End Sub
End Class
采纳答案by Steve
You have only one column in your query and the index of the column should be zero
您的查询中只有一列,该列的索引应为零
TextBox4.Text = (reader.GetChar(0))
This is for SqlServerbut the rules are the same for a MySqlDataReader
这是用于 SqlServer但规则对于 MySqlDataReader 是相同的
回答by Guffa
The field index is zero based.
字段索引从零开始。
TextBox4.Text = (reader.GetChar(0))
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getchar.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getchar.aspx
Parameter i: "The zero-based column ordinal."
参数 i:“从零开始的列序号。”
回答by John Gacau
TextBox4.Text = (reader.GetChar("Name of column to get the value")) All the best
TextBox4.Text = (reader.GetChar("获取值的列名")) 一切顺利

