急!VB怎样实现数据库的查找功能

急!VB怎样实现数据库的查找功能,第1张

Private Sub Command1_Click()

'建立链接

Dim Cntion As ADODBConnection

Dim RecSet As ADODBRecordset

Dim ConStr, RecStr As String

Set Cntion = New ADODBConnection

Set RecSet = New ADODBRecordset

ConStr = "Provider=MicrosoftJetOLEDB40;Persist Security Info=False;Data Source=D:\1mdb"

CntionOpen ConStr

'模糊搜索语句

RecStr = "SELECT FROM 表1 WHERE TEXT1 LIKE '%" & Text1Text & "%'"

'打开数据库

RecSetOpen RecStr, Cntion, adOpenKeyset, adLockOptimistic

'如果查询结果为空,跳到NotExist

If RecSetBOF <> True And RecSetEOF <> True Then

RecSetMoveFirst

Else

RecSetClose

GoTo NotExist

End If

'输出查询结果。其中输出文本框为MultiLine,可以输出多个结果。

Text2Text = ""

Do While RecSetEOF = False

Text2Text = Text2Text & RecSetFields("TEXT1") & Chr(13) & Chr(10)

RecSetMoveNext

Loop

RecSetClose

Exit Sub

NotExist:

'输出第一行

RecStr = "SELECT FROM 表1"

RecSetOpen RecStr, Cntion, adOpenKeyset, adLockOptimistic

If RecSetBOF <> True And RecSetEOF <> True Then

RecSetMoveFirst

End If

Text2Text = ""

Text2Text = RecSetFields("TEXT1")

RecSetClose

End Sub

工程->引用Microsoft ActiveX Data Objects 2x Library

'工程->引用Microsoft ADO Ext 2x for DDL and Security

'在窗体上有1个DataGrid、6个CommandButton、3个TextBox

Option Explicit

Public mCnnString As String

Dim mRst As New ADODBRecordset

Private Sub Command1_Click() '1在数据库mdb1上创建 "ID" "用户名" "密码" 字段的UserPassWord表

Dim mTbl As New Table

Dim mIdx As New ADOXIndex

Dim mCat As New ADOXCatalog

' 打开目录。

mCatActiveConnection = "Provider=MicrosoftJetOLEDB40;Data Source=" & AppPath & "\db1mdb" & ";Persist Security Info=False"

' 定义表并将其追加到库中

mTblName = "UserPassWord"

mTblParentCatalog = mCat

mTblColumnsAppend "ID", adVarWChar

mTblColumnsAppend "用户名", adVarWChar

mTblColumnsAppend "密码", adVarWChar

mCatTablesAppend mTbl

' 定义多列索引

mIdxName = "MultiColIdx"

mIdxColumnsAppend "ID"

' 将索引追加到表上

mTblIndexesAppend mIdx

End Sub

Private Sub Command2_Click() '2添加 "01" "小明" "999999" 到相应字段中的数据库文件中

Dim mCon As New ADODBConnection

mConCursorLocation = adUseClient

mConOpen mCnnString

mConExecute "Insert Into UserPassWord Values('" & "01'," & " '" & "小明" & "'," & " '999999" & "')"

mConClose

Set mCon = Nothing

End Sub

Private Sub Command3_Click() '3删除指定ID(Text1Text)整行的数据行

Dim mCon As New ADODBConnection

mConCursorLocation = adUseClient

mConOpen mCnnString

mConExecute "Delete From UserPassWord Where ID = '" & Trim(Text1Text) & "'"

mConClose

Set mCon = Nothing

End Sub

Private Sub Command4_Click() '4根据用ID(Text2Text)查询数据库

If mRstState = adStateOpen Then mRstClose

mRstCursorLocation = adUseClient

mRstOpen "Select From UserPassWord Where ID = '" & Trim(Text2Text) & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText

Set DataGrid1DataSource = mRst

End Sub

Private Sub Command5_Click() '4根据用户名(Text3Text)查询数据库

If mRstState = adStateOpen Then mRstClose

mRstCursorLocation = adUseClient

mRstOpen "Select From UserPassWord Where 用户名 = '" & Trim(Text3Text) & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText

Set DataGrid1DataSource = mRst

End Sub

Private Sub Command6_Click() '5清空数据表,保留字段

Dim mCon As New ADODBConnection

mConCursorLocation = adUseClient

mConOpen mCnnString

mConExecute "Delete From UserPassWord Where True"

mConClose

Set mCon = Nothing

End Sub

Private Sub Form_Load()

mCnnString = "Provider=MicrosoftJetOLEDB40;Data Source=" & AppPath & "\db1mdb" & ";Persist Security Info=False" '连接字符串

End Sub

Private Sub Form_Unload(Cancel As Integer) '退出时候释放mRst对象

If mRstState = adStateOpen Then

mRstClose

Set mRst = Nothing

End If

End Sub

用这个SQL语句

在ACCESS中

select from 表名 where 地址 like "哈尔滨"

在SQL中

select from 表名 where 地址 like '%哈尔滨%'

以上就是关于急!VB怎样实现数据库的查找功能全部的内容,包括:急!VB怎样实现数据库的查找功能、vb中用ado怎么实现数据库的查找功能代码、VB从Access数据库中查找的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10092480.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存