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
主要是設定條件值