我很好
分类: WINDOWS
2008-08-21 21:15:47
REPORT z_barry_access.
INCLUDE ole2incl.
CONSTANTS: provider(40) VALUE 'Provider=Microsoft.Jet.OLEDB.4.0',
security(40) VALUE 'Persist Security Info=False',
datasource(30) VALUE 'Data Source=D:\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,
mandt LIKE t001-mandt ,
bukrs LIKE t001-bukrs ,
butxt LIKE t001-butxt ,
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 'con_str' = 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 from TEST_ZTEST'.
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-mandt.
CALL METHOD OF o_recordset 'fields' = o_field
EXPORTING
#1 = 1.
GET PROPERTY OF o_field 'Value' = itab-bukrs.
CALL METHOD OF o_recordset 'fields' = o_field
EXPORTING
#1 = 2.
GET PROPERTY OF o_field 'Value' = itab-butxt.
APPEND itab.
CALL METHOD OF o_recordset 'MoveNext'.
GET PROPERTY OF o_recordset 'EOF' = rs_eof.
ENDWHILE.
LOOP AT itab.
WRITE: / itab-mandt,itab-bukrs,itab-butxt.
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
|
nanyunjian2008-10-18 13:55:38
修改如下两处可以处理SQL数据库 1、CONSTANTS: provider(100) VALUE 'Provider=SQLOLEDB;Data Source=SQLserver;uid=sa;pwd=123456;database=ZTEST', 2、con_str = provider. 查询语句 sql_str = 'select top 10 * from ztest'.
nanyunjian2008-10-18 13:43:48
按照老白的就没有错了,上次作测试的时候CALL METHOD OF o_conn 'Open'.没有出错,是连通了库,没数据出来。