public void importExcelData(ListItemCollection dbColumns, ListItemCollection excelColumns, String tableName) { //set up and make connection //connection string specified in the web.config ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["Conn"]; SqlConnection conn = new SqlConnection(connString.ConnectionString); conn.Open(); using (SqlBulkCopy s = new SqlBulkCopy(conn)) { try { s.DestinationTableName = tableName; s.NotifyAfter = 10000; for (int i = 0; i < dbColumns.Count; i++) { s.ColumnMappings.Add(excelColumns[i].Value.ToString(), dbColumns[i].Value.ToString()); } s.WriteToServer(data); s.Close(); } catch(Exception ex) { //Error handling } } conn.Close(); }Ref: http://blog.shuasolutions.com/2008/10/mapping-and-importing-excel-files-to-a-database-using-sqlbulkcopy/
Translate
Tuesday, 16 June 2015
Mapping and Importing Excel Files to a Database using SqlBulkCopy
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment