vb中用ado怎么实现数据库的查找功能?代码?

vb中用ado怎么实现数据库的查找功能?代码?,第1张

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

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

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

Option Explicit

Public mCnnString As String

Dim mRst As New ADODB.Recordset

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

Dim mTbl As New Table

Dim mIdx As New ADOX.Index

Dim mCat As New ADOX.Catalog

' 打开目录。

mCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" & App.Path & "\db1.mdb" & "Persist Security Info=False"

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

mTbl.Name = "UserPassWord"

mTbl.ParentCatalog = mCat

mTbl.Columns.Append "ID", adVarWChar

mTbl.Columns.Append "用户名", adVarWChar

mTbl.Columns.Append "密码", adVarWChar

mCat.Tables.Append mTbl

' 定义多列索引

mIdx.Name = "MultiColIdx"

mIdx.Columns.Append "ID"

' 将索引追加到表上

mTbl.Indexes.Append mIdx

End Sub

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

Dim mCon As New ADODB.Connection

mCon.CursorLocation = adUseClient

mCon.Open mCnnString

mCon.Execute "Insert Into UserPassWord Values('" & "01'," & " '" & "小明" & "'," & " '999999" & "')"

mCon.Close

Set mCon = Nothing

End Sub

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

Dim mCon As New ADODB.Connection

mCon.CursorLocation = adUseClient

mCon.Open mCnnString

mCon.Execute "Delete From UserPassWord Where ID = '" & Trim(Text1.Text) & "'"

mCon.Close

Set mCon = Nothing

End Sub

Private Sub Command4_Click() '4.根据用ID(Text2.Text)查询数据库

If mRst.State = adStateOpen Then mRst.Close

mRst.CursorLocation = adUseClient

mRst.Open "Select * From UserPassWord Where ID = '" & Trim(Text2.Text) & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText

Set DataGrid1.DataSource = mRst

End Sub

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

If mRst.State = adStateOpen Then mRst.Close

mRst.CursorLocation = adUseClient

mRst.Open "Select * From UserPassWord Where 用户名 = '" & Trim(Text3.Text) & "'", mCnnString, adOpenStatic, adLockOptimistic, adCmdText

Set DataGrid1.DataSource = mRst

End Sub

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

Dim mCon As New ADODB.Connection

mCon.CursorLocation = adUseClient

mCon.Open mCnnString

mCon.Execute "Delete From UserPassWord Where True"

mCon.Close

Set mCon = Nothing

End Sub

Private Sub Form_Load()

mCnnString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" & App.Path & "\db1.mdb" & "Persist Security Info=False" '连接字符串

End Sub

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

If mRst.State = adStateOpen Then

mRst.Close

Set mRst = Nothing

End If

End Sub

select * from table where ````rs->open(.....)if(rs->adoEOF){ MessageBox("没有这个数据") rs->close() }rs->PutCollect(....)rs->Update()rs->Close

我刚做了一个在线程中使用ado的系统,实现方式如下:使用dm来存放

connection

,在各个线程中create

adoquery控件,connection属性设为dm中的connection,在使用完之后别忘了free。另外别忘了同步。好运!


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存