Ver Mensaje Individual
  #18 (permalink)  
Antiguo 21/02/2012, 14:35
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

este es el codigo funcionando por si alguien lo necesita:

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 Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
Using cnn = New SqlConnection("su conexion a la base de datos")
cnn.Open()
Dim sql As String = "INSERT INTO nombre de tabla (ID, PORCENTAJE,FECHA_INGRESO,FECHA_SALIDA,HORA_ENTRADA ,HORA_SALIDA,ESTADO) VALUES (@ID,@PORCENTAJE,@FECHA_INGRESO,@FECHA_SALIDA,@HOR A_ENTRADA,@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_INGRESO", 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

tienen que agregarle:

2 textbox
1 datagridview
4 botones