Excel and DB2 connectivity

不想你离开。 提交于 2019-12-06 10:47:40

问题


I have to check whether the connection between Excel and DB2 has been established using CLI/ODBC driver or not.

For that I plan to write a batch file where I will be calling the excel sheet which in turn will automatically execute a macro which will bring out some dummy data from the sysibm.sysdummy1 table.

I require code with which I can make a connection to my database and check if the connection has been established or not by giving out some success message if the connection was established and a failure message if the connection was not established. (Probably with some explanation where the problem occurred)


回答1:


You can make an ODBC (or OleDB) connection between a DB2 server and Excel using ADODB (ActiveX Data Objects). See this link for sample connection strings.

This link will show you sample VBA code to use with ADODB to connect to your database: How To Use ADO with Excel Data from Visual Basic or VBA

EDIT: Here's some quick and dirty sample code. Replace the .connectionstring = portion with the proper connection string for your setup.

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

strSQL = "SELECT * FROM sysibm.sysdummy1 FETCH FIRST 10 ROWS ONLY"
Set cn = New ADODB.Connection

With cn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source=" & App.Path & _
"\ExcelSrc.xls;Extended Properties=Excel 8.0;"
    .Open
End With

rs.Open strSQL, cn

rs.MoveFirst
Do Until rs.EOF
    Debug.Print rs.Fields(0)
    rs.MoveNext
Loop

rs.Close
cn.Close

Set rs = Nothing
Set cn = Nothing


来源:https://stackoverflow.com/questions/7171546/excel-and-db2-connectivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!