protected void cmdImport_Click(object sender, EventArgs e) { string sSourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\AgentList.xls; Extended Properties=""Excel 8.0;HDR=YES;"""; string sDestConstr = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString; OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr); using (sSourceConnection) { string sql = string.Format("Select [MSISDN],[Name],[Company],[Status] FROM [{0}]", "Sheet1$"); OleDbCommand command = new OleDbCommand(sql, sSourceConnection); sSourceConnection.Open(); using (OleDbDataReader dr = command.ExecuteReader()) { using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sDestConstr)) { bulkCopy.DestinationTableName = "tblAgent"; //You can mannualy set the column mapping by the following way. //bulkCopy.ColumnMappings.Add("MSISDN", "MSISDN"); bulkCopy.WriteToServer(dr); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)