显示数据集vb.net中的数据

时间:2014-08-19 01:05:28

标签: sql-server wpf vb.net binding

我遇到从MS SQL数据库返回数据的问题。

代码返回'System.data.datarowview'而不是我的查询结果。子代码是:

    Public Sub newquery(query As String)
    Dim SQLConn As SqlConnection = New SqlConnection
    Dim SqlCommand As New SqlCommand

    SQLConn.ConnectionString = "Data Source=.\testing;Initial Catalog=eurostyle;Integrated Security=SSPI;"

    SqlCommand = New SqlCommand(query, SQLConn)

    Try
        SQLConn.Open()
        sqlDA = New SqlDataAdapter(SqlCommand)
        sqlDataset = New DataSet
        sqlDA.Fill(sqlDataset)
        SQLConn.Close()
        listbox1.DataContext = sqlDataset
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try


End Sub

我是WPF的新手,我确信这只是微不足道的事情。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

DataGrid视图更适合显示数据库的结果。

试试这个:

Public Sub newquery(query As String)
    Dim SQLConn As SqlConnection = New SqlConnection
    Dim SqlComm As New SqlCommand
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource
    Dim sqlDA As New SqlDataAdapter

    SQLConn.ConnectionString = "Data Source=.\testing;Initial Catalog=eurostyle;Integrated Security=SSPI;"  

Try
     SQLConn.Open()
     SqlComm = SQLConn.CreateCommand
     SqlComm.CommandText = query

     SqlComm = New SqlCommand(zapytanie, myConn)
     sqlDA.SelectCommand = SqlComm
     sqlDA.Fill(dbDataSet)
     bSource.DataSource = dbDataSet
     DataGridView.DataSource = bSource
     sqlDA.Update(dbDataSet)
     SQLConn.Close()

Catch ex As SqlException
     MessageBox.Show("Query Error: " & ex.Message)
End Try