Vb连接数据库,使用ado。ado属性页中“命令文本(SQL)”怎么用?最好能举个例子
2个回答
展开全部
'首先须引用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
'*注意以下两段须写在程式最上面(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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询