Foros del Web » Programación para mayores de 30 ;) » Programación General » Visual Basic clásico »

[SOLUCIONADO] Problema en Insert

Estas en el tema de Problema en Insert en el foro de Visual Basic clásico en Foros del Web. Buen Día, les escribo porque la verdad he buscado y he desarmado el query como 20 veces y la verdad no entiendo el problema que ...
  #1 (permalink)  
Antiguo 05/04/2013, 10:35
Avatar de yamiblancoc  
Fecha de Ingreso: agosto-2011
Mensajes: 103
Antigüedad: 12 años, 8 meses
Puntos: 4
Sonrisa Problema en Insert

Buen Día, les escribo porque la verdad he buscado y he desarmado el query como 20 veces y la verdad no entiendo el problema que tengo.

Tengo el siguiente codigo:
donde la primera parte se labre un libro en excel y se lee la hoja, luego realizo la consulta a toda la hoja y luego realizo el insert para cada columna en SQL
los .replaces es porque hay celdas que estan en blanco y las reemplazo por un guion (-) para que no me de error el insert.

Que es lo que me pasa... la hoja tiene 15736 filas el insert funciona perfecto, es decir inserta todas las filas del excel a la base de datos en SQL peo que pasa la columna Variable_Values (columna 32) tiene valor toda la columna y no me esta tomando eso valores y en vez de colocarme el valor que sale en la hoja de excel lo deja en blanco, ¿por que me pasa esto? Trabajo con SQL y Visual Studio

Si alguien puede ayudarme GRACIAS!!!

Código vb:
Ver original
  1. conexion.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & BuscarArchivo.FileName & " ; Extended Properties= Excel 8.0;"
  2.                         conexion.Open()
  3.                         Dim hoja As String = Strings.Left(BuscarArchivo.SafeFileName.ToString, 31)
  4.                         comando.CommandText = "SELECT * FROM [" & hoja & "$]"
  5.                         comando.Connection = conexion
  6.                         adaptador.SelectCommand = comando
  7.                         adaptador.Fill(ds)
  8.                         Form1.ProgressBar1.Minimum = 0
  9.                         Form1.ProgressBar1.Maximum = ds.Tables(0).Rows.Count - 1
  10.                         For Each f As DataRow In ds.Tables(0).Rows
  11.                             If filas < ds.Tables(0).Rows.Count - 1 Then
  12.  
  13.                                 Comm.CommandText = "INSERT INTO [NSO].[dbo].[Audited_Facts_regular]([Fecha_Carga],[CountryID],[ContryName],[StoreID],[NSO_StoreID],[StoreName] " & _
  14.                                                     " ,[StoreAddres] ,[StoreStatud],[StoreComments],[StoreStarDateTime],[StoreEndDateTime],[StoreClosing]" & _
  15.                                                     " ,[PlannetAudit],[AuditInstructionID],[SyneryMode],[IMDB_ProductID],[NSO_ProductID],[ProductName]" & _
  16.                                                     " ,[ProductCategory],[Barcode],[ProductLevel],[ProductComment],[LocationID],[LocationName],[LocationComments]" & _
  17.                                                     " ,[ExhibitionID],[ExhibitionName],[ExhibitionLevel],[ExhibitionPossition],[ExhibitionComment],[VariableID]" & _
  18.                                                     " ,[VariableName],[VariableAtribute],[VariableValue],[VariableComments],[FactType],[ReUser])" & _
  19.                                 "VALUES('" & String.Format("{0:dd-MM-yyyy}", DateTime.Now.Date) & "','" & f(0).ToString().Replace(" ", "-") & "','" & f(1).ToString().Replace(" ", "-") & "','" & f(2).ToString() & "','" & f(3).ToString() & "','" & f(4).ToString().Replace("'", "´") _
  20.                                         & "  ','" & f(5).ToString().Replace(" ", "-") & "','" & f(6).ToString().Replace(" ", "-") & "',' " & f(7).ToString().Replace(" ", "-") & "','" & f(8).ToString().Replace(" ", "-") _
  21.                                         & "  ','" & f(9).ToString().Replace(" ", "-") & "',' " & f(10).ToString().Replace(" ", "-") & "','" & f(11).ToString().Replace(" ", "-") & "','" & f(12).ToString().Replace(" ", "-") _
  22.                                         & "  ','" & f(13).ToString().Replace(" ", "-") & "','" & f(14).ToString().Replace(" ", "-") & "','" & f(15).ToString().Replace(" ", "-") & "','" & f(16).ToString().Replace(" ", "-") _
  23.                                         & "  ','" & f(17).ToString().Replace(" ", "-") & "','" & f(18).ToString().Replace(" ", "-") & "','" & f(19).ToString().Replace(" ", "-") & "',' " & f(20).ToString().Replace(" ", "-") _
  24.                                         & "  ','" & f(21).ToString().Replace(" ", "-") & "','" & f(22).ToString().Replace(" ", "-") & "','" & f(23).ToString().Replace(" ", "-") & "','" & f(24).ToString().Replace(" ", "-") _
  25.                                         & "  ','" & f(25).ToString().Replace(" ", "-") & "','" & f(26).ToString().Replace(" ", "-") & "','" & f(27).ToString().Replace(" ", "-") & "',' " & f(28).ToString().Replace(" ", "-") _
  26.                                         & "  ','" & f(29).ToString().Replace(" ", "-") & "','" & f(30).ToString().Replace(" ", "-") & "','" & f(31).ToString().Replace(" ", "'0'") & "','" & f(32).ToString().Replace(" ", "'0'") _
  27.                                         & "  ','" & f(33).ToString().Replace(" ", "-") & "','" & f(34).ToString().Replace(" ", "-") & "','NULL')"
  28.  
  29.                                 Comm.Connection = Cone
  30.                                 inserts = inserts + Comm.ExecuteNonQuery()
  31.                                 filas = filas + 1
  32.                                 Form1.ProgressBar1.Value = Form1.ProgressBar1.Value + 1
  33.                                 Application.DoEvents()
  34.                             End If
  35.                         Next
  #2 (permalink)  
