问题的提出:如何在form的程序中实现oracle与access表之间的导入和导出。
问题的解答:
准备工作:
1.安装oca。运行developer的安装盘,选择自定义安装,选择oracle open client adapter for odbc安装。
2.在数据源(odbc)中添加dsn。控制面板->管理工具->数据源(odbc),选择“用户dsn”,添加要进行操作的access的文件。在“高级”选项里,填上“登录名称”和“密码”(很重要,在程序中会用到)。
下面以实际例子来说明:
假设在oracle中和access中都有一个student表,表中字段相同(name char(10) ,age number(2)),在准备工作2中的“数据源名”为test,“登录名称”和“密码”都为user。
下面为从oracle导出到access的procedure:
以下是引用片段:
procedure oracle_to_access is
connection_id exec_sql.conntype;
action_cursor exec_sql.curstype;
ignore pls_integer;
t_name student.name%type;
t_age student.age%type;
cursor temp_cursor is select * from student;
begin
connection_id:= exec_sql.open_connection('user/[email protected]:test');
action_cursor := exec_sql.open_cursor(connection_id);
exec_sql.parse(connection_id, action_cursor,'delete * from student');
ignore := exec_sql.execute(connection_id, action_cursor);
exec_sql.close_cursor(connection_id,action_cursor);
open temp_cursor;
export_count := 0;
action_cursor := exec_sql.open_cursor(connection_id);
exec_sql.parse(connection_id, action_cursor,'insert into student(name,age) values(:1,:2)');
loop
fetch temp_cursor into t_name,t_age;
exit when temp_cursor%notfound;
exec_sql.bind_variable(connection_id,action_cursor, ':1', t_name);
exec_sql.bind_variable(connection_id,action_cursor, ':2', t_age);
ignore := exec_sql.execute(connection_id, action_cursor);
end loop;
close temp_cursor;
exec_sql.parse(connection_id, action_cursor,'commit');
ignore := exec_sql.execute(connection_id,action_cursor);
exec_sql.close_cursor(connection_id,action_cursor);
exec_sql.close_connection(connection_id);
exception
when exec_sql.package_error then
if exec_sql.last_error_code(connection_id) != 0 then
message('数据导出至access失败: ' || to_char(exec_sql.last_error_code(connection_id)) || ': ' || exec_sql.last_error_mesg(connection_id));
end if;
if exec_sql.is_connected(connection_id) then
if exec_sql.is_open(connection_id,action_cursor) then
exec_sql.close_cursor(connection_id,action_cursor);
end if;
exec_sql.close_connection(connection_id);
end if;
end;
下面为从access导出到oracles的procedure:
procedure access_to_oracle is
connection_id exec_sql.conntype;
action_cursor exec_sql.curstype;
ignore pls_integer;
t_name student.name%type;
t_age student.age%type;
begin
connection_id := exec_sql.open_connection('user/[email protected]:test');
action_cursor := exec_sql.open_cursor(connection_id);
delete from student;
exec_sql.parse(connection_id, action_cursor,'select name,age from student');
ignore := exec_sql.execute(connection_id, action_cursor);
exec_sql.define_column(connection_id,action_cursor,1,t_name,10);
exec_sql.define_column(connection_id,action_cursor,2,t_age);
ignore := exec_sql.execute(connection_id, action_cursor);
while(exec_sql.fetch_rows(connection_id,action_cursor)>0)
loop
exec_sql.column_value(connection_id,action_cursor,1,t_name);
exec_sql.column_value(connection_id,action_cursor,2,t_age);
insert into test(name,age) values(t_name,t_age);
end loop;
commit;
exec_sql.close_cursor(connection_id,action_cursor);
exec_sql.close_connection(connection_id);
exception
when exec_sql.package_error then
if exec_sql.last_error_code(connection_id) != 0 then
message('数据导入至oracle失败: ' || to_char(exec_sql.last_error_code(connection_id)) || ': ' || exec_sql.last_error_mesg(connection_id));
end if;
if exec_sql.is_connected(connection_id) then
if exec_sql.is_open(connection_id,action_cursor) then
exec_sql.close_cursor(connection_id,action_cursor);
end if;
exec_sql.close_connection(connection_id);
end if;
end;
新闻热点
疑难解答