我想要一个允许用户创建自己的查询以从数据库中检索数据的程序。实现此类任务的最佳方法是什么?任何链接或示例代码将不胜感激。谢谢。
答案 0 :(得分:0)
我会选择:
创建表单,将其命名为formSample
添加一个文本框,将其命名为txbSample
添加一个datagridview,将其命名为dgvSample
添加一个按钮,将其命名为btnSample
客户端将使用txbSample来输入查询 btnSample是你放置代码的地方。 dgvSample是数据,不是真正推荐的,但它只是用于显示您检索到的数据。
假设您已连接到数据库,btnSample上的代码应该看起来像
注意:不是完整的代码
Dim sql As String
sql = txbSample.text
Using cmd As New MySqlCommand
Try
'open the connection here
cmd.Connection = con
cmd.CommandText = sql
Dim dt As New DataTable
da = New MySqlDataAdapter(cmd)
da.Fill(dt)
'close the connection here
dgvSample.DataSource = dt
Catch ex As Exception
MessageBox.Show("Error while fetching data.")
'close the connection here
End Try
End Using
dt is a datatable where the data are stored. You should do something to be able to use it:)