ExecuteScalar()返回的值超过1

时间:2017-03-20 15:11:49

标签: visual-studio

我想要返回我的查询=" show database" 然后这是我的代码

dim conn as conn.connectionstring = Myconnection
dim Mydb as new Mysql.data.mysqlclient.mysqlcommand(myquery,conn)
conn.open
textbox2.text = convert.tostring(mydb.excutescalar())

我的输出是:

  • infomationschema
  • MySQL的
  • 测试

1 个答案:

答案 0 :(得分:0)

现在我找到了一种方法

以下代码

Dim myQuery As String = "show databases"
TextBox1.Text = "localhost"
    constring = "server=" & TextBox1.Text & ";" _
    & "uid=root;" _
    & "pwd=admin;" _
    & "database=mysql;"
    Try
        conn.ConnectionString = constring
        Dim myDb As New MySql.Data.MySqlClient.MySqlCommand(myQuery, conn)
        conn.Open()
        ComboBox1.Text = "select database"
        'myDb.Connection.Open()
        'TextBox2.Text = TextBox2.Text + Convert.ToString(myDb.ExecuteScalar())
        Dim myReader As MySql.Data.MySqlClient.MySqlDataReader = myDb.ExecuteReader()
        Try
            While myReader.Read
                ComboBox1.Items.Add(myReader.GetString(0))
            End While
        Finally
            myReader.Close()
            conn.Close()
        End Try

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

然后我的输出 enter image description here