ASP.NET 存储过程导入(oracle)返回导入成功数和导入失败数

存储过程格式

 1 create or replace procedure 存储过程名 ([参数] [参数类型],[参数] [参数类型])
 2 as
 3
 4  successc number;
 5  failc number;
 6 begin
 7     insert into [表名](
 8     字段,
 9     .
10     .
11     .
12     字段)
13     (select
14     字段,
15     .
16     .
17     .
18     字段
19     from [表名] where [条件]);
20
21     update [表名] set xx=‘xx‘ where [条件] ;
22
23     commit;
24     select  count(1) into successc  from [表名] where  Flag=‘1‘;
25     select count(1) into failc  from [表名] where  nvl(flag,0)<>1;
26
27      open cur_arg for  select successc as successc,failc as failc from dual; //打开游标查找
28   end;

存储过程

create or replace procedure Proc_PX_SchoolInportScore(PlanType nvarchar2,BanQGuid nvarchar2,CountryNums nvarchar2, cur_arg out sys_refcursor)
as

 successc number;
   failc number;
begin

update PX_SchoolInportScore set CountryNum=CountryNums where nvl( identitynum,‘‘)<>1;
    --导入成绩
   insert into pk_user(RowGuid,
             Name,--姓名
             IdentityNum,--身份证号码
             Age,--年龄
             Sex,--性别
             EducationCode,--学历
             DanWeiName,--单位名称
             WorkType,--工种
             IsEnable,--是否启用
             Status,--状态
             UserType--类别
             )
   (--插入到人员表   条件:临时表里的人员不在人员表中  判断依据 IdentityNum
   select
   LOWER(sys_guid()),
   Name,
   IdentityNum,
   Age,
   (case Sex when cast(‘男‘ as nvarchar2(10)) then cast(‘0‘ as nvarchar2(10))  else cast(‘1‘ as nvarchar2(10))  end) as Sex,
   (select ItemValue from VIEW_CodeMain_CodeItems where CodeName=‘PX_学历‘ and ItemText=Education) as EducationCode,
   DanWeiName,
   WorkType,
   ‘1‘,
   ‘1‘,
   ‘0‘
   from PX_SchoolInportScore
   WHERE not exists (select pk_user.IdentityNum from pk_user WHERE  PX_SchoolInportScore.IdentityNum=pk_user.IdentityNum)
   );

   insert into PX_BaoM(RowGuid,
             Name,
             UserGuid,
             LoginID,
             DanWeiName,
             PXPrograms,
             Note,
             CountryNum,
             Ispay
             )
   (--插入到报名表    条件:从临时表里插入成功到人员表中的人  并且这些人不存在报名表中  条件:人员表的RowGuid   报名表的UserGuid
      select LOWER(sys_guid()),
           a.Name,
           b.RowGuid,
           a.IdentityNum,
           a.DanWeiName,
           ‘02‘,
           Note,
           CountryNum,
           ‘1‘
      from PX_SchoolInportScore a inner join pk_user b on a.IdentityNum=b.IdentityNum
      --where b.rowguid not in (select UserGuid from PX_BaoM)
      where not exists (select UserGuid from PX_BaoM where userguid= b.rowguid)
     );

     insert into PX_BaoMDetail(
                               RowGuid,
                               ItemGuid,
                               IsDel,
                               ClassGuid,
                               ParentGuid
                                )
     (--插入到报名子表   条件:插入成功到报名表里的人  并且这些人不在子表中       子表的ParentGuid  报名表的 RowGuid
      select
      LOWER(sys_guid()),
      PlanType,
      ‘0‘,
      BanQGuid,
      c.RowGuid
            from PX_SchoolInportScore a
            join pk_user b on a.IdentityNum=b.IdentityNum
            join PX_BaoM c on  b.rowguid =c.UserGuid
            where c.RowGuid not in (select ParentGuid  from PX_BaoMDetail)
     );

 update PX_BaoM set CountryNum=CountryNums,IsPay=‘1‘ where RowGuid
         in ( select a.RowGuid from View_Score_UserType a inner join PX_SchoolInportScore b on
              a.IdentityNum=b.IdentityNum where  a.ClassGuid=BanQGuid and a.TypeGuid=PlanType
              );

update PX_SchoolInportScore set flag=1 where IdentityNum in (select LoginID  from view_schoolimportscore where classGuid=BanQGuid );

commit;

     select  count(1) into successc  from PX_SchoolInportScore where  Flag=‘1‘;
     select count(1) into failc  from PX_SchoolInportScore where  nvl(flag,0)<>1;

   open cur_arg for  select successc as successc,failc as failc from dual;
  end;

PX_SchoolInportScore   临时表

