vb資料庫查詢
❶ 在VB怎麼查詢資料庫中的數據啊!
1.一個簡單的查詢的例子:
'*定義一個連接
Dim Conn As ADODB.Connection
'*定義一個記錄集
Dim mrc As ADODB.Recordset
'*分別實例化
Set Conn = New ADODB.Connection
set mrc =New ADODB.Recordset
'*定義一個連接字元串
dim ConnectString as string
ConnectString="provider=microsoft.jet.oledb.4.0;data source=" & App.Path & "\data\資料庫名.mdb;jet oledb:database password=資料庫密碼"
'*打開連接
Conn1.Open ConnectString
'*定義游標位置
Conn1.CursorLocation = adUseClient
'*查詢記錄集(從student表中找出名子為"張三"的記錄)
mrc.open "select * from student where name='張三'",Conn, adOpenKeyset, adLockOptimistic
'*現在你已經得到了你想要查詢的記錄集了,那就是mrc
'*你可以把此記錄集與DataGrid榜定,用datagrid顯示你查詢的記錄
set me.datagrid.datasource=mrc
❷ VB連接資料庫查詢
VB連接資料庫查詢過程基本辯叢是這樣子:
須引用ADO.
private sub 查詢_click()
Dim Cnn1 As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Dim cnnstr As String, s As Boolean
CnnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\mp.mdb;Persist Security Info=False"
Cnn1.Open CnnStr
s = "select * from 表1 where 下載編號='" & Text1.Text & "'"攜譽櫻
Rs.Open s, Cnn1, adOpenStatic, adLockReadOnly
If Rs.RecordCount > 0 Then
Text2.text = rs!歌手 & rs!歌名虛信 & rs!文件地址
else
Text2.text = "沒找到!"
End If
Rs.Close
Cnn1.Close
Set Rs = Nothing
Set Cnn1 = Nothing
end sub
❸ VB中查詢資料庫內容方法
多條件混合模糊搜索
"select * from 表名 where 欄位名 Like'%" & text1.text & "%'察虧and 欄位名 like'%" & combo1.text & "%' and 欄位名 like'%" & text2.text & "%'"
例子
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim cnstr As String
Private Sub Form_Load() '窗口鍵中打開時,連接資料庫
conn.CursorLocation = adUseClient
cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= db1.mdb;Jet OLEDB:Database Password=" '修改成你的數據為地址/密碼
conn.ConnectionString = cnstr
conn.Open cnstr
End Sub
Private Sub Command1_Click()
if rs.state=adstateopen then rs.close'稿沒山記錄集打開時則關閉記錄集
strsql ="select * from 表名 where 欄位名 Like'%" & text1.text & "%'and 欄位名 like'%" & combo1.text & "%' and 欄位名 like'%" & text2.text & "%'"
rs.Open strsql, conn, 3, 3
set DataGrid1.DataSource =rs
'這時適當調整一下datagird控制項的格式(略)
End Sub
Private sub form_unload()
conn.close
end sub
❹ VB資料庫查詢
雖然不推薦,你可以試正讓下放到公共變數里傳舉族局穗盯遞
全局變數當然可以保留值
模塊
Public
TestRS
As
ADODB.Recordset
窗體1
Set
TestRS
=
rs
窗體2
MsgBox
TestRS.RecordCount
❺ vb.net 中如何使用SQL語句查詢資料庫中的數據
1、首先打開Visual Studio 2008代好瞎兄碼窗口,添加引用。
❻ vb 資料庫查詢語句
like語句要緩檔核配合sql里的通配符進行模糊查詢的,1
,11
,111
這三個數是這樣
sql
=
"蠢滾select
*
from
廠家資料
where
廠家手機擾掘
like
'"
&
trim(text1.text)
&
"%'"
❼ VB資料庫查詢日期
說明:
仔細看了看你的代碼,你肯定是用combox控制項來組合了一個年
月
日的
時間表,其實有個DTpicker控制項
可能更適合.但你這樣做也沒有錯,某些情況下也具有優點.
正確使用sql,在這里顫洞猜
你要查詢的條件是對的.但你資料庫里的日期欄位的格式一定要設置正確(相匹配的),否則在查詢有於時顫改間變數原因可能會出現意想不茄型到的結果,這就是你錯誤的原因所在.
下面是你要的sql語句.代入使用即刻
sql="select
*
from
data
where
sf='"
&
text1.text
&"'
and
beteewn
'"&
data1
&"'
and
'"&
data1
&"'"
❽ 求助:VB資料庫查詢
周末沒上線,今早才看到你的消息
Option Explicit
Dim CNN As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Private Sub Form_Load()
Dim Cnstr As String, I As Integer
'設置資料庫連接
Cnstr = "Provider=Microsoft.Jet.OLEDB.4.0;"
Cnstr = Cnstr + "Data Source=" + App.Path & "\XX.MDB;"
CNN.ConnectionString = Cnstr
'讀取combo(0 - 4)下拉列表
CNN.Open
Rs.Open "select 品種 from TT", CNN
Do While Not Rs.EOF
For I = 0 To 4
Combo1(I).AddItem Rs.Fields(0).Value
Next I
Rs.MoveNext
Loop
Rs.Close
CNN.Close
'請選擇
For I = 0 To 4
Combo1(I).Text = "請選擇品種"
Next I
End Sub
Private Sub Command1_Click() '顯銷冊昌示
Dim I As Integer
List1.Clear
For I = 0 To 4
If Combo1(I).Text <> "請選擇姿陪品種" Then
List1.AddItem Combo1(I).Text
End If
Next I
End Sub
Private Sub Command2_Click() '排序
Dim I As Integer, J As Integer
List2.Clear
For I = 0 To List1.ListCount - 1
For J = 0 To List2.ListCount - 1
If Val(Left(List2.List(J), 2)) >= Val(Left(List1.List(I), 2)) Then Exit For
Next J
List2.AddItem List1.List(I), J
Next I
End Sub
'其實排序這里有個不費腦筋的辦法,只要在窗體設虧扒計時把list2的sorted屬性設為TRUE,它就會把添加進來的數據自動排序
Private Sub Command3_Click() '查詢
Dim I As Integer, N As Integer
If Text1.Text = "" Then
MsgBox "請填寫間隔數。", vbOKOnly, "提示"
Exit Sub
End If
N = Val(Text1.Text)
List3.Clear
CNN.Open
For I = 0 To List2.ListCount - 1
Rs.Open "select * from TT where 品種 ='" + List2.List(I) + "'", CNN
If 1 + I * N <= 9 Then
List3.AddItem Rs.Fields(1 + I * N).Value
Else
List3.AddItem Rs.Fields(9).Value
End If
Rs.Close
Next I
CNN.Close
End Sub
❾ vb如何做資料庫查詢功能
Adodc1.RecordSource = "select " & text1.text & " from dbo.Table_tl where 後面是查詢條件。比如日期=?and時間=?之類的「
這樣會返回資料庫列值卜辯。還有森帶一個方法是查詢是select * from 但顯示的時間用 recordset1("壓力值"型春缺)
這樣得到的也是壓力值。
❿ VB如何實現查詢資料庫並顯示出來
可以快速導出使用excel 就有該功能
PublicFunctionExportToExcel(ByValstrOpenAsString,TitleAsString,diAsString,conAsADODB.Connection)
'*********************************************************
'*名稱:ExporToExcel
'*功能:導出數據到EXCEL'*用法:ExporToExcel(strOpen查詢字元串,titile
'*excel標題,di保存路徑,con資料庫連接地址)
'*********************************************************
lok:OnErrorGoToer
Screen.MousePointer=11
DimRs_DataAsNewADODB.Recordset
DimIrowcountAsLong
DimIcolcountAsLong
DimXlAppAsNewExcel.Application
DimxlbookAsExcel.Workbook
DimxlSheetAsExcel.Worksheet
DimxlQueryAsExcel.QueryTable
WithRs_Data
If.State=adStateOpenThen
.Close
EndIf
.ActiveConnection=con
.CursorLocation=adUseClient
.CursorType=adOpenStatic
.LockType=adLockReadOnly
.Source=strOpen
DoEvents
'Debug.PrintstrOpen
.Open
EndWith
Debug.PrintstrOpen
'SetRs_Data=Open_rst_from_str(strOpen)
WithRs_Data
If.RecordCount<1Then
MsgBox("沒有記錄!")
Screen.MousePointer=0
ExitFunction
EndIf
'記錄總數
Irowcount=.RecordCount
'欄位總數
Icolcount=.Fields.Count
EndWith
SetXlApp=CreateObject("Excel.Application")
Setxlbook=Nothing
SetxlSheet=Nothing
Setxlbook=XlApp.Workbooks().Add
SetxlSheet=xlbook.Worksheets("sheet1")
'添加查詢語句,導入EXCEL數據
SetxlQuery=xlSheet.QueryTables.Add(Rs_Data,xlSheet.Range("a1"))
WithxlQuery
.FieldNames=True
.RowNumbers=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.RefreshOnFileOpen=False
.BackgroundQuery=True
.RefreshStyle=xlInsertDeleteCells
.SavePassword=True
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.PreserveColumnInfo=True
EndWith
xlQuery.FieldNames=True'顯示欄位名
xlQuery.Refresh
DimiAsInteger,ZdAsString
WithxlSheet
Fori=1To6
Zd=.Range(.Cells(1,1),.Cells(1,Icolcount)).item(1,i)
'.Range(.Cells(1,1),.Cells(1,Icolcount)).Item(1,i)=Lm_YwToZw(Zd)
Next
.Range(.Cells(1,1),.Cells(1,Icolcount)).Font.name="黑體"
'設標題為黑體字
'.Range(.Cells(1,1),.Cells(1,Icolcount)).Font.Bold=True
'標題字體加粗
.Range(.Cells(1,1),.Cells(Irowcount+1,Icolcount)).Borders.LineStyle=xlContinuous
'.Range(.Cells(Irowcount+2,Icolcount)).Text=Zje
'設表格邊框樣式
EndWith
XlApp.Visible=True
XlApp.Application.Visible=True
'xlBook.SaveAsdi
SetXlApp=Nothing'"交還控制給Excel
Setxlbook=Nothing
SetxlSheet=Nothing
Screen.MousePointer=0
ExitFunction
er:
'Dispose_Err
MsgBoxerr.Description&"從新導報表,請等待!"
GoTolok:
EndFunction
使用這個模塊就可以,你可以看看引用的函數即可