VBA中用ADO连接SqlServer2005
2010-10-25 13:09
113 查看
按Alt+F11打开代码窗口,点击菜单上的"工具">>"引用",勾选它:Microsoft ActiveX Data Objects 2.x
'================================
' 连接SqlServer2005
'================================
Private Sub ConnectDB()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strCnn As String
Dim strHost As String
Dim strDatabaseName As String
Dim strUserName As String
Dim strPassword As String
Dim strSQL As String
Dim lCountOfFields As Long
Dim lrow As Long
Dim lcol As Long
'================================
' 定义连接字符串
'================================
strHost = "10.203.0.XX" 'Sqlsever服务器IP地址或计算机名
strDatabaseName = "master" '连接数据库名
strUserName = "sa" '登录用户名
strPassword = "123" '登录密码
On Error GoTo ConnectErr:
'================================
' 建立连接
'================================
strCnn = ""
strCnn = strCnn & "Provider=SQLOLEDB;"
strCnn = strCnn & "Password=" & strPassword & ";"
strCnn = strCnn & "User ID=" & strUserName & ";"
strCnn = strCnn & "Initial Catalog=" & strDatabaseName & ";"
strCnn = strCnn & "Data Source = " & strHost
Set cnn= New ADODB.Connection
With cnn
.ConnectionString = strCnn
.Open
End With
MsgBox "Connect successfully!", vbOKOnly + vbInformation
'================================
' 查询数据集
'================================
Set rs = New ADODB.Recordset
strSQL = "select * from spt_values "
rs.Open strSQL, cnn, adOpenForwardOnly, adLockReadOnly
'================================
' 将数据集填充到sheet
'================================
lrow = 2
lcol = 2
With Worksheets("Sheet1")
lCountOfFields = rs.Fields.Count
.Range(Cells(lrow, lcol).Address & ":" & Cells(lrow + 2, lcol + lCountOfFields - 1).Address).ClearContents
.Cells(lrow, lcol).CopyFromRecordset rs
End With
rs.Close
Exit Sub
ConnectErr:
MsgBox "Connect error!", vbOKOnly + vbCritical
End Sub
----------------------------------------------------------------------------------
'================================
' 断开连接
'================================
Public Sub CloseConnect()
On Error GoTo DisconnectErr:
cnn.Close
Set cnn = Nothing
MsgBox "Disconnect successfully!", vbOKOnly + vbInformation
Exit Sub
DisconnectErr:
MsgBox "Disconnect error!", vbOKOnly + vbCritical
End Sub
'================================
' 连接SqlServer2005
'================================
Private Sub ConnectDB()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strCnn As String
Dim strHost As String
Dim strDatabaseName As String
Dim strUserName As String
Dim strPassword As String
Dim strSQL As String
Dim lCountOfFields As Long
Dim lrow As Long
Dim lcol As Long
'================================
' 定义连接字符串
'================================
strHost = "10.203.0.XX" 'Sqlsever服务器IP地址或计算机名
strDatabaseName = "master" '连接数据库名
strUserName = "sa" '登录用户名
strPassword = "123" '登录密码
On Error GoTo ConnectErr:
'================================
' 建立连接
'================================
strCnn = ""
strCnn = strCnn & "Provider=SQLOLEDB;"
strCnn = strCnn & "Password=" & strPassword & ";"
strCnn = strCnn & "User ID=" & strUserName & ";"
strCnn = strCnn & "Initial Catalog=" & strDatabaseName & ";"
strCnn = strCnn & "Data Source = " & strHost
Set cnn= New ADODB.Connection
With cnn
.ConnectionString = strCnn
.Open
End With
MsgBox "Connect successfully!", vbOKOnly + vbInformation
'================================
' 查询数据集
'================================
Set rs = New ADODB.Recordset
strSQL = "select * from spt_values "
rs.Open strSQL, cnn, adOpenForwardOnly, adLockReadOnly
'================================
' 将数据集填充到sheet
'================================
lrow = 2
lcol = 2
With Worksheets("Sheet1")
lCountOfFields = rs.Fields.Count
.Range(Cells(lrow, lcol).Address & ":" & Cells(lrow + 2, lcol + lCountOfFields - 1).Address).ClearContents
.Cells(lrow, lcol).CopyFromRecordset rs
End With
rs.Close
Exit Sub
ConnectErr:
MsgBox "Connect error!", vbOKOnly + vbCritical
End Sub
----------------------------------------------------------------------------------
'================================
' 断开连接
'================================
Public Sub CloseConnect()
On Error GoTo DisconnectErr:
cnn.Close
Set cnn = Nothing
MsgBox "Disconnect successfully!", vbOKOnly + vbInformation
Exit Sub
DisconnectErr:
MsgBox "Disconnect error!", vbOKOnly + vbCritical
End Sub
相关文章推荐
- VC++6.0通过ADO方式连接SQLServer2005
- VBA ADO连接语法及常用参数
- VBA中用ADO访问SQL SERVER数据库:数据查询
- 【VBA研究】利用ADO实现VBA连接Oracle并执行存储过程
- Delphi中用ADO连接数据库
- vba通过ado连接oracle执行SQL语句,报非法字符错误。(超级恶心)
- delphi中用ado连接Sybase驱动是Sybase.ASEOLEDBProvider.2
- Delphi中用ADO连接数据库
- VBA中初始化ADO连接的几种方法
- vba 用ADO连接不上sqlserver 错误提示:client unable to establish connection
- 一个在ASP中用ADO连接SQLServer2005的问题
- Delphi中用ADO控件连接数据库例子
- Delphi中用ADO连接数据库
- 在VS2008中通过ADO方式连接SQLSERVER2005
- excel-ADO技术-VBA连接mySQL 与orcal
- ADO.NET 连接字符串集合(Oracle)
- VC连接SQL2005(例子ADO_2)
- ADO连接access数据库
- 一个QTP中用ADO操作Excel的例子
- 【转】MFC下用ADO连接SQL SERVER,保存图片,BLOB