使用VB.NET从MySql表读取值

时间:2012-10-31 22:56:54

标签: vb.net

我在尝试使用VB.NET读取MySQL数据库时遇到问题,我收到的错误消息是“ArgumentExcpetion未处理”

此外,读数应返回'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

3 个答案:

答案 0 :(得分:1)

字段索引基于零。

TextBox4.Text = (reader.GetChar(0))

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getchar.aspx

参数i:“从零开始的列序数。”

答案 1 :(得分:0)

查询中只有一列,列的索引应为零

TextBox4.Text = (reader.GetChar(0))   

This is for SqlServer但是MySqlDataReader的规则是相同的

答案 2 :(得分:0)

TextBox4.Text =(reader.GetChar(“获取值的列名称”)) 一切顺利