Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将每500个员工id发送到Oracle以解决ORA-07195问题_C#_Asp.net_.net_Oracle_Oracle Sqldeveloper - Fatal编程技术网

C# 如何将每500个员工id发送到Oracle以解决ORA-07195问题

C# 如何将每500个员工id发送到Oracle以解决ORA-07195问题,c#,asp.net,.net,oracle,oracle-sqldeveloper,C#,Asp.net,.net,Oracle,Oracle Sqldeveloper,我已经有了为每个员工生成stringBuilder的代码,我从另一个表中获取了所有employeesId。但是如果我有超过1000名员工,我会得到错误ORA-07195,我知道这个错误与列表中的最大表达式有关。因此,如何将每500名员工发送到数据访问对象中的查询 Public List<GraphModel> countRequestCreatedByTypeDefaulPage(int year, int month, String employeeID) {

我已经有了为每个员工生成stringBuilder的代码,我从另一个表中获取了所有employeesId。但是如果我有超过1000名员工,我会得到错误ORA-07195,我知道这个错误与列表中的最大表达式有关。因此,如何将每500名员工发送到数据访问对象中的查询

Public List<GraphModel> countRequestCreatedByTypeDefaulPage(int year, int month, String employeeID)
    {
        int count = 0;
        int countEmployeess = 0;
        string employeesid = "";

        DataView dv = _employeeOverrideBO.getRelatedEmployees(year, month, employeeID);
        StringBuilder listEmployees = new StringBuilder();

        for (int i = 0; i < countEmployees; i += 500) 
        {

        foreach (DataRowView rowView in dv)
        {
            DataRow row = rowView.Row;
            String employee = row["EMPLOYEE_ID"].ToString();
            if (count > 0)
                listEmployees.Append(",");
            listEmployees.Append("'").Append(employee).Append("'"); 
            count++;
        }

        }
        countEmployeess++;
        employeesid = listEmployees.ToString();

        return _requestDAO.countRequestCreatedByTypeDefaulPage(employeesid);
Public List countRequestCreatedByTypeDefaulPage(整数年、整数月、字符串employeeID)
{
整数计数=0;
int countEmployeess=0;
字符串employeesid=“”;
DataView dv=_employeeOverrideBO.getRelatedEmployees(年、月、employeeID);
StringBuilder listEmployees=新建StringBuilder();
对于(int i=0;i0)
listEmployees.追加(“,”);
listEmployees.Append(“”)。Append(employee.Append(“”);
计数++;
}
}
countEmployeess++;
employeesid=listEmployees.ToString();
返回_requestDAO.countRequestCreatedByTypeDefaulPage(employeesid);
这也是我在数据访问对象中的查询

public List<GraphModel> countRequestCreatedByTypeDefaulPage(string employeesIds)
    {
        String sql = " select NVL(TO_CHAR(RR.REASON_NM_NEW), 'Total') as SERIES1, count(*) AS VAL" +
                     " from REQUEST R, REQUEST_PERSON RP, REQUEST_REASON RR " +
                     " WHERE R.STATUS IN ('CREATED', 'PENDING APPROVAL', 'APPROVED BY MANAGER', 'APPROVED', 'IN PROCESS') " +
                     " AND R.REQUEST_ID = RP.REQUEST_ID" +
                     " AND RP.REQUEST_ROLE = 'REQUESTOR' " +
                     " AND RR.REASON_ID = R.REASON_ID" +
                     " AND RP.EMPLOYEE_ID IN (" + employeesIds + ") " +
                     " group by rollup (RR.REASON_NM_NEW) " + 
                     " ORDER BY count(*)  DESC";
        OracleCommand cmd = new OracleCommand(sql);


        try
        {
            DataTable dataTable = Data_base_Access.executeSQL(cmd, ConfigurationManager.ConnectionStrings["stage"].ToString());
            return (GraphModel.convertToList(dataTable));
        }
        catch (Exception ex)
        {
            Log.writeError("Request DAO", ex);
            throw new DataAccessException("There was an error counting the open requests");
        }
    }
public List countRequestCreatedByTypeDefaulPage(字符串employeesIds)
{
String sql=“选择NVL(TO_CHAR(RR.REASON_NM_NEW),'Total')作为SERIES1,count(*)作为VAL”+
“来自请求R、请求人RP、请求原因RR”+
“其中R.状态为(‘已创建’、‘待批准’、‘已经理批准’、‘已批准’、‘正在进行’)”+
“和R.REQUEST\u ID=RP.REQUEST\u ID”+
“和RP.REQUEST_ROLE='REQUESTOR'”+
“和RR.REASON\u ID=R.REASON\u ID”+
“和RP.EMPLOYEE_ID在(“+employeesIds+”)中”+
“分组汇总(RR.REASON\u NM\u NEW)”+
“按计数排序(*)说明”;
OracleCommand cmd=新的OracleCommand(sql);
尝试
{
DataTable DataTable=Data_base_Access.executeSQL(cmd,ConfigurationManager.connectionString[“stage”].ToString());
返回(GraphModel.convertToList(dataTable));
}
捕获(例外情况除外)
{
Log.writeError(“请求DAO”,ex);
抛出新的DataAccessException(“计算打开的请求时出错”);
}
}
此查询还获取名为GraphModel的列表计数

     public static List<GraphModel> convertToList(System.Data.DataTable dataTable)
    {
        List<GraphModel> list = new List<GraphModel>();
        foreach (DataRow dtRow in dataTable.Rows)
        {
            list.Add(convertToGraphModel(dtRow));
        }
        return list;
    }

    public static GraphModel convertToGraphModel(DataRow dtRow)
    {

        GraphModel graphModel = new GraphModel();

        if (dtRow.Table.Columns.Contains("SERIES1") && dtRow["SERIES1"] != DBNull.Value)
        {
            graphModel.SERIES1 = Convert.ToString(dtRow["SERIES1"]);
        }

        if (dtRow.Table.Columns.Contains("SERIES2") && dtRow["SERIES2"] != DBNull.Value)
        {
            graphModel.SERIES2 = Convert.ToString(dtRow["SERIES2"]);
        }

        if (dtRow.Table.Columns.Contains("VAL") && dtRow["VAL"] != DBNull.Value)
        {
            graphModel.VAL = Convert.ToInt32(dtRow["VAL"]);
        }


        return graphModel;
    }
}
公共静态列表转换器列表(System.Data.DataTable)
{
列表=新列表();
foreach(dataTable.Rows中的DataRow dtRow)
{
list.Add(convertToGraphModel(dtRow));
}
退货清单;
}
公共静态图形模型convertToGraphModel(DataRow dtRow)
{
GraphModel GraphModel=新GraphModel();
if(dtRow.Table.Columns.Contains(“SERIES1”)和&dtRow[“SERIES1”!=DBNull.Value)
{
graphModel.SERIES1=Convert.ToString(dtRow[“SERIES1]”);
}
if(dtRow.Table.Columns.Contains(“SERIES2”)和&dtRow[“SERIES2”!=DBNull.Value)
{
graphModel.SERIES2=Convert.ToString(dtRow[“SERIES2]”);
}
if(dtRow.Table.Columns.Contains(“VAL”)和&dtRow[“VAL”!=DBNull.Value)
{
graphModel.VAL=Convert.ToInt32(dtRow[“VAL”]);
}
返回图形模型;
}
}

我非常感谢您的帮助,因为我正在进行大量研究,我不知道我能做些什么

将列表拆分为1000个项目列表,并将查询更改为:

" AND (RP.EMPLOYEE_ID IN (" + ids_1_1000 + ") OR RP.EMPLOYEE_ID IN (" + ids_1001_2000 + "))" +

我最喜欢Oracle的一个功能是Oracle调用接口(OCI),它允许您使用编程语言访问Oracle的一些更强大的功能。特别是在本例中,大容量插入的功能应该非常有用

如果不采用上面的方法,即在一条SQL语句中插入数千个文本,而是将这些值放入一个表中并进行连接,我认为您将:

  • 使共享池不必编译非常糟糕的SQL语句
  • 无需转义字符串或担心SQL注入
  • 有一个闪电般快速的查询,用一个连接(面包和黄油代替数据库)来代替列表中的一个巨人
  • 步骤1:创建GTT:

    create global temporary table employee_list (
      employee_id varchar2(100) not null
    ) on commit preserve rows;
    
    GTT基于一个会话,因此,即使您在多个实例中运行此代码,每个GTT都将充当每个实例的一张白板——不存在数据冲突的可能性

    步骤2:在您的代码中,创建一个
    事务
    ,以处理您需要在表中插入和选择数据作为同一事务的一部分:

    OracleTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
    
    步骤3:使用ODP.net的批量插入功能一次插入所有员工ID。我鼓励您对此进行基准测试,而不是一次插入一个。你会惊讶的。如果你有50000多个字段,那么你可能需要将这些字段分为多个块,但对于单个字段,我认为这应该足够了:

    // string[] employeesIds
    
    OracleCommand cmd = new OracleCommand("insert into employee_list values (:EMPLOYEE)",
        conn);
    cmd.Transaction = trans;
    cmd.Parameters.Add(new OracleParameter("EMPLOYEE", OracleDbType.Varchar2));
    cmd.Parameters[0].Value = employeesIds;
    cmd.ArrayBindCount = employeesIds.Length;
    cmd.ExecuteNonQuery();
    
    注意
    employeeid
    应该是一个数组

    步骤4:将SQL从in列表更改为join:

     select NVL(TO_CHAR(RR.REASON_NM_NEW), 'Total') as SERIES1, count(*) AS VAL
     from
       REQUEST R,
       REQUEST_PERSON RP,
       REQUEST_REASON RR,
       employee_list e       -- added this
     WHERE R.STATUS IN ('CREATED', 'PENDING APPROVAL', 'APPROVED BY MANAGER',
         'APPROVED', 'IN PROCESS')
     AND R.REQUEST_ID = RP.REQUEST_ID
     AND RP.REQUEST_ROLE = 'REQUESTOR'
     AND RR.REASON_ID = R.REASON_ID
     AND RP.EMPLOYEE_ID = e.employee_id  -- changed this
     group by rollup (RR.REASON_NM_NEW)
     ORDER BY count(*)  DESC
    
    下面是这一切在一起的样子:

    public List<GraphModel> countRequestCreatedByTypeDefaulPage(string[] employeesIds)
    {
        OracleTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
        OracleCommand cmd = new OracleCommand("insert into employee_list values (:EMPLOYEE)",
            conn);
        cmd.Transaction = trans;
        cmd.Parameters.Add(new OracleParameter("EMPLOYEE", OracleDbType.Varchar2));
        cmd.Parameters[0].Value = employeesIds;
        cmd.ArrayBindCount = employeesIds.Length;
        cmd.ExecuteNonQuery();
    
        String sql = "";  // code from above goes here
    
        cmd = new OracleCommand(sql, conn);
        cmd.Transaction = trans;
    
        DataTable dataTable = null;
    
        try
        {
            dataTable = Data_base_Access.executeSQL(cmd,
                ConfigurationManager.ConnectionStrings["stage"].ToString());
            return (GraphModel.convertToList(dataTable));
        }
        catch (Exception ex)
        {
            Log.writeError("Request DAO", ex);
            throw new DataAccessException("There was an error counting the open requests");
        }
        finally
        {
            trans.Rollback();
        }
    
        return dataTable;
    }
    
    public List countRequestCreatedByTypeDefaulPage(字符串[]employeesIds)
    {
    OracleTransaction=conn.BeginTransaction(IsolationLevel.ReadCommitted);
    OracleCommand cmd=新的OracleCommand(“插入员工列表值(:员工)”,
    康涅狄格州);
    cmd.Transaction=trans;
    Add(新的OracleParameter(“EMPLOYEE”,OracleDbType.Varchar2));
    cmd.Parameters[0]。Value=employeesIds;
    cmd.ArrayBindCount=employeesIds.Length;
    cmd.ExecuteNonQuery();
    字符串sql=“”;//上面的代码在这里
    cmd=新的OracleCommand(sql,conn);
    
    public List<GraphModel> countRequestCreatedByTypeDefaulPage(int year, int month, String employeeID)
        {
            int count = 0;
            int countEmployees = 0;
            Dictionary<string, int> dataChart = new Dictionary<string, int>();
            DataView dv = _employeeOverrideBO.getRelatedEmployeesRequests(year, month, employeeID);
            StringBuilder listEmployees = new StringBuilder();
    
            foreach (DataRowView rowView in dv) 
            {
                if (countEmployees == 500)
                {
                    List<GraphModel> listReturn = _requestDAO.countRequestCreatedByTypeDefaulPage(listEmployees.ToString());
    
                    foreach(GraphModel model in listReturn){
    
                        if (dataChart.ContainsKey(model.SERIES1))
                        {
                            dataChart[model.SERIES1] = dataChart[model.SERIES1] + model.VAL;
                        }
                        else
                        {
                            dataChart[model.SERIES1] = model.VAL;
                        }
    
                    }
                    listEmployees = new StringBuilder();
                    count = 0;
                    countEmployees = 0;
                }
    
                DataRow row = rowView.Row;
                String employee = row["EMPLOYEE_ID"].ToString();
                if (count > 0)
                    listEmployees.Append(",");
                listEmployees.Append("'").Append(employee).Append("'");
                count++;
    
                countEmployees++;
    
            }
    
            //Last Call
            List<GraphModel> listReturnLast = _requestDAO.countRequestCreatedByTypeDefaulPage(listEmployees.ToString());
            foreach (GraphModel model in listReturnLast) {
    
                if (dataChart.ContainsKey(model.SERIES1))
                {
                    dataChart[model.SERIES1] = dataChart[model.SERIES1] + model.VAL;
                }
                else
                {
                    dataChart[model.SERIES1] = model.VAL;
                }
    
            }
    
    
    
    
            List<GraphModel> list = new List<GraphModel>();
            foreach (KeyValuePair<string, int> entry in dataChart)
            {
                GraphModel model = new GraphModel();
                model.SERIES1 = entry.Key;
                model.VAL = entry.Value;
                list.Add(model);
            }
    
            return list;
        }