Antiguo 07/04/2013, 15:58
Usuario no validado
 
Fecha de Ingreso: junio-2010
Ubicación: Guatemala
Mensajes: 196
Antigüedad: 13 años, 10 meses
Puntos: 25
Respuesta: Problema en Insert

Saludos, has intentado solo realizar el insert (en una tabla temporal) del contenido de la columna que te ha dado problemas?
Otra cosa que te puede estar afectando, trata de usar parámetros en lugar de ingresarlos directamente.
http://stackoverflow.com/a/542542
  #3 (permalink)  
Antiguo 08/04/2013, 10:11
Avatar de yamiblancoc  
Fecha de Ingreso: agosto-2011
Mensajes: 103
Antigüedad: 12 años, 8 meses
Puntos: 4
Respuesta: Problema en Insert

Cita:
Iniciado por JonathanB Ver Mensaje
Saludos, has intentado solo realizar el insert (en una tabla temporal) del contenido de la columna que te ha dado problemas?
Otra cosa que te puede estar afectando, trata de usar parámetros en lugar de ingresarlos directamente.
http://stackoverflow.com/a/542542
Cuando lo realizo con parametros

Código vb:
Ver original
  1. For Each f As DataRow In ds.Tables(0).Rows
  2.                             If filas < ds.Tables(0).Rows.Count - 1 Then
  3.                                 Comm.CommandText = "INSERT INTO [NSO].[dbo].[Audited_Facts_regular]([Fecha_Carga],[CountryID],[ContryName],[StoreID],[NSO_StoreID],[StoreName] " & _
  4.                                 " ,[StoreAddres] ,[StoreStatud],[StoreComments],[StoreStarDateTime],[StoreEndDateTime],[StoreClosing]" & _
  5.                                 " ,[PlannetAudit],[AuditInstructionID],[SyneryMode],[IMDB_ProductID],[NSO_ProductID],[ProductName]" & _
  6.                                 " ,[ProductCategory],[Barcode],[ProductLevel],[ProductComment],[LocationID],[LocationName],[LocationComments]" & _
  7.                                 " ,[ExhibitionID],[ExhibitionName],[ExhibitionLevel],[ExhibitionPossition],[ExhibitionComment],[VariableID]" & _
  8.                                 " ,[VariableName],[VariableAtribute],[VariableValue],[VariableComments],[FactType],[ReUser])" & _
  9.                                 " VALUES(" & String.Format("{0:dd-MM-yyyy}", DateTime.Now.Date) & ",@CountryID,@ContryName,@StoreID,@NSO_StoreID,@StoreName " & _
  10.                                 " ,@StoreAddres ,@StoreStatud,@StoreComments,@StoreStarDateTime,@StoreEndDateTime,@StoreClosing" & _
  11.                                 " ,@PlannetAudit,@AuditInstructionID,@SyneryMode,@IMDB_ProductID,@NSO_ProductID,@ProductName" & _
  12.                                 " ,@ProductCategory,@Barcode,@ProductLevel,@ProductComment,@LocationID,@LocationName,@LocationComments" & _
  13.                                 " ,@ExhibitionID,@ExhibitionName,@ExhibitionLevel,@ExhibitionPossition,@ExhibitionComment,@VariableID" & _
  14.                                 " ,@VariableName,@VariableAtribute,@VariableValue,@VariableComments,@FactType,@ReUser)"
  15.                                 'Comm.Parameters.AddWithValue("@Fecha12", String.Format("{0:dd-MM-yyyy}", DateTime.Now.Date))
  16.                                Comm.Parameters.AddWithValue("@CountryID", f(0).ToString())
  17.                                 Comm.Parameters.AddWithValue("@ContryName", f(1).ToString())
  18.                                 Comm.Parameters.AddWithValue("@StoreID", f(2).ToString())
  19.                                 Comm.Parameters.AddWithValue("@NSO_StoreID", f(3).ToString())
  20.                                 Comm.Parameters.AddWithValue("@StoreName", f(4).ToString())
  21.                                 Comm.Parameters.AddWithValue("@StoreAddres", f(5).ToString())
  22.                                 Comm.Parameters.AddWithValue("@StoreStatud", f(6).ToString())
  23.                                 Comm.Parameters.AddWithValue("@StoreComments", f(7).ToString())
  24.                                 Comm.Parameters.AddWithValue("@StoreStarDateTime", f(8).ToString())
  25.                                 Comm.Parameters.AddWithValue("@StoreEndDateTime", f(9).ToString())
  26.                                 Comm.Parameters.AddWithValue("@StoreClosing", f(10).ToString())
  27.                                 Comm.Parameters.AddWithValue("@PlannetAudit", f(11).ToString())
  28.                                 Comm.Parameters.AddWithValue("@AuditInstructionID", f(12).ToString())
  29.                                 Comm.Parameters.AddWithValue("@SyneryMode", f(13).ToString())
  30.                                 Comm.Parameters.AddWithValue("@IMDB_ProductID", f(14).ToString())
  31.                                 Comm.Parameters.AddWithValue("@NSO_ProductID", f(15).ToString())
  32.                                 Comm.Parameters.AddWithValue("@ProductName", f(16).ToString())
  33.                                 Comm.Parameters.AddWithValue("@ProductCategory", f(17).ToString())
  34.                                 Comm.Parameters.AddWithValue("@Barcode", f(18).ToString())
  35.                                 Comm.Parameters.AddWithValue("@ProductLevel", f(19).ToString())
  36.                                 Comm.Parameters.AddWithValue("@ProductComment", f(20).ToString())
  37.                                 Comm.Parameters.AddWithValue("@LocationID", f(21).ToString())
  38.                                 Comm.Parameters.AddWithValue("@LocationName", f(22).ToString())
  39.                                 Comm.Parameters.AddWithValue("@LocationComments", f(23).ToString())
  40.                                 Comm.Parameters.AddWithValue("@ExhibitionID", f(24).ToString())
  41.                                 Comm.Parameters.AddWithValue("@ExhibitionName", f(26).ToString())
  42.                                 Comm.Parameters.AddWithValue("@ExhibitionLevel", f(26).ToString())
  43.                                 Comm.Parameters.AddWithValue("@ExhibitionPossition", f(27).ToString())
  44.                                 Comm.Parameters.AddWithValue("@ExhibitionComment", f(28).ToString())
  45.                                 Comm.Parameters.AddWithValue("@VariableID", f(29).ToString())
  46.                                 Comm.Parameters.AddWithValue("@VariableName", f(30).ToString())
  47.                                 Comm.Parameters.AddWithValue("@VariableAtribute", f(31).ToString())
  48.                                 Comm.Parameters.AddWithValue("@VariableValue", f(32).ToString())
  49.                                 Comm.Parameters.AddWithValue("@VariableComments", f(33).ToString())
  50.                                 Comm.Parameters.AddWithValue("@FactType", f(34).ToString())
  51.                                 Comm.Parameters.AddWithValue("@ReUser", f(35).ToString())
  52.                                 Comm.Connection = Cone
  53.                             next


