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

Hola amigos estoy programando una aplicacion que importe los datos de un archivo excel en una tabla de la base de datos, inserta bien los datos de la siguiente forma.

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.                 //Aca se daña el archivo excel
  37.                 OleDbCommand command = new OleDbCommand("select * from [hoja1$]", connection);
  38.                 connection.Open();
  39.                 // Create DbDataReader to Data Worksheet
  40.                 DbDataReader dr = command.ExecuteReader();
  41.  
  42.                 // SQL Server Connection String
  43.                 string sqlConnectionString = @"Data Source=.;Initial Catalog=excel;Integrated Security=True";
  44.  
  45.                 // Bulk Copy to SQL Server
  46.                 SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnectionString);
  47.  
  48.                 //Give your Destination table name
  49.                 sqlBulk.DestinationTableName = "excel01";
  50.                 sqlBulk.WriteToServer(dr);
  51.                 Label1.Text = "Exito";
  52.                 dr.Close();
  53.                 connection.Close();                
  54.             }
  55.             catch (Exception ex)
  56.             {
  57.                 Label1.Text = ex.Message;
  58.             }
  59.         }
  60.         else
  61.         {
  62.             Label1.Text = "No se pudo cargar el archivo excel";
  63.         }
  64.     }

y se insertan los datos normalmente pero si quiero agregar una comprobacion de que si los datos del excel son correctos (ej. que una fila tenga letras donde solo deben ir numeros)

asi:

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.                 //Aca se daña el archivo excel
  37.                 OleDbCommand command = new OleDbCommand("select * from [hoja1$]", connection);
  38.                 connection.Open();
  39.                 // Create DbDataReader to Data Worksheet
  40.                 DbDataReader dr = command.ExecuteReader();
  41.  
  42.                
  43.  
  44.                 //////////////////////////////////////////////////////////////////////////////////////////
  45.                 int verif_correctas = 0;
  46.                 int verif_total = 0;
  47.  
  48.                 if (dr.HasRows)
  49.                 {
  50.                     while (dr.Read())
  51.                     {
  52.                         if (!(dr["marks"] is DBNull))
  53.                         {
  54.                             verif_correctas++;
  55.                         }
  56.  
  57.                         if (!(dr["id"] is DBNull))
  58.                         {
  59.                             verif_total++;
  60.                         }
  61.                     }
  62.                 }
  63.                 Label2.Text = verif_correctas.ToString();
  64.                 Label3.Text = verif_total.ToString();
  65.  
  66.                 if (verif_correctas == verif_total)
  67.                 {
  68.                     // SQL Server Connection String
  69.                    string sqlConnectionString = @"Data Source=.;Initial Catalog=excel;Integrated Security=True";
  70.  
  71.                    // Bulk Copy to SQL Server
  72.                    SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnectionString);
  73.  
  74.                    //Give your Destination table name
  75.                    sqlBulk.DestinationTableName = "excel01";
  76.                    sqlBulk.WriteToServer(dr);
  77.                    Label1.Text = "Exito";
  78.                    dr.Close();
  79.                    connection.Close();
  80.                 }
  81.                 else
  82.                 {
  83.                     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";
  84.                 }
  85.                 //////////////////////////////////////////////////////////////////////////////////////////
  86.                
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 Label1.Text = ex.Message;
  91.             }
  92.         }
  93.         else
  94.         {
  95.             Label1.Text = "No se pudo cargar el archivo excel";
  96.         }
  97.     }

ya no guarda los datos en la base de datos, la verdad no le veo tanta diferencia intente varios cambios pero sigue igual sea por el uso del if (dr.HasRows), while (dr.Read()) y if (!(dr["marks"] is DBNull))???

saludos