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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

632 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