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?
richard smithPosted Dec 25, 2012, 10:42 AM
I am working on a solution...please bear with me, and Merry Christmas.
venkata kumarPosted Dec 24, 2012, 7:00 AM
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);