USING System;
USING System.Collections.Generic;
USING System.Linq;
USING System.Web;
USING System.Web.UI;
USING System.Web.UI.WebControls;
USING System.DATA.SqlClient;
USING System.DATA;
USING System.Configuration;
USING System.Collections;
USING System.Web.Security;
USING System.Web.UI.WebControls.WebParts;
USING System.Web.UI.HtmlControls;
USING System.DATA.Common;
USING System.DATA.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string excelConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;""", Server.MapPath("database.xls"));
USING (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("Select ID,Data FROM [Sheet1$]", connection);
connection.Open();
USING (DbDataReader dr = command.ExecuteReader())
{
string sqlConnectionString = "Data Source=;Initial Catalog=Test;Integrated Security=True";
USING (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}
}
}