Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to import data from an excel to sql database in asp.net c#
I try this but have an error.

"The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.


I try the link :
http://www.aspdotnet-suresh.com/2010/09/import-data-from-excel-to-sql-database.html[^]

error in the line:
C#
dReader = cmd.ExecuteReader();

please any one help me.
Posted
Updated 22-Aug-12 19:49pm
v3

 
Share this answer
 
Comments
Shibiny 23-Aug-12 2:22am    
I USE THIS CODE LIKE THIS BUT ERROR......

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)

{










String fileName = fileuploadExcel.ResolveClientUrl(fileuploadExcel.PostedFile.FileName);
int count = 0;
DataClassesDataContext conLinq = new DataClassesDataContext("Data Source=WEB;Initial Catalog=ExcelDB;Integrated Security=true");
try
{
DataTable dtExcel = new DataTable();
string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Sheet1$]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
try
{
count += conLinq.ExecuteCommand("insert into Excel_table values(" + dtExcel.Rows[i][0] + "," + dtExcel.Rows[i][1] + ",'" + dtExcel.Rows[i][2] + "')");
}
catch (Exception ex)
{
continue;
}
}
if (count == dtExcel.Rows.Count)
{
//<--Success Message-->
}
else
{
//<--Failure Message-->
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
conLinq.Dispose();
}







string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + +"';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection con = new OleDbConnection(SourceConstr);
string query = "Select * from [Sheet1$]";
OleDbDataAdapter data = new OleDbDataAdapter(query, con);
data.Fill(dtExcel);

}

}
Prasad_Kulkarni 23-Aug-12 2:24am    
Where is error line??
 
Share this answer
 
Comments
Shibiny 23-Aug-12 1:59am    
how to connect with databse. in sql server.

and i want to browse excel file to import
Vani Kulkarni 23-Aug-12 2:20am    
Did not understand your question. How to connect with database means what? Please elaborate.
Shibiny 23-Aug-12 2:24am    
i want to connect to sql server databse using connection string or any method

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900