Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/03/2014, 14:22
Avatar de gokufast
gokufast
 
Fecha de Ingreso: abril-2007
Mensajes: 540
Antigüedad: 17 años
Puntos: 3
Respuesta: no inserta los datos del excel en la BD

bueno ya di con la solucion era tema de ordenar un poco.

Código ASP:
Ver original
  1. protected void btnUpload_Click(object sender, EventArgs e)
  2.     {
  3.         Boolean fileOk = false;
  4.         string path = string.Concat(Server.MapPath("~/Uploaded Folder/" + FileUpload1.FileName));
  5.  
  6.         if (FileUpload1.HasFile)
  7.         {
  8.             int fileSize = FileUpload1.PostedFile.ContentLength;
  9.             if (fileSize < 420000000)
  10.             {
  11.                 string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
  12.                 string allowwExtension4 = ".xls";
  13.                 string allowwExtension5 = ".xlsx";
  14.                 if (FileExtension == allowwExtension4 || FileExtension == allowwExtension5)
  15.                 {
  16.                     fileOk = true;
  17.                 }
  18.  
  19.             }
  20.             else
  21.             {
  22.                 fileOk = false;
  23.             }
  24.  
  25.         }
  26.  
  27.         if (fileOk)
  28.         {
  29.             try
  30.             {
  31.                 FileUpload1.PostedFile.SaveAs(path);
  32.                 // Connection String to Excel Workbook
  33.                 string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
  34.                 OleDbConnection connection = new OleDbConnection();
  35.                 connection.ConnectionString = excelConnectionString;
  36.                 OleDbCommand command = new OleDbCommand("select * from [hoja1$]", connection);
  37.                 connection.Open();
  38.  
  39.                 //////////////////////////////////////////////////////////////////////////////////////////
  40.                 int verif_correctas = 0;
  41.                 int verif_total = 0;
  42.                 DbDataReader dr_verif = command.ExecuteReader();
  43.                 if (dr_verif.HasRows)
  44.                 {
  45.                     while (dr_verif.Read())
  46.                     {
  47.                         if (!(dr_verif["marks"] is DBNull))
  48.                         {
  49.                             verif_correctas++;
  50.                         }
  51.  
  52.                         if (!(dr_verif["id"] is DBNull))
  53.                         {
  54.                             verif_total++;
  55.                         }
  56.                     }
  57.                 }
  58.                 dr_verif.Close();
  59.                 Label2.Text = verif_correctas.ToString();
  60.                 Label3.Text = verif_total.ToString();
  61.  
  62.                 if (verif_correctas == verif_total)
  63.                 {
  64.                     // Create DbDataReader to Data Worksheet
  65.                     DbDataReader dr = command.ExecuteReader();
  66.                    
  67.                     // SQL Server Connection String
  68.                     string sqlConnectionString = @"Data Source=.;Initial Catalog=excel;Integrated Security=True";
  69.  
  70.                     // Bulk Copy to SQL Server
  71.                     SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnectionString);
  72.  
  73.                     //Give your Destination table name
  74.                     sqlBulk.DestinationTableName = "excel01";
  75.                     sqlBulk.WriteToServer(dr);
  76.                     Label1.Text = "Exito";
  77.                     dr.Close();
  78.                     connection.Close();
  79.                 }
  80.                 else
  81.                 {
  82.                     Label1.Text = "Algun dato de la columna 'Marks' no es un numero, debido a esto no se pudo agregar los datos del excel en la base de datos";
  83.                 }
  84.                 //////////////////////////////////////////////////////////////////////////////////////////
  85.                
  86.             }
  87.             catch (Exception ex)
  88.             {
  89.                 Label1.Text = ex.Message;
  90.             }
  91.         }
  92.         else
  93.         {
  94.             Label1.Text = "No se pudo cargar el archivo excel";
  95.         }
  96.     }