Go Premium for a chance to win a PS4. Enter to Win

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,114 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

783 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