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
1,099 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now