vb读取数据库数据
⑴ VB读取数据库方法
可以用ADODB,先定义
Dim cnn
As
New
ADODB.Connection
Dim rs
As
New
ADODB.Recordset
这两个cnn用来连接数据库rs用来读取数据库中的值,然后用循环遍历rs将值ADD到listview里面
修改和保存可以做成同一个按钮保存就是遍历listview将数据UPDATE到数据库里面就行!
⑵ VB读取ACCESS数据库
Set
dbs
=
OpenDatabase(App.Path
+
"\APP\TL.mdb")
参数不对:workspace.OpenDatabase(数据库名,打开模式,是否只读,连接字符串)
Set
dbs=Workspaces(0).OpenDatabase(App.Path+"\APP\TL.mdb",False,False,"MS
Access;PWD=***")
'下面引用ADO2.1
Private
Sub
Command1_Click()
On
Error
GoTo
aa:
Dim
dbs
As
New
Connection
'定义为数据库类型
Dim
rst
As
Recordset
'定义为记录类型
Dim
theday
As
Date
'定义为日期类型
Dim
connstr
As
String
Dim
i
As
Integer
Dim
Bcell,
Ecell,
SQLString
As
String
Set
ExcelReport
=
New
Excel.Application
ExcelReport.Workbooks.Open
FileName:=App.Path
+
"\APP\脱硫系统运行日志.xls"
ExcelReport.DisplayAlerts
=
False
Set
Sheet1
=
ExcelReport.Sheets("Sheet1")
Set
Sheet2
=
ExcelReport.Sheets("Sheet2")
Set
Sheet3
=
ExcelReport.Sheets("Sheet3")
Set
Sheet4
=
ExcelReport.Sheets("Sheet4")
Sheet1.Activate
theday
=
Day(CDate(DTPicker1.Value))
connstr
=
"DBQ="
+
App.Path
+
"\APP\TL.mdb"
+
";DefaultDir=;DRIVER={Microsoft
Access
Driver
(*.mdb)};"
Set
dbs
=
New
Connection
dbs.Open
connstr
'dbs.Execute
"delete
from
day1
"
SQLString
=
"select
*
from
TL1
where
DT='"
&
CStr(DTPicker1.Value)
&
"'"
dbs.Execute
SQLString
Set
rst
=
dbs.Execute("select
*
from
TL1")
If
rst.EOF
=
False
Then
rst.MoveFirst
End
If
ExcelReport.Visible
=
True
i
=
0
While
rst.EOF
=
False
i
=
i
+
1
Sheet1.Cells(i
+
7,
2)
=
rst!GLFH
Sheet1.Cells(i
+
7,
3)
=
rst!PH
Sheet1.Cells(i
+
7,
4)
=
rst!TFTW
Sheet1.Cells(i
+
7,
5)
=
rst!TFMD
Sheet1.Cells(i
+
7,
6)
=
rst!JT1
Sheet1.Cells(i
+
7,
7)
=
rst!CT1
Sheet1.Cells(i
+
7,
8)
=
rst!JP1
Sheet1.Cells(i
+
7,
9)
=
rst!CP1
Sheet1.Cells(i
+
7,
10)
=
rst!CWSP
Sheet1.Cells(i
+
7,
11)
=
rst!CWXP
Sheet1.Cells(i
+
7,
12)
=
rst!XAI
Sheet1.Cells(i
+
7,
13)
=
rst!XBI
Sheet1.Cells(i
+
7,
14)
=
rst!XCI
Sheet1.Cells(i
+
7,
15)
=
rst!MAI
Sheet1.Cells(i
+
7,
16)
=
rst!MBI
Sheet1.Cells(i
+
7,
17)
=
rst!YAI
Sheet1.Cells(i
+
7,
18)
=
rst!YAP
Sheet1.Cells(i
+
7,
19)
=
rst!YBI
Sheet1.Cells(i
+
7,
20)
=
rst!YBP
Sheet1.Cells(i
+
7,
21)
=
rst!SHAP
Sheet1.Cells(i
+
7,
22)
=
rst!SHBP
Sheet1.Cells(i
+
7,
23)
=
rst!SH_4MIDU
Sheet1.Cells(i
+
7,
24)
=
rst!SGAI
Sheet1.Cells(i
+
7,
25)
=
rst!SGBI
Sheet1.Cells(i
+
7,
26)
=
rst!MFT
Sheet1.Cells(i
+
7,
27)
=
rst!MFP
rst.MoveNext
Wend
ExcelReport.Visible
=
True
GoTo
cc
aa:
ExcelReport.DisplayAlerts
=
False
dbs.Close
Set
dbs
=
Nothing
Unload
Me
cc:
End
Sub
⑶ VB如何从数据库读取指定数据
你加载两个TEXTBOX,一个ADODC,连接上数据库后,用条件法搜索指定的内容。
Adodc1.RecordSource
=
"select
*
from
表
where
"
&
Text1.Text
&
"=
'"
&
Text2.Text
&
"'"
Adodc1.Refresh
主要是设定条件值