先加裝 MySQL Connector .Net
在表單上佈置 1 個 DataGridView、1 個 TextBox、1 個 Button
程式 Form1.vb 如下:
<pre class="c" name="code">Imports MySql.Data.MySqlClient
Public Class Form1
Dim connectionString As String = "資料庫連線 DSN"
Dim sql As String = "SELECT * FROM *"
Dim connection As New MySqlConnection(connectionString)
Dim dataadapter As New MySqlDataAdapter(Sql, connection)
Dim ds As New DataSet()
Private Sub Fetch_Data(Filter as System.String)
Dim dv As DataView
dv = New DataView(ds.Tables(0), Filter, "", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connection.Open()
dataadapter.Fill(ds, "Titles_table")
connection.Close()
Fetch_Data("")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Fetch_Data(TextBox1.Text)
End Sub
End Class
只要在 TextBox 打上過濾條件 (也就是 SQL 指令的 where 部份),點 button 就會執行查詢。
You can extend the DataGridView control in a number of ways to build custom behaviors into your applications. http://vb.net-informations.com/datagridview/vb.net_datagridview_tutorial.htm giving you a detailed insight into datagridview.
thanks
algor