inserta la primera fila luego me aparece un cuadro de texto que me dice que la variable ya esta declarda que debe ser unica por cada lote o procedimiento
  #4 (permalink)  
Antiguo 08/04/2013, 13:00
Avatar de yamiblancoc  
Fecha de Ingreso: agosto-2011
Mensajes: 103
Antigüedad: 12 años, 8 meses
Puntos: 4
Respuesta: Problema en Insert

ya solucione el problema de la variable repetida el query debe estar dentro de un using... pero igual el problema sigue con la columna 32... tengo espacios en blanco cuando originalmente no es asi
  #5 (permalink)  
Antiguo 12/04/2013, 10:30
Avatar de yamiblancoc  
Fecha de Ingreso: agosto-2011
Mensajes: 103
Antigüedad: 12 años, 8 meses
Puntos: 4
De acuerdo Respuesta: Problema en Insert

para quienes tienen problema con esto... lo que me paso fue que la fila es de tipo entero y lo necesitaba de tipo texto... pero como el archivo viene tipo .csv

lei el archivo de texto y luego lo almacene aqui les dejo como hacerlo

Código vb:
Ver original
  1. Dim fName As String = ""
  2.         BuscarArchivo.InitialDirectory = "Desktop"
  3.         BuscarArchivo.Filter = "CSV files (*.csv)|*.CSV"
  4.         BuscarArchivo.FilterIndex = 2
  5.         BuscarArchivo.RestoreDirectory = True
  6.                 If BuscarArchivo.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  7.                 fName = BuscarArchivo.FileName
  8.                 '**** IMPORTAR DATOS CSV A DGV ****
  9.                Dim TextLine As String = ""
  10.                 Dim SplitLine() As String
  11.                 If System.IO.File.Exists(fName) = True Then
  12.                     Dim objReader As New System.IO.StreamReader(fName)
  13.                     Do While objReader.Peek() <> -1
  14.                         TextLine = objReader.ReadLine()
  15.                         SplitLine = Split(TextLine, ",")
  16.                         Me.DGVCarga.Rows.Add(SplitLine)
  17.                     Loop
  18.  
  19.                     '****  GUARDAR DATOS EN SQL  *******
  20.                    AbrirConexion()
  21.                  
  22.                     For Each f As DataGridViewRow In DGVCarga.Rows
  23.                         If filas < DGVCarga.Rows.Count - 1 Then
  24.                             Using Comm As New SqlCommand(sql, Cone)
  25.                                 Comm.CommandText = "INSERT INTO [NSO].[dbo].[Audited_Facts_regular]([CountryID],[ContryName],[StoreID],[NSO_StoreID],[StoreName] " & _
  26.                             " ,[StoreAddres] ,[StoreStatud],[StoreComments],[StoreStarDateTime],[StoreEndDateTime],[StoreClosing]" & _
  27.                                                         " ,[PlannetAudit],[AuditInstructionID],[SyneryMode],[IMDB_ProductID],[NSO_ProductID],[ProductName]" & _
  28.                                                         " ,[ProductCategory],[Barcode],[ProductLevel],[ProductComment],[LocationID],[LocationName],[LocationComments]" & _
  29.                                                         " ,[ExhibitionID],[ExhibitionName],[ExhibitionLevel],[ExhibitionPossition],[ExhibitionComment],[VariableID]" & _
  30.                                                         " ,[VariableName],[VariableAtribute],[VariableValue],[VariableComments],[FactType],[ReUser])" & _
  31.                                     "VALUES('" & f.Cells(0).Value.Replace(" ", "-") & "','" & f.Cells(1).Value.Replace(" ", "-") & "','" & f.Cells(2).Value & "','" & f.Cells(3).Value & "','" & f.Cells(4).Value.Replace("'", "´") _
  32.                                             & "  ','" & f.Cells(5).Value.Replace(" ", "-") & "','" & f.Cells(6).Value.Replace(" ", "-") & "',' " & f.Cells(7).Value.Replace(" ", "-") & "','" & f.Cells(8).Value.Replace(" ", "-") _
  33.                                             & "  ','" & f.Cells(9).Value.Replace(" ", "-") & "',' " & f.Cells(10).Value.Replace(" ", "-") & "','" & f.Cells(11).Value.Replace(" ", "-") & "','" & f.Cells(12).Value.Replace(" ", "-") _
  34.                                             & "  ','" & f.Cells(13).Value.Replace(" ", "-") & "','" & f.Cells(14).Value.Replace(" ", "-") & "','" & f.Cells(15).Value.Replace(" ", "-") & "','" & f.Cells(16).Value.Replace(" ", "-") _
  35.                                             & "  ','" & f.Cells(17).Value.Replace(" ", "-") & "','" & f.Cells(18).Value.Replace(" ", "-") & "','" & f.Cells(19).Value.Replace(" ", "-") & "',' " & f.Cells(20).Value.Replace(" ", "-") _
  36.                                             & "  ','" & f.Cells(21).Value.Replace(" ", "-") & "','" & f.Cells(22).Value.Replace(" ", "-") & "','" & f.Cells(23).Value.Replace(" ", "-") & "','" & f.Cells(24).Value.Replace(" ", "-") _
  37.                                             & "  ','" & f.Cells(25).Value.Replace(" ", "-") & "','" & f.Cells(26).Value.Replace(" ", "-") & "','" & f.Cells(27).Value.Replace(" ", "-") & "',' " & f.Cells(28).Value.Replace(" ", "-") _
  38.                                             & "  ','" & f.Cells(26).Value.Replace(" ", "-") & "','" & f.Cells(30).Value.Replace(" ", "-") & "','" & f.Cells(31).Value.Replace(" ", "0") & "','" & f.Cells(32).Value.Replace(" ", "0") _
  39.                                             & "  ','" & f.Cells(33).Value.Replace(" ", "-") & "','" & f.Cells(34).Value.Replace(" ", "-") & "','NULL')"
  40.                      
  41.                                 Comm.Connection = Cone
  42.                                 inserts = inserts + Comm.ExecuteNonQuery()
  43.                                 filas = filas + 1
  44.                              
  45.                             End Using
  46.                         End If
  47.                     Next
  48.                 End If
  49.             End If



FUNCIONA PERFECTAMENTE

Etiquetas: basic, excel, insert, sql, studio, visual
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 20:15.