ayuda con fox y .net

Meli
19 de Abril del 2005
Tengo una aplicaci贸n en visual basic.net que me permite cargar informaci贸n de unos archivos texto hacia una base de sql server, pero despues de esto tengo que enviar esta misma informacion hacia unas tablas de visual foxpro 驴C贸mo puedo hacer que se a帽adan estos nuevos registros en mis tablas de fox? 驴Qu茅 se necesita? Gracias

sgomez
19 de Abril del 2005
Te paso una funcion (en codigo de VB) que te exporta el contenido de un recordset ADO a una tabla dbf. Fij谩te si lo pod茅s adaptar a tu c贸digo. Si est谩s utilizando ADO.NET no deber铆a haber demasiados cambios en el c贸digo:

Public Function ExportaADBF(ByVal rstLocal As ADODB.Recordset, Destino As String) As Boolean
Dim conexion As New ADODB.Connection, Archivo As String, PathArchivo As String
Dim a As Byte, AuxDestino As String
Dim AuxContador As Byte
AuxContador = 1
Dim CamposAux As New ADODB.Recordset
CamposAux.CursorLocation = adUseClient
CamposAux.Fields.Append "Campo", adChar, 10
CamposAux.Open
'CamposAux.Fields("Campo").Properties("optimize") = True
Dim Campos As Variant

a = 0
'Genera nombre de archivo
Do While Mid(Destino, Len(Destino) - a, 1) <> ""
AuxDestino = Mid(Destino, Len(Destino) - a, 1) + AuxDestino
a = a + 1
Loop
Archivo = AuxDestino
AuxDestino = ""
'Genera path del archivo
Do While Len(Destino) - a > 0
AuxDestino = Mid(Destino, Len(Destino) - a, 1) + AuxDestino
a = a + 1
Loop
PathArchivo = AuxDestino
AuxDestino = ""

'Crea conexion
conexion.Open ConnectionString:="Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" & PathArchivo & ";Exclusive=No;Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO"

'Crea archivo destino
Dim SQLCreacion As String, columna As ADODB.Field
SQLCreacion = "create dbf " & Destino & "("
a = 0
For i = 0 To rstLocal.Fields.Count - 1
SQLCreacion = SQLCreacion & IIf(Right(SQLCreacion, 1) = ")", ",", "")

'Corrige conflictos en nombres de campos
buscar = "'" & Trim(Left(replace(rstLocal.Fields(i).Name, " ", ""), 10)) & "'"
If Not (CamposAux.EOF And CamposAux.BOF) Then
CamposAux.MoveFirst
End If
CamposAux.Find "Campo=" & buscar
If CamposAux.EOF Then
CamposAux.AddNew
CamposAux(0) = Left(replace(rstLocal.Fields(i).Name, " ", ""), 10) ' & ","
CamposAux.Update

SQLCreacion = SQLCreacion & Trim(CamposAux(0))
Else
CamposAux.AddNew
CamposAux(0) = Left(replace(rstLocal.Fields(i).Name, " ", ""), 9) & AuxContador
CamposAux.Update
SQLCreacion = SQLCreacion & Left(replace(rstLocal.Fields(i).Name, " ", ""), 9) & AuxContador
AuxContador = AuxContador + 1
End If

Select Case rstLocal.Fields(i).Type
Case adBoolean
'Boolean
SQLCreacion = SQLCreacion & " L ("
Case adDBTimeStamp
'DateTime
SQLCreacion = SQLCreacion & " T ("
Case adCurrency
'Currency
SQLCreacion = SQLCreacion & " Y ("
Case adDate, adDBDate
'Date
SQLCreacion = SQLCreacion & " D ("
Case adBigInt, adInteger, adSmallInt, adTinyInt, adUnsignedBigInt, adUnsignedInt, adUnsignedSmallInt, adUnsignedTinyInt
'Integer
SQLCreacion = SQLCreacion & " I ("
Case adBinary
'Character binario
Case adDecimal, adNumeric, adSingle
'Numeric
SQLCreacion = SQLCreacion & " N ("
Case adDouble
'Double
SQLCreacion = SQLCreacion & " B ("
Case adWChar, adBSTR, adChar, adVarChar, adVarWChar
'character
SQLCreacion = SQLCreacion & " C ("
End Select

SQLCreacion = SQLCreacion & rstLocal.Fields(i).DefinedSize

'Agrega precision
Select Case rstLocal.Fields(i).Type
Case adCurrency, adDouble
'Numeric o Double
SQLCreacion = SQLCreacion & "," & rstLocal.Fields(a).Precision
End Select

SQLCreacion = SQLCreacion & ")"
Next
SQLCreacion = SQLCreacion & ")"



conexion.Execute SQLCreacion

'Genera consulta de insercion
Dim SQLInsercion As String, SQLInsercionAux As String
SQLInsercionAux = "Insert into " & Archivo & "("
'Agrega nombres de campos
For i = 0 To rstLocal.Fields.Count - 1
CamposAux.Move i, adBookmarkFirst
SQLInsercionAux = SQLInsercionAux & IIf(Right(SQLInsercionAux, 1) = "(", "", ",") & Trim(CamposAux(0))
Next
SQLInsercionAux = SQLInsercionAux & ") values ("

'Inserta registros
rstLocal.MoveFirst
Do While Not rstLocal.EOF
SQLInsercion = SQLInsercionAux
For i = 0 To rstLocal.Fields.Count - 1
SQLInsercion = SQLInsercion & IIf(Right(SQLInsercion, 1) = "(", "", ",")

Select Case rstLocal.Fields(i).Type
Case adBoolean
'Boolean
SQLInsercion = SQLInsercion & IIf(rstLocal(i), ".T.", ".F.")
Case adDBTimeStamp
'DateTime
SQLInsercion = SQLInsercion & "{" & rstLocal(i) & "}"
Case adCurrency
'Currency
SQLInsercion = SQLInsercion & " $" & rstLocal(i)
Case adDate, adDBDate
'Date
SQLInsercion = SQLInsercion & " {" & rstLocal(i) & "}"
Case adBigInt, adInteger, adSmallInt, adTinyInt, adUnsignedBigInt, adUnsignedInt, adUnsignedSmallInt, adUnsignedTinyInt
'Integer
SQLInsercion = SQLInsercion & rstLocal(i)
Case adBinary
'Character binario
Case adDecimal, adNumeric, adSingle
'Numeric
SQLInsercion = SQLInsercion & rstLocal(i)
Case adDouble
'Double
SQLInsercion = SQLInsercion & rstLocal(i)
Case adWChar, adBSTR, adChar, adVarChar, adVarWChar
'character
If IsNull(rstLocal(i)) Then
SQLInsercion = SQLInsercion & "''"
Else
SQLInsercion = SQLInsercion & "'" & Trim(replace(rstLocal(i), "'", " ")) & "'"
End If
End Select
Next
SQLInsercion = SQLInsercion & ")"
conexion.Execute SQLInsercion
rstLocal.MoveNext
Loop
End Function