4
Answers

how to store the data table content to sql data base

Dinesh Santhalingam

Dinesh Santhalingam

7y
325
1
I have a excel file.I Store the data from excel to a data table .Now i want to store the data table content to sql table .how to store the data table to data base ?
 
 
 
Am storing the data table content from excel by using below procedure.
 
 
 
 
string filePath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(filePath);
//string filePath=@"C:\Users\350154\Desktop\Sample.xlsx";
try
{
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
//Read the first Sheet from Excel file.
IXLWorksheet workSheet = workBook.Worksheet(1);
//Create a new DataTable.
DataTable dt = new DataTable();
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
//Add rows to DataTable.
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
 

Answers (4)