Suponemos que Excel y DataTable tienen las mismas columnas y en el mismo orden.
string strConnnectionOle = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=" + rutaexcel + ";" + @"Extended Properties=" + '"' + "Excel 8.0;HDR=NO" + '"';
foreach (DataRow row in dtSQL.Rows)
{
row.SetAdded();
}
string sql = "SELECT * FROM [RANGO_DE_LA_EXCEL]";
OleDbDataAdapter da = new OleDbDataAdapter(sql, cnn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
cb.QuotePrefix = "[";
cb.QuoteSuffix = "]";
da.InsertCommand = cb.GetInsertCommand();
// Devolverá el número de registros afectados.
int n = da.Update(dtSQL);
// Aceptamos los cambios
dtSQL.AcceptChanges();
No hay comentarios:
Publicar un comentario