Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is there way to execute a SQL Statement and export the result set to Excel without using a stored procedure from a C# application on a windows platform?

Posted on 2012-08-29
2
Medium Priority
?
1,112 Views
Last Modified: 2012-08-30
I am writing a C# application under a windows platform using VS2005.
In the following routine, is there a way to revise this routine to execute a SQL statement instead of processing a STORED PROCEDURE? I ultimately export a result set to an Excel file and the records don't come out in the correct sequence to the Excel file even from a SQL Server table with an ORDER BY clause unless I generate the SQL Statement without having it stored into a table.

private void ReportToExcel(string reportyr, string cmdText, string ftnote, string flename)
        {            
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            string connetionString = null;
            SqlConnection connection;
            SqlDataAdapter adapter;
            SqlCommand command = new SqlCommand();
            SqlParameter param;            

            //int i = 0;
            connetionString = MyGlobals.connectLiteral;
            connection = new SqlConnection(connetionString);

            connection.Open();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            //command.CommandText = "procFlACSExclAAA";
            command.CommandText = cmdText;

            if (reportyr != "bypass")
            {
                param = new SqlParameter("@RptYear", reportyr);
                param.Direction = ParameterDirection.Input;
                param.DbType = DbType.String;
                command.Parameters.Add(param);
            }
            else
            {
                reportyr = "";
            }

            DataSet ds = new DataSet();
            adapter = new SqlDataAdapter(command);
            adapter.Fill(ds);

            string MyProjectDir = DirProject();
            string footnote = ftnote;

            DataTable myDataTable = ds.Tables[0];
            string ExportedFile = MyProjectDir + flename + "_" + reportyr + "_" + DateTime.Now.ToString("MMddhhmmss") + ".XLS";
            ExcelExport(myDataTable, ExportedFile);
            ExcelFormat(ExportedFile, footnote);
            Cursor.Current = Cursors.Default;
        }
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38348857
Change

command.CommandType = CommandType.StoredProcedure;

to

command.CommandType = CommandType.Text;

I assume cmdText holds the SQL query you would like to execute.
0
 
LVL 9

Expert Comment

by:keyu
ID: 38350167
You can do it multiple way

1) using SSMS Tool - > here you can directly get export to excel option

2) using commandline BCP option.
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question