从mysql获取数据到dataGrid

时间:2017-03-01 08:29:48

标签: mysql wpf vb.net datagrid

如何在VB.net中向dataGrid视图添加数据(我正在使用WPF dataGrid)。 这是一个有效的代码,这个代码用于登录,我的应用程序可以访问数据库,我可以操作数据:

Public Function login(ByVal username As String, ByVal password As String)
        Try
            ManageConnection(False) 'Open connection'

            Dim strQuery As String = "SELECT * FROM `dbns`.`wrks` WHERE `username` = '" + username + "' and password = '" + password + "'"
            Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
            Dim reader As MySqlDataReader = SqlCmd.ExecuteReader()

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

我如何实现SELECT * FROM xxxxx WHERE xxxx并填充

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="376,167,0,0" VerticalAlignment="Top" Height="100" Width="645"/>

这是我打开和关闭连接的Sub:

 Public Sub ManageConnection(ByVal CloseConnection As Boolean)
        Try
            'Pripremanje konekcije i upita'
            dbCon = New MySqlConnection("server=127.0.0.1;user id=root;password=*****;database=****")
            If CloseConnection = False Then
                If dbCon.State = ConnectionState.Closed Then _
                    dbCon.Open()
            Else
                dbCon.Close()
            End If
        Catch ex As Exception
            MsgBox("Nije moguća konekcija na udaljenu bazu podataka, koristi drugu opciju.")
        End Try

    End Sub

我想知道我是否可以使用现有功能连接数据库以满足数据网格的特定需求。 例如:

Public Function testniQuery()
        Dim queryString As String =
  "SELECT * FROM table"
        Dim adapter As SqlDataAdapter = New SqlDataAdapter(
          queryString, connection)

        Dim customers As DataSet = New DataSet
        adapter.Fill(customers, "Customers")

    End Function

1 个答案:

答案 0 :(得分:0)

我不太了解Visual Basic,因为我使用C#但公共库运行时(CLR)使得更容易理解这两种语言。

当您连接到MySQL时,运行您将使用 DataAdapter从查询获取数据的查询。 查询结果可以保存在DataTable中,您可以轻松地将DataTable绑定到DataGrid。

因此,您运行一个查询,并且可以在代码中持久使用该表。

我这样做是C#。 请承担C#代码。这是一种可以做到的方法

YY

然后可以将返回的DataTable绑定到DataGrid,如下所示:

public DataTable Login(string username, string password){
//"SELECT * FROM `dbns`.`wrks` WHERE `username` = '" + username + "' and password = '" + password + "'"
    string query = "SELECT * FROM wrks WHERE username = @username AND password = @password";
    DataTable table = new DataTable();
    using (MySQlConnection connection = new MySQlConnection(connectionString))
    {
         using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, connection))
    {
            adapter.SelectCommand.Parameters.AddWithValue("@username", username);
            adapter.SelectCommand.Parameters.AddWithValue("@password", password);
            adapter.Fill(table);
            return table;
        }
    }
}