Excel VBA连接并操作Oracle
以下是通过Excel 的VBA连接Oracle并操作Oracle相关数据的示例
Excel 通过VBA连接数据库需要安装相应的Oracle客户端工具并引用ADO的相关组件,引用ADO相关组件可按如下步骤操作:
1、打开VBA编辑器,在菜单中点选“工具”,“引用”;
2、确保“Microsoft ActiviteX Data Objects 2.8 Library”和“Microsoft ActiviteX Data ObjectS Recordset 2.8 Library”被勾选上。
建立连接过程,代码如下:
可以根据需要调整SQL语句,获取相关数据,并输出到Excel完成数据处理
上述代码在Windows XP SP3/2003 SP2 + Office2003下测试通过.
Excel 通过VBA连接数据库需要安装相应的Oracle客户端工具并引用ADO的相关组件,引用ADO相关组件可按如下步骤操作:
1、打开VBA编辑器,在菜单中点选“工具”,“引用”;
2、确保“Microsoft ActiviteX Data Objects 2.8 Library”和“Microsoft ActiviteX Data ObjectS Recordset 2.8 Library”被勾选上。
建立连接过程,代码如下:
代码如下:
Public Sub ConOra() On Error GoTo ErrMsg: Dim ConnDB As ADODB.Connection Set ConnDB = New ADODB.Connection Dim ConnStr As String Dim DBRst As ADODB.Recordset Set DBRst = New ADODB.Recordset Dim SQLRst As String Dim OraOpen As Boolean OraOpen = False OraID="Orcl" 'Oracle数据库的相关配置 OraUsr="user" OraPwd="password" ConnStr = "Provider = MSDAORA.1;Password=" & OraPwd & _ ";User ID=" & OraUsr & _ ";Data Source=" & OraID & _ ";Persist Security Info=True" ConnDB.CursorLocation = adUseServer ConnDB.Open ConnStr OraOpen = True '成功执行后,数据库即被打开 'MsgBox "Connect to the oracle database Successful!", vbInformation, "Connect Successful" DBRst.ActiveConnection = ConnDB DBRst.CursorLocation = adUseServer DBRst.LockType = adLockBatchOptimistic SQLRst = "Select * From TstTab" DBRst.Open SQLRst, ConnDB, adOpenStatic, adLockBatchOptimistic DBRst.MoveFirst Exit Function ErrMsg: OraOpen = False MsgBox "Connect to the oracle database fail ,please check!", vbCritical, "Connect fail!" End Function Public Sub ConOra() On Error GoTo ErrMsg: Dim ConnDB As ADODB.Connection Set ConnDB = New ADODB.Connection Dim ConnStr As String Dim DBRst As ADODB.Recordset Set DBRst = New ADODB.Recordset Dim SQLRst As String Dim OraOpen As Boolean OraOpen = False OraID="Orcl" 'Oracle数据库的相关配置 OraUsr="user" OraPwd="password" ConnStr = "Provider = MSDAORA.1;Password=" & OraPwd & _ ";User ID=" & OraUsr & _ ";Data Source=" & OraID & _ ";Persist Security Info=True" ConnDB.CursorLocation = adUseServer ConnDB.Open ConnStr OraOpen = True '成功执行后,数据库即被打开 'MsgBox "Connect to the oracle database Successful!", vbInformation, "Connect Successful" DBRst.ActiveConnection = ConnDB DBRst.CursorLocation = adUseServer DBRst.LockType = adLockBatchOptimistic SQLRst = "Select * From TstTab" DBRst.Open SQLRst, ConnDB, adOpenStatic, adLockBatchOptimistic DBRst.MoveFirst Exit Function ErrMsg: OraOpen = False MsgBox "Connect to the oracle database fail ,please check!", vbCritical, "Connect fail!" End Function
可以根据需要调整SQL语句,获取相关数据,并输出到Excel完成数据处理
上述代码在Windows XP SP3/2003 SP2 + Office2003下测试通过.
相关推荐
lklong 2020-11-22
oraclemch 2020-11-06
shilukun 2020-10-10
周嘉笙 2020-11-09
zhuzhufxz 2020-09-16
bfcady 2020-08-16
Hody 2020-08-16
FightFourEggs 2020-08-16
数据库设计 2020-08-16
yanghuatong 2020-08-16
dbasunny 2020-08-16
罗罗 2020-08-16
ihuaqiang 2020-08-16
choice0 2020-07-30
娜娜 2020-07-28
solarspot 2020-07-28
踩风火轮的乌龟 2020-07-26
娜娜 2020-07-20
xwb 2020-07-19
娜娜 2020-07-18
流云追风 2020-07-04
dataminer 2020-06-25
娜娜 2020-06-22
zhangchaoming 2020-06-21