VB.NET怎么查询ACCESS数据并在datagirdview中显示

2024-12-16 07:02:21
推荐回答(2个)
回答1:

在致思路如下:
dim a as string= textbox1.text
dim b as string=textbox2.text
dim strsql as string="select * from table where 1=1"
if a<>"" then
strsql &=" and 编号='" & a & "'"

end if
if b<>"" then
strsql &=" and 姓名=‘" & b & "'"

end if
.....
如果你想用模糊查询就用Like,如果还有其它条件就在后面再加就好了!

回答2:

using System.Data.OleDb
//引用
在按钮事件中加入:
OleDbConnection cnn = new OleDbConnection()
//示例化连接对象
cnn.ConnectionString="Provider="Microsoft.Jet.OLEDB.4.0;DataSource=ACCESS路径"
Try
cnn.Open
Catch
MessageBox.Show("连接失败")
Finlly
cnn.Close
//打开连接,并关闭
DataSet d = new DataSet()
Dim sql as string;
sql="Select * From 表"
OleDbDataAdapter N = new OleDbDataAdapter(sql,cnn)
N.Fill(d,"表查询")
set DataGridView.DataSource=d.Tables["表查询"]
dataGridView.Binding()