ODBC select huge data rows

Dear Experts,
 I'm using ODBC in C# to select data (SQL statement) from a database then to insert these data into CSV file.

How do I improve my code when the SQL result is huge data?

        public Void executeQuery(string query)
        {

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            OdbcCommand dc = new OdbcCommand(query, conn);
            OdbcDataReader reader = dc.ExecuteReader();

            schematab = reader.GetSchemaTable();

            DataTable dt = new DataTable();
            dt.Load(reader);

            reader.Close();

            Insert dt to CSV file

        }

Open in new window



Thanks
Jamil MuammarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Maybe tell SQL to output the file ?
1
Máté FarkasDatabase Developer and AdministratorCommented:
Your possibilities:
  • Export the result of your select directly to CSV file. Most of SQL Server can do this (MySQL, MSSQL, Oracle etc.)
  • Don't serialize the result of your query into a DataSet/DataTable. Use DataReader to run a loop and export rows one-by-one.
0
Jamil MuammarAuthor Commented:
Hello Máté Farkas,

 Can you please show me the second option, how to code it in my attached code ?

Thanks
0
Máté FarkasDatabase Developer and AdministratorCommented:
        public void executeQuery(string query)
        {
            System.IO.TextWriter CSVFile = new System.IO.File.CreateText("C:\ExportedData.csv");


            if (conn.State == ConnectionState.Closed) conn.Open();

            OdbcCommand dc = new OdbcCommand(query, conn);
            OdbcDataReader reader = dc.ExecuteReader();


            while (reader.Read())
            {
                
                for(int i = 0; i < reader.FieldCount; i++)
                {
                    CSVFile.Write(reader.Item(i).ToString());
                    CSVFile.Write(';'); // delimiter
                }
                CSVFile.WriteLine();
            }

            reader.Close();
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.