VB.net和mysql返回多行

时间:2017-02-26 19:41:47

标签: mysql vb.net

如何将从mysql数据库返回的多行从一个函数返回到另一个函数?

查询mysql的函数是:

 Public Function infoSucelje(ByVal username As String)
        Try
            ManageConnection(False) 'Open connection'

            Dim strQuery As String = "SELECT gui_value FROM kerametal.presets"
            Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
            Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()


            If reader.HasRows Then
                While reader.Read()
                    Return ?????
                End While
            End If

            reader.Close()
        Catch ex As MySqlException
            Console.WriteLine("Error: " & ex.ToString())
        Finally
            ManageConnection(True)
        End Try
        Return False
    End Function

第二个函数是应该接收从第一个返回的所有字符串的函数,如何使循环检查返回的字符串然后根据返回的字符串执行某些操作。如果函数为每个字符串返回字符串buttonCallbuttonDisable,我想运行代码w.buttonCall.Visibility = Visibility.Visiblew.buttonDisable.Visibility = Visibility.Visible

 Public Function provjeriDozvoleSucelja(ByVal username As String)
      //for each ?? mysql.infoSucelje(username)
    End Function

2 个答案:

答案 0 :(得分:0)

您可以简单地将所有值添加到List(String)中,并使您的函数返回该列表

Public Function infoSucelje(ByVal username As String) As List(Of String)
    Dim result = new List(Of String)()
    Try
        ManageConnection(False) 
        Dim strQuery As String = "SELECT gui_value FROM kerametal.presets"
        Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
        Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()
        While reader.Read()
            result.Add(reader("gui_value").ToString())
        End While
        reader.Close()
        return result
    Catch ex As MySqlException
        Console.WriteLine("Error: " & ex.ToString())
        return Nothing
    Finally
        ManageConnection(True)
    End Try
End Function

并用

调用它
Public Function provjeriDozvoleSucelja(ByVal username As String)
     Dim output = mysql.infoSucelje(username)
     For each s in output
         Console.WriteLine(s)
     Next
End Function

我假设您的 gui_value 是字符串类型的字段。如果不是,您需要将List(Of String)更改为适当的数据类型,并且您可能需要检查读取器中存在的值....

答案 1 :(得分:0)

不知道它是否会有所帮助,但我会像这样查询......

byte x = 0;
Do While reader.Read()
//handle row items
x = x + 1
Loop
reader.Close()

reader.read()对象的每次传递都将使用下一行值重新填充reader数组。根据您编写SQL查询的方式,您只需直接引用每个列对象。如果你正在做“从表格中选择x,y,z”,读者(0)将是x列,读者(1)将是y列,读者(2)将是z列,所有第一行在第一遍...第二遍的第二行,依此类推。