如何用vb.net选择mysql中的所有列值

tyg4sfes  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(386)

我有两个列的表:“total”和“returned”,我试图从这两个列中获取所有的列值,但它不适合我。它只取列中的第一行,我要列中的总值
我的代码:

Private Sub TotalTextBox_TextChanged(sender As Object, e As EventArgs) Handles TotalTextBox.TextChanged
    Dim str As String = "Server=localhost;Port=3306;Database=testdb;Uid=root;Pwd=password"

    Using net As New MySqlConnection(str)

        Dim totalnet As String = "Select * from testata where Qty_Returned and Total" 

        Dim cm As New MySqlCommand(totalnet, net)

        net.Open()
        Dim rdnet As MySqlDataReader = cm.ExecuteReader()

        If rdnet.Read() Then
            Label16.Text = rdnet.GetString(12) - rdnet.GetString(11)
        Else

        End If
    End Using

End Sub
oug3syen

oug3syen1#

很高兴你成功了。只是一些建议。。。为此代码和所有代码启用选项strict。注解是内联的。

Private Sub testdataDataGridView_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles testataDataGridView.DataBindingComplete
        'A Using...End Using block will ensure that your objects that use unmanages 
        'resources are closed and disposed event if there is an error in the code
        Using con As New MySqlConnection("Server=localhost;Port=3306;Database=testdb;   Uid=root;Pwd=1234")
            'A command object constructor can take an query string and connection as arguments
            Using cmd As New MySqlCommand("select sum(Qty_Returned) FROM testdata", con)
                'Open the connection at the last possible minute
                con.Open()
                Dim sqlresult = cmd.ExecuteScalar
                Label16.Text = sqlresult.ToString
            End Using
            Using cmg As New MySqlCommand("select sum(Total) FROM testgdata", con)
                Dim sqlresult2 = cmg.ExecuteScalar
                Label10.Text = sqlresult2.ToString
            End Using
        End Using
        Label18.Text = (CInt(La
End Sub

不确定此事件中出现此代码的原因。

mnemlml8

mnemlml82#

我用这个代码得到了解决方案:

Private Sub testdataDataGridView_DataBindingComplete(sender As Object, e As     DataGridViewBindingCompleteEventArgs) Handles testataDataGridView.DataBindingComplete
    Dim con As New MySqlConnection("Server=localhost;Port=3306;Database=testdb;   Uid=root;Pwd=1234")

    Dim cmd As New MySqlCommand
    Dim cmg As New MySqlCommand

    con.Open()
    cmd.Connection = con
    cmd.CommandText = "select sum(Qty_Returned) FROM testdata"
    Dim sqlresult As Object
    sqlresult = cmd.ExecuteScalar
    Dim str1 As String
    str1 = sqlresult
    Label16.Text = str1
    cmg.Connection = con
    cmg.CommandText = "select sum(Total) FROM testgdata"
    Dim sqlresult2 As Object
    sqlresult2 = cmg.ExecuteScalar
    Dim str2 As String
    str2 = sqlresult2
    Label10.Text = str2

    Label18.Text = Label10.Text - Label16.Text
End Sub

相关问题