Ver Mensaje Individual
  #15 (permalink)  
Antiguo 21/02/2012, 13:40
valderramalex
 
Fecha de Ingreso: febrero-2012
Ubicación: bogota
Mensajes: 104
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Pasar de Datagridview a Sql

me sale un error ahora con los nombres de las columnas, este es el codigo :
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim dt As New DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openFD As New OpenFileDialog()
With openFD
.Title = "Seleccionar archivos"
.Filter = "Todos los archivos (*.xls)|*.xls"
.Multiselect = False
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocume nts
If .ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = .FileName
End If
End With
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim strconn As String
strconn = "Provider=Microsoft.Jet.Oledb.4.0; data source= " + TextBox1.Text + ";Extended properties=""Excel 8.0;hdr=yes;imex=1"""
Dim mconn As New OleDbConnection(strconn)
Dim ad As New OleDbDataAdapter("Select * from [" & TextBox2.Text & "$]", mconn)
mconn.Open()
ad.Fill(dt)
mconn.Close()
Me.DataGridView1.DataSource = dt
Catch ex As OleDbException
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
With DataGridView1
.Columns(3).Visible = False
.Columns(4).Visible = False
End With
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
Using cnn = New SqlConnection("Data Source=GL203\SQLEXPRESS;Initial Catalog=Asamblea;Integrated Security=True")
cnn.Open()
Dim sql As String = "INSERT INTO Asamblea (ID, PORCENTAJE,FECHA_INGRESO,FECHA_SALIDA,HORA_ENTRADA ,HORA_SALIDA,ESTADO) VALUES (ID,PORCENTAJE,FECHA_INGRESO,FECHA_SALIDA,HORA_ENT RADA,HORA_SALIDA,ESTADO)"
For Each fila As DataGridViewRow In DataGridView1.Rows
Using command As New SqlCommand(sql, cnn)
command.Parameters.AddWithValue("ID", fila.Cells(0).Value)
command.Parameters.AddWithValue("PORCENTAJE", fila.Cells(1).Value)
command.Parameters.AddWithValue("FECHA_ENTRADA", fila.Cells(2).Value)
command.Parameters.AddWithValue("FECHA_SALIDA", fila.Cells(3).Value)
command.Parameters.AddWithValue("HORA_ENTRADA", fila.Cells(4).Value)
command.Parameters.AddWithValue("HORA_SALIDA", fila.Cells(5).Value)
command.Parameters.AddWithValue("ESTADO", fila.Cells(6).Value)
command.ExecuteNonQuery()
End Using
Next
End Using
Catch ex As Exception
MsgBox(ex.Message.ToString)
MsgBox("no se pudo cargar a la base de datos")
End Try
End Sub
End Class