>我有一个问题。我必须编写一个与 MS Access (.mdb 文件) 与 OLEDB 连接的 ABAP 应用程序,但我的代码无法连接。我有 SY-SUBRC = 2。我该如何解决它?如何在 MS Access 中检查 SQL 中应该User ID=
的内容?
INCLUDE ole2incl.
DATA: con TYPE ole2_object,
rec TYPE ole2_object.
DATA SQL(1023).
DATA: BEGIN OF SPL OCCURS 0,
VAL(1023),
END OF SPL.
DATA: BEGIN OF I1 OCCURS 0,
F1(10),
F2 TYPE I,
END OF I1.
IF con-header IS INITIAL OR con-handle = -1.
CREATE OBJECT con 'ADODB.Connection'.
IF NOT sy-subrc = 0.
EXIT.
ENDIF.
CREATE OBJECT REC 'ADODB.Recordset'.
IF NOT sy-subrc = 0.
EXIT.
ENDIF.
ENDIF.
*MDB connetion infomations
CONCATENATE 'Provider=' '''Microsoft.Jet.OLEDB.4.0''' ';'
INTO SQL.
CONCATENATE SQL 'Password=' '''platnikbcc''' ';'
INTO SQL.
CONCATENATE SQL 'User ID=' '''Admin''' ';'
INTO SQL.
CONCATENATE SQL 'Data Source=' '''D:Platnik.mdb''' ';'
INTO SQL.
CONCATENATE SQL 'Mode=' '''Share Deny None'''
INTO SQL.
*MDB connection
CALL METHOD OF CON 'Open'
EXPORTING #1 = SQL.
*Query run
*SY-SUBRC still equals 0
CALL METHOD OF REC 'Open'
EXPORTING #1 = SQL
#2 = CON
#3 = '1'.
*SY-SUBRC = 2 FROM HERE
*Query (select) statement ...
SQL = 'select * from UBEZP_IDENT'.
*Query run
CALL METHOD OF REC 'Open'
EXPORTING #1 = SQL
#2 = CON
#3 = '1'.
*Selecting MDB record into SAP internal table
DO.
CALL METHOD OF REC 'getstring' = SQL
EXPORTING #1 = '2' "Do not modify!
#2 = 1 "Do not modify!
#3 = '|' "Do not modify!
#4 = '|'. "Do not modify!
IF sy-SUBRC EQ 0.
REFRESH SPL. CLEAR SPL.
SPLIT SQL AT '|' INTO TABLE SPL.
LOOP AT SPL.
CASE SY-TABIX.
WHEN 1.
I1-F1 = SPL-VAL.
WHEN OTHERS.
I1-F2 = SPL-VAL.
ENDCASE.
ENDLOOP.
APPEND I1. CLEAR I1.
ELSE.
EXIT.
ENDIF.
ENDDO.
*Result writing...
LOOP AT I1.
WRITE: AT /1(10) I1-F1,
AT (10) I1-F2.
ENDLOOP.
*Connetion close & destroy
FREE OBJECT con.
FREE OBJECT rec.
我可以自己回答! :)
我更改了代码。我在这里不写关于管理从MDB文件下载的数据。
INCLUDE ole2incl.
TYPE-POOLS ole2.
DATA: conn TYPE ole2_object,
rsdb TYPE ole2_object,
sql TYPE c LENGTH 1024.
*You need here something to manage downloaded data!
*Create ADODB Object:
START-OF-SELECTION.
CREATE OBJECT conn 'ADODB.Connection'.
CREATE OBJECT rsdb 'ADODB.Recordset'.
*Set connection and run query:
*MDB Connetion infomations
CONCATENATE 'Provider=Microsoft.Jet.OLEDB.4.0;'
'Data Source=D:File.mdb;' 'Jet OLEDB:Database Password=''p@s$w0rD'';'
INTO sql.
CALL METHOD OF conn 'Open' EXPORTING #1 = sql.
*Query Statement ...
sql = 'SELECT * FROM EXAMPLE'.
*Run Query
CALL METHOD OF rsdb 'OPEN'
EXPORTING
#1 = sql
#2 = conn
#3 = '1'.
*Save records
DO.
CALL METHOD OF rsdb 'getstring' = sql
EXPORTING #1 = '2' "Do not modify!
#2 = 1 "Do not modify!
#3 = ' ' "Do not modify!
#4 = ' '. "Do not modify!
IF sy-subrc EQ 0.
*Save data into (for example) internal table.
ELSE.
EXIT.
ENDIF.
ENDDO.
*Manage your data, display, whatever