C# And SQL Query
Is it possible to run a sql query in C# -- I know it is I am currently doing this --- but I want to export the dataset returned from this query into Excel and pass the connection and SQL Code to the Excel workbook. Is it possible to do this?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

venkata kumarRank #5,034 · 314 reputationPosted Dec 24, 2012, 2:00 AM EST
check fallowing code
datatable dt=new datatable();
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlApp.Visible = true;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;
for (i = 0; i <= dt.RowCount - 1; i++)
{
for (j = 0; j <= dt.ColumnCount - 1; j++)
{
dt cell = dt[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
//xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
richard smithRank #8,268 · 225 reputationPosted Dec 25, 2012, 5:42 AM EST
I am working on a solution...please bear with me, and Merry Christmas.