ASP.NET代码

protected void upload1_FileUploadCompleted_Custom(object sender, EventArgsOperate.AttachEventArgs[] args)
        {
            if (!Directory.Exists(Server.MapPath(@"ImportExcel")))
                Directory.CreateDirectory(Server.MapPath(@"ImportExcel"));
            string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
            string oldfileName = args[0].FileName;
            string documentType = oldfileName.Substring(oldfileName.LastIndexOf(‘.‘), oldfileName.Length - oldfileName.LastIndexOf(‘.‘));
            string fileName = "Import_" + mark + documentType;
            args[0].CuteArgs.CopyTo(Server.MapPath(@"ImportExcel\") + fileName);

            if (!string.IsNullOrEmpty(DDLBQ.SelectedValue))
                ReadExcel(Server.MapPath(@"ImportExcel\") + fileName);
            else
            {
                this.AlertAjaxMessage("请选择班次");
            }
        }

public void ReadExcel(string ExcelFile)
        {
            DataSet ds;
            string ms = "0";//记录导入成功数
            string mf = "0";//记录导入失败数
            string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFile + ";" + "Extended Properties=‘Excel 12.0‘;";

            OleDbConnection conn = new OleDbConnection(strConn);
            DataTable dtExcelSchema = new DataTable();
            try
            {
                conn.Open();
                dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                    new object[] { null, null, null, "Table" });//获取需要上传的Excel的Sheet
                conn.Close();
            }
            catch
            {
                throw;
            }
            for (int k = 0; k < dtExcelSchema.Rows.Count; k++)
            {
                try
                {
                    string SheetName = (string)dtExcelSchema.Rows[k]["TABLE_NAME"];
                    if (SheetName.Contains("$") && !SheetName.Replace("‘", "").EndsWith("$"))
                        continue;//过滤无效SheetName

                    //OleDbDataAdapter oada = new OleDbDataAdapter("select * from [Sheet" + k + "$]", strConn);
                    OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + SheetName + "]", strConn);
                    ds = new DataSet();
                    oada.Fill(ds);
                }
                catch
                {
                    throw;
                }

                DataTable dt = ds.Tables[0];
                int count = dt.Rows.Count;
                //
                if (count == 0 || (count > 0 && dt.Rows[0][0].ToString() == ""))
                    continue;//过滤无数据的sheet
                string tableName = "PX_SchoolInportScore";

                bool flag = false;

                BulkToDB(dt, tableName, out flag);
                if (flag)
                {
                    M_PX_ZhuanYe m_zy = b_zy.SelectByZhuanYeNum_PXProgram(plantype, programs);
                    DataSet dsscore = b_bmd.InportSchoolScore(m_zy.RowGuid, DDLBQ.SelectedValue, DDLCountry.SelectedValue);
                    // 展示成功多少个 失败多少个
                    if (dsscore.Tables.Count > 0)
                    {
                        DataView dvs = dsscore.Tables[0].DefaultView;
                        ms = dvs.Count > 0 ? dvs[0]["successc"].ToString() : "0";
                        mf = dvs.Count > 0 ? dvs[0]["failc"].ToString() : "0";
                    }
                    b_bmd.DeleteSchoolScoreAll();//每次导入完成后删除零时表中数据
                }
            }
            string alerterror = "成功导入:" + ms + "人,导入失败:" + mf + "人";
            this.AlertAjaxMessage(alerterror);
        }

public static void BulkToDB(DataTable dt, string targetTable, out bool flag)
        {
            string connectionString = System.Configuration.ConfigurationManager.AppSettings["CoonString"].ToString();
            bool flags = false;
            OracleConnection conn = new OracleConnection(connectionString);

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            OracleBulkCopy bulkCopy = new OracleBulkCopy(conn, OracleBulkCopyOptions.Default);
            bulkCopy.BatchSize = 100000;
            bulkCopy.BulkCopyTimeout = 260;
            bulkCopy.DestinationTableName = targetTable;
            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                if (dt != null && dt.Rows.Count != 0)
                {
                    bulkCopy.ColumnMappings.Add("姓名", "Name");
                    bulkCopy.ColumnMappings.Add("性别", "Sex");
                    bulkCopy.ColumnMappings.Add("年龄", "Age");
                    bulkCopy.ColumnMappings.Add("学历", "Education");
                    bulkCopy.ColumnMappings.Add("工作单位", "DanWeiName");
                    bulkCopy.ColumnMappings.Add("工种", "WorkType");
                    bulkCopy.ColumnMappings.Add("身份证号码", "IdentityNum");
                    bulkCopy.WriteToServer(dt);
                    flags = true;

                }
            }
            catch (Exception ex)
            {
                flags = false;
                Epoint.Frame.Common.LogOperate.WriteLog(ex.ToString());
            }
            finally
            {
                flag = flags;
                conn.Close();
                if (bulkCopy != null)
                    bulkCopy.Close();

            }

        }

时间: 2024-10-15 05:44:26

ASP.NET 存储过程导入(oracle)返回导入成功数和导入失败数的相关文章

Asp.NET Excel导入数据返回JSON字符

前端创建ID为ExcelDy的一个按钮 以及 一个有导入按钮的窗体 <a id="ExcelDy" class="left disabled" href="javascript:void(0)"><span> <img alt='' src='../images/primeval/import.bmp' /></span>导入入库</a> <span class="lef

Excel导入oracle库

Excel导入oracle库 ? 建表 ? /*==============================================================*/ /* DBMS name: ORACLE Version 10gR2 */ /* Created on: 2017/9/18/周一 14:19:00 */ /*==============================================================*/ ? ? drop table D

简析将shp导入Oracle并利用geoserver将导入的数据发布

1.环境准备 1.1 软件准备 首先要安装有支持空间数据的Oracle,其次有安装版或免安装版的geoserver,两者都部署好后,我们将开始讨论进一步需要配置的环境. 这里我使用由Oracle提供的shp2sdo来进行shp导入,同时使用由geoserver提供的可以连接Oracle数据源的插件来进行数据的发布. Shp2sdo的下载地址为:http://down.51cto.com/data/223757. Geoserver的Oracle插件下载地址为:http://sourceforge

SqlServer2012导入Oracle详细案例

第一次使用SqlServer2012,界面和VS2012风格一致了,Great! 进入正题,这篇博文主要写一下自己亲测的一个案例,使用SqlServer2012的“导入和导出数据”功能向Oracle11g导入数据表,本机装的是SqlServer2012和Oracle11g客户端,Oracle实例安装在服务器192.168.40.115上. 打开“导入和导出数据”工具,第一个选择数据源,本地服务器名称就写点号“.”或者“localhost”,身份验证选择第一个即可,然后选择要导出的数据库名称,接着

loadjava 把java导入Oracle数据库

1)load jar file loadjava -r -f -verbose -resolve -user xmlbook/xmlbook xmlparserv2.jar  2)load class file loadjava -r -f  -user user_name/password[@URL] oracle/AAA/bbb/CCC.class  3)remove Java classes from the database dropjava -r -f  -user user_name

