Foros del Web » Programación para mayores de 30 ;) » .NET »

no inserta los datos del excel en la BD

Estas en el tema de no inserta los datos del excel en la BD en el foro de .NET en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 13/03/2014, 08:55
Avatar de gokufast  
Fecha de Ingreso: abril-2007
Mensajes: 540
Antigüedad: 16 años, 11 meses
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
  #2 (permalink)  
Antiguo 13/03/2014, 14:22
Avatar de gokufast  
Fecha de Ingreso: abril-2007
Mensajes: 540
Antigüedad: 16 años, 11 meses
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.     }

Etiquetas: bd, excel, inserta, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:42.