Showing posts with label Transferring Data From Excel Sheet to Access. Show all posts
Showing posts with label Transferring Data From Excel Sheet to Access. Show all posts

Thursday, May 21, 2009

Transferring Data From Excel Sheet to Access

protected void Page_Load(object sender, EventArgs e)

{

//Calling Cotacts.mdb(Access Database) from the App_Data Folder

string Access = Server.MapPath("App_Data/contact.mdb");

//Calling Book.xls(Excel Sheet) from the Root Folder

string Excel = Server.MapPath("Book.xls");

//Connection string For Excel Sheet

string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;";

using (OleDbConnection conn = new OleDbConnection(connect))

{

using (OleDbCommand cmd = new OleDbCommand())

{

try

{

cmd.Connection = conn;

//Inserting Data into the Access Database fromthe Excel Sheet where Excel Sheet Name is Sheet1

cmd.CommandText = "INSERT INTO [MS Access;Database=" + Access + "].[Person] SELECT * FROM [Sheet1$]";

conn.Open();

cmd.ExecuteNonQuery();

}

catch (Exception ex)

{

Response.Write("Your error is " + ex.Message);

}

}

}

}