vb.net 用 SQL 中的数据填充 DataGridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7058141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:39:07  来源:igfitidea点击:

Fill DataGridView with data from SQL

vb.netoledb

提问by Sourav

I want to fill a DataGridView with data returned from a SQL. So here is my code [I provided cause some people may think I'm asking for help before trying myself]

我想用从 SQL 返回的数据填充 DataGridView。所以这是我的代码[我提供的原因是有些人可能认为我在尝试自己之前寻求帮助]

I want the DataGridView to be filled by a data from SQL not to show all the records.

我希望 DataGridView 由 SQL 中的数据填充,而不是显示所有记录。

The SQL "Select * From books where title='php%' Order By Title;"

SQL "Select * From books where title='php%' Order By Title;"

useless code :( :'( :<

无用的代码 :( :'( :<

Imports System.Data
Imports System.Data.SqlClient
Public Class frmMain
 Dim connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.mdb;Persist" & " Security Info=True"

 Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Me.BooksTableAdapter.Fill(Me.TblBooks.books)
 End Sub

  Private Sub txtTerm_TextChanged() Handles txtTerm.TextChanged
   If Trim(txtTerm.Text) = "" Then Exit Sub

   Dim tblCustomBooks As New DataTable
   Dim adpBooks As New OleDb.OleDbDataAdapter("Select * From books where title='php%' Order By Title", _
  '"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.mdb;Persist" & " Security Info=True")
   adpBooks.Fill(tblCustomBooks)
   BooksTableAdapter.Fill(tblCustomBooks)
  'Dim myConnection As SqlConnection
  'Dim myCommand As SqlDataAdapter
  'myConnection = New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Database.mdb;Persist" & " Security Info=True")
  'myCommand = New SqlDataAdapter("Select * From books where title='php%' Order By Title", myConnection)
  'Dim ds As DataSet = New DataSet()
  'myCommand.Fill(ds)
  'gridTable.DataSource = ds
 End Sub

回答by Tim

Looks like you've tried a number of different things, but it's not apparent from your code what order you tried them in. Based on the current version of your code, you're missing two things:

看起来您已经尝试了许多不同的东西,但是从您的代码中看不出您尝试它们的顺序。根据您的代码的当前版本,您缺少两件事:

First, an OleDBConnection object to use with the OleDbDataAdapter.

首先,要与 OleDbDataAdapter 一起使用的 OleDBConnection 对象。

Second, you're not assigning anything to the DataGridViews DataSource property, so nothing will show up.

其次,您没有为DataGridViews DataSource 属性分配任何内容,因此不会显示任何内容。

Also, you appear to be using two different OleDbDataAdapters (or maybe two different DataAdapters altogether) to fill tblCustomBooks, so depending on what BooksTableAdapteris set up as may also be causing you problems.

此外,您似乎正在使用两个不同的 OleDbDataAdapter(或者可能是两个不同的 DataAdapter)来填充tblCustomBooks,因此取决于BooksTableAdapter设置的内容,也可能会导致您出现问题。

Try this:

尝试这个:

Private Sub txtTerm_TextChanged() Handles txtTerm.Changed

    If Trim(txtTerm.Text) = "" Then Exit Sub

    Dim tblCustomBooks As New DataTable

    Using conn As New OleDbConnection(connectionString)
        Dim adpBooks As New OleDbDataAdapter("SELECT * FROM books WHERE title = 'php%' ORDER BY title", conn)
        adpBooks.Fill(tblCustomBooks)

        gridTable.DataSource = tblCustomBooks
    End Using
End Sub

See:

看:

DataGridView.DataSource Property

DataGridView.DataSource 属性

OleDbDataAdapter Class

OleDbDataAdapter 类

回答by InteXX

In your SQL statement try [WHERE Title LIKE 'php%'] instead of [WHERE Title = 'php%'].

在您的 SQL 语句中尝试 [WHERE Title LIKE 'php%'] 而不是 [WHERE Title = 'php%']。

I've run into similar problems with MS SQL and this was the fix. I'm not sure if the SQL syntax is the same for the Jet provider, but it's worth a try at least.

我遇到了与 MS SQL 类似的问题,这就是修复。我不确定 Jet 提供程序的 SQL 语法是否相同,但至少值得一试。

HTH

HTH

回答by Juman

dim dt as new datatable
'i already maked the class and now load from a database
dt=cls.getdata("select * from tblinf")
datagridview1.datasource=dt