转自老白文章
REPORT Z_BARRY_TEST_MDB2.
INCLUDE OLE2INCL.
CONSTANTS: PROVIDER(40) VALUE 'Provider=Microsoft.Jet.OLEDB.4.0',
SECURITY(40) VALUE 'Persist Security Info=False',
DATASOURCE(30) VALUE 'Data Source=F:\db1.mdb'.
DATA: O_CONN TYPE OLE2_OBJECT,
O_RECORDSET TYPE OLE2_OBJECT,
O_FIELD TYPE OLE2_OBJECT.
DATA: CON_STR(128) ,SQL_STR(400).
DATA: BEGIN OF TY_ITAB OCCURS 0,
A TYPE STRING ,
B TYPE STRING,
C TYPE STRING,
D TYPE STRING,
END OF TY_ITAB.
DATA: ITAB LIKE TABLE OF TY_ITAB WITH HEADER LINE.
START-OF-SELECTION.
PERFORM OLEDB_OPEN.
PERFORM SQL_SELECT.
PERFORM SQL_UPDATE.
PERFORM SQL_CLOSE.
*&---------------------------------------------------------------------*
*& Form oledb_open
*&---------------------------------------------------------------------*
FORM OLEDB_OPEN.
CONCATENATE PROVIDER ';' SECURITY ';' DATASOURCE INTO CON_STR.
CREATE OBJECT O_CONN 'ADODB.Connection'.
SET PROPERTY OF O_CONN 'Provider' = PROVIDER.
SET PROPERTY OF O_CONN 'ConnectionString' = CON_STR.
CALL METHOD OF O_CONN 'Open'.
ENDFORM. "OLEDB_open
*&---------------------------------------------------------------------*
*& Form sql_select
*&---------------------------------------------------------------------*
FORM SQL_SELECT .
DATA: RS_EOF TYPE I.
SQL_STR = 'select mandt, bukrs,butxt,add from TEST_ZTEST'. "取出Access数据
CALL METHOD OF O_CONN 'Execute' = O_RECORDSET
EXPORTING
#1 = SQL_STR.
GET PROPERTY OF O_RECORDSET 'EOF' = RS_EOF.
REFRESH ITAB.
WHILE RS_EOF NE 1.
CALL METHOD OF O_RECORDSET 'fields' = O_FIELD
EXPORTING
#1 = 0.
GET PROPERTY OF O_FIELD 'Value' = ITAB-A.
CALL METHOD OF O_RECORDSET 'fields' = O_FIELD
EXPORTING
#1 = 1.
GET PROPERTY OF O_FIELD 'Value' = ITAB-B.
CALL METHOD OF O_RECORDSET 'fields' = O_FIELD
EXPORTING
#1 = 2.
GET PROPERTY OF O_FIELD 'Value' = ITAB-C.
CALL METHOD OF O_RECORDSET 'fields' = O_FIELD
EXPORTING
#1 = 3.
GET PROPERTY OF O_FIELD 'Value' = ITAB-D.
APPEND ITAB.
CALL METHOD OF O_RECORDSET 'MoveNext'.
GET PROPERTY OF O_RECORDSET 'EOF' = RS_EOF.
ENDWHILE.
LOOP AT ITAB.
WRITE: / ITAB-A,ITAB-B,ITAB-C,ITAB-D.
ENDLOOP.
ENDFORM. "sql_select
*&---------------------------------------------------------------------*
*& Form sql_update
*&---------------------------------------------------------------------*
FORM SQL_UPDATE .
CONCATENATE 'update TEST_ZTEST set butxt=' '''' 'SAP AG' ''''
' where bukrs=' '''' '2005' '''' INTO SQL_STR.
CALL METHOD OF O_CONN 'Execute' = O_RECORDSET
EXPORTING
#1 = SQL_STR.
ENDFORM. "sql_update
*&---------------------------------------------------------------------*
*& Form sql_close
*&---------------------------------------------------------------------*
FORM SQL_CLOSE.
CALL METHOD OF O_RECORDSET 'Close'.
CALL METHOD OF O_CONN 'Close'.
FREE OBJECT O_RECORDSET.
FREE OBJECT O_CONN.
ENDFORM. "sql_close