Vb连接数据库,使用ado。ado属性页中“命令文本(SQL)”怎么用?最好能举个例子

2025-01-08 00:06:39
推荐回答(2个)
回答1:

'首先须引用Projects-References-Microsoft Active X Data Objects 2.0 Library.
'*注意以下两段须写在程式最上面(Gerenal一般)中。

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL As String

'然后在Form_Load()输入以下代码:
Private Sub Form_Load()
cn.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0;Persist security info= False; Data source=" & App.Path & "\数据库名.mdb"
cn.Open

rs.CursorLocation = adUseClient
SQL = "select * from 表格名"
rs.Open Trim$(SQL), cn, adOpenKeyset, adLockPessimistic
End Sub

------------

'若是连接SQL数据库写法如下:

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL As String

'然后在Form_Load()输入以下代码:
Private Sub Form_Load()
cn.ConnectionString = "Provider=SQLOLEDB;Password=密码;Persist Security Info=False;User ID=用户名;Initial Catalog=数据库名;Data Source=服务器名"
cn.Open

rs.CursorLocation = adUseClient
SQL = "select * from 表格名"
rs.Open Trim$(SQL), cn, adOpenKeyset, adLockPessimistic
End Sub

回答2:

select * from 表