Visual Basic.NET - Problema al insertar en sql archivos .csv

 
Vista:

Problema al insertar en sql archivos .csv

Publicado por Cecilia (1 intervención) el 02/03/2006 23:48:22
Hola tengo un problema tengo una hoja en excel llena de datos y la inserto en una tabla de sql de la siguiente manera.

Dim x As String
Try

Dim openFileDialog1 As New OpenFileDialog
Try
openFileDialog1.Filter = "Archivos de Texto|*.csv"
openFileDialog1.Title = "Seleccionar un archivo de Texto"
If openFileDialog1.ShowDialog() = DialogResult.OK Then
If openFileDialog1.FileName <> "" Then

Try
x = openFileDialog1.FileName.Substring(openFileDialog1 .FileName.LastIndexOf("\"))
Dim y As Integer
longitud = x.Length
longitud = longitud - longitud + 2
firstword = Mid(x, longitud, x.Length)
Catch ex As Exception
End Try
End If
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim dire As String = openFileDialog1.FileName
Dim excelConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("provider=Micros oft.Jet.OLEDB.4.0; " & "data source='" & dire.ToString & "'; " & "Extended Properties=Excel 8.0;")
excelConnection.Open()
firstword = Mid(x, longitud, x.Length - 5)
Dim MyCommand = New System.Data.OleDb.OleDbDataAdapter("INSERT INTO [ODBC; Driver={SQL Server};Server=(local);Database=Cydsadb;Trusted_Co nnection=yes].[DESCRU2004CORT] SELECT * FROM [" & firstword.Trim + "$" & "];", excelConnection)
MyCommand.TableMappings.Add("Table", "ExcelTest")
Dim DS = New System.Data.DataSet
MyCommand.Fill(DS)
Me.datagrid1.DataSource = DS.Tables(0).DefaultView
Catch ex As Exception
MsgBox(ex.Message)
End Try

el problema es cuando trato de hacer lo mismo con archivos .csv, si me los lee y los pasa a un datagrid pero no puedo hacer que me los guarde...me marca el siguiente error...Syntaxix error in INSERT INTO statement

lo hago de la siguiente manera

Try
Dim cnx As OdbcConnection
Dim cn As SqlConnection
Dim dt As New DataTable
Dim da As New OdbcDataAdapter
Dim cmd As OdbcCommand
Dim pTest As OdbcParameter
Dim cnxStr As String
Dim CSVFolder As String
Dim CSVFileName As String
CSVFolder = "C:\"
CSVFileName = "DESCRU2004CORT.csv"
cnxStr = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + CSVFolder + ";"
cnx = New OdbcConnection(cnxStr)

cmd = New OdbcCommand("INSERT INTO [ODBC; Driver={SQL Server};Server=(local);Database=Cydsadb;Trusted_Co nnection=yes].[DESCRU2004CORT] SELECT * FROM [DESCRU2004CORT.csv];", cnx)
cmd.CommandType = CommandType.Text
da.SelectCommand = cmd
cnx.Open()
da.Fill(dt)

Me.datagrid1.DataSource = dt
' Me.datagrid1.DataBind()

Catch ex As Exception
MsgBox(ex.Message)
End Try

alguna idea ????
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder