首页 > 数据库 > Oracle > 正文

Oracle与Access表之间的导入和导出实现

2024-08-29 13:32:19
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

  问题的提出:如何在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;

  注意:exec_sql.bind_variable中绑定的变量只能是以下三种类型:number,date,varchar2。对于access中的“是/否”的布尔型变量,可以用number类型的1和0来表示。如果access中的表名或者字段名中有空格,在写sql语句的时候可以用双引号把表名或者字段名包括起来,如:本例中如果access中表名为student detail,字段名分别为student name和student age,那插入数据的sql语句为:insert into “student detail”(“student name”,”student age”) values(:1,:2)。
 
  请作者联系本站,及时附注您的姓名。联系邮箱:edu#chinaz.com(把#改为@)。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表