Oracle简单的备份和恢复-导入和导出

ylbtech-Oracle:Oracle简单的备份和恢复-导入和导出  Oracle安全运行离不开良好的备份和恢复机制,因为我们不是DBA.所以我们也就不过多的讲解DBA的备份和恢复.作为程序员开发者来说,必须了解的是数据的导入和导出,利用这个方法我们可以搬迁数据库和数据. 1. 导出(exp)返回顶部 1.1, Oracle简单的备份和恢复-导出(exp) 1.2, 1.3, 2. 导入(imp)返回顶部 2.1, Oracle简单的备份和恢复-导入(imp) 2.2, 2.3, 3. 导入

Oracle 11g导出来的dmp导入到 10g的数据库(IMP-00010:不是有效的导出文件,头部验证失败)

因为喜欢新的东西,所以基本上电脑的开发工具都是最新的,oracle也装了11g R2,但是同事同学不是11g R1就是10G的,从我这里导出去的dmp文件在他们那边导进去,都显示: IMP-00010:不是有效的导出文件,头部验证失败 为了这个问题一直苦恼,差点就想卸掉11g然后装10g了,后来想想,头部验证,那么头部到底是什么,用Notepad++查看了dmp文件,发现头部真的显示一些东西: 11g R2:V11.02.00 11g R1:V11.01.00 10g:V10.02.01 把版本

csv、excel导入oracle

hcsv导入oracle #coding:gbk import csv import cx_Oracle fields = [] data = [] table_name = 'ygl_test3' file_name = '清单.csv' with open(file_name, 'rb') as f: reader = csv.reader(f) contents = [i for i in reader] title = contents[0] data = contents[1:] co

大批量数据导入数据库,dbf导入oracle,csv导入oracle

Excel 07-2003一个工作表最多可有65536,行最多可有256列:Excel 2007及以后版本,一个工作表最多可有1048576行,16384列. 一.小批量数据处理: 方法一:用excel可以直接打开dbf或csv文件,数据库表for update,直接把excel表中想要的列值直接复制到pl/sql中 相对应字段. 方法二:若是dbf文件可用dbfplus工具转换成csv文件,打开pl/sql中的 工具>文本导入器,在'到Oracle的数据'中选择到导入哪个用户下的哪个表,点击左