Cannot export big data from oracle to excel file using C #

I have a problem with fetching big data from an oracle table in C # and I could not find a solution myself.

For this task, I wrote C # code that loaded data from the oracle procedure, which returns the cursor, in the excel file for the first time. But when I tried to load a large table (about 20 columns and 90,000 rows), it just didn't work. Script does not crash with error, but data is not inserted into excel file.

I tried loading 10,000 rows and then saving the results, but then again, only 30,000 rows were inserted. I tracked the counter in a loop, it is fixed and reaches 90,000, and ExecuteNonQuery()always returns 10,000. But when I open the excel file, there are only 30,000 lines.

Could you help me catch the error, or maybe someone met the same problem, and can advise me what to do or what to read.

Thanks for the help!

I did not write a connection string, but I think it is correct, because the script works correctly with a small datatable.

public static void Main()
{
    string datetime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
    try
    {
        OleDbConnection Excel_OLE_Con = new OleDbConnection();
        OleDbCommand Excel_OLE_Cmd = new OleDbCommand();

        string qwe_constr = "connection string";
        OracleConnection myADONETConnection = new OracleConnection(qwe_constr);

        string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + "E:\\qaz\\15.07.2016\\qwe" +
                            ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";

        File.Delete("E:\\qaz\\15.07.2016\\qwe.xlsx");

        //fill datatable with data for insert
        myADONETConnection.Open();
        OracleCommand cmd_proc = new OracleCommand();
        cmd_proc.Connection = myADONETConnection;
        cmd_proc.CommandType = System.Data.CommandType.StoredProcedure;
        cmd_proc.CommandText = "procedure_name";
        cmd_proc.Parameters.Add("p_show_del", OracleDbType.Int16).Value = 0;
        cmd_proc.Parameters.Add("p_type", OracleDbType.Varchar2, 3).Value = "INV";
        cmd_proc.Parameters.Add("p_errno", OracleDbType.Int16).Value = 157;
        cmd_proc.Parameters.Add("outcur", OracleDbType.RefCursor).Direction = ParameterDirection.Output;

        DataTable dt_with_data = new DataTable();
        dt_with_data.Load(cmd_proc.ExecuteReader());
        myADONETConnection.Close();

        //string with column headers
        string TableColumns = "";
        foreach (DataColumn column in dt_with_data.Columns)
        {
            TableColumns += column + "],[";
        }

        // Replace most right comma from Columnlist
        TableColumns = ("[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
        TableColumns = TableColumns.Remove(TableColumns.Length - 2);

        //Use OLE DB Connection and Create Excel Sheet
        Excel_OLE_Con.ConnectionString = connstring;
        Excel_OLE_Con.Open();
        Excel_OLE_Cmd.Connection = Excel_OLE_Con;
        Excel_OLE_Cmd.CommandText = "Create table [sheet1] (" + TableColumns + ")";
        Excel_OLE_Cmd.ExecuteNonQuery();
        Excel_OLE_Con.Close();

        //Write Data to Excel Sheet from DataTable dynamically
        //string with command
        Excel_OLE_Con.Open();
        String sqlCommandInsert = "";
        String sqlCommandValue = "";
        foreach (DataColumn dataColumn in dt_with_data.Columns)
        {
            sqlCommandValue += dataColumn + "],[";
        }

        sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
        sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
        sqlCommandInsert = "INSERT into [sheet1] (" + sqlCommandValue + ") VALUES(";

        int columnCount = dt_with_data.Columns.Count;

        int i_qaz = 0;
        foreach (DataRow row in dt_with_data.Rows)
        {
            i_qaz++;
            Console.WriteLine(i_qaz.ToString());
            string columnvalues = "";
            for (int i = 0; i < columnCount; i++)
            {
                int index = dt_with_data.Rows.IndexOf(row);
                columnvalues += "'" + dt_with_data.Rows[index].ItemArray[i].ToString().Replace("'", "''") + "',";
            }
            columnvalues = columnvalues.TrimEnd(',');
            var command = sqlCommandInsert + columnvalues + ")";
            Excel_OLE_Cmd.CommandText = command;
            Excel_OLE_Cmd.ExecuteNonQuery();
        }
    }
    catch (Exception exception)
    {
        // Create Log File for Errors
        using (StreamWriter sw = File.CreateText("E:\\qaz\\15.07.2016\\qwe_" + datetime + ".log"))
        {
            sw.WriteLine(exception.ToString());
        }

    }
}

PS: The same question in Russian.

+4
source share

Source: https://habr.com/ru/post/1654128/


All Articles