Imports System.Data.SqlClient
Public Class Form1
Public Connection As New SqlConnection("Data Source=OSCAR-B\SQLEXPRESS;Initial Catalog=DBPRUEBA;Persist Security Info=True;User ID=sa;Password=administrador")
Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
If Me.txtNombre.Text <> "" Then
If Me.cbxEmpleo.Text <> "" Then
Me.dgvEjemplo.Rows.Add(Me.txtNombre.Text, Me.cbxEmpleo.Text)
Me.txtNombre.Text = ""
Me.cbxEmpleo.Text = ""
Else
MessageBox.Show("Ingrese un deporte", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Else
MessageBox.Show("Ingrese un nombre", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub dgvEjemplo_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEjemplo.CellContentClick
If dgvEjemplo.Columns(e.ColumnIndex).Name = "Eliminar" Then
dgvEjemplo.Rows.RemoveAt(e.RowIndex)
End If
End Sub
Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
If dgvEjemplo.Rows.Count > 0 Then
Dim SqlString As String = ""
Dim ListSqlStrings As New ArrayList
For i As Integer = 0 To dgvEjemplo.Rows.Count - 1
SqlString = "INSERT INTO datos (nombre,deporte,fecha_inscripcion) VALUES ('" + dgvEjemplo.Rows(i).Cells(0).Value.ToString() + "','" + dgvEjemplo.Rows(i).Cells(1).Value.ToString() + "','" + dgvEjemplo.Rows(i).Cells(2).Value.ToString() + "')"
ListSqlStrings.Add(SqlString)
Next
If EjecutarTransaccion(ListSqlStrings) Then
MessageBox.Show("Info. guardada correctamente")
Close()
Else
MessageBox.Show("La Info. no se guardo")
End If
Else
MessageBox.Show("No hay informacion para guardar")
End If
End Sub
Public Function AbrirConexion() As Boolean
Dim band As Boolean = False
Try
Connection.Open()
band = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return band
End Function
Public Function CerrarConexion() As Boolean
Dim band As Boolean = False
Try
Connection.Close()
band = True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return band
End Function
Public Function EjecutarTransaccion(ByVal ListaSentencias As ArrayList) As Boolean
Dim band As Boolean = False
If AbrirConexion() Then
Dim command As SqlCommand = Connection.CreateCommand()
Dim transaction As SqlTransaction
Dim strSentencia As Object
Dim sentencia As String = ""
transaction = Connection.BeginTransaction()
command.Connection = Connection
command.Transaction = transaction
Try
For Each strSentencia In ListaSentencias
sentencia = strSentencia.ToString()
command.CommandText = sentencia.ToString()
command.ExecuteNonQuery()
Next
transaction.Commit()
band = True
Catch ex As Exception
MessageBox.Show(ex.Message)
Try
transaction.Rollback()
Catch ex2 As Exception
MessageBox.Show(ex2.Message)
End Try
Finally
CerrarConexion()
End Try
End If
Return band
End Function
End Class
__________________________________________________ _______________
como le cambio a Msyql o como puedo enlazar ya q me marca este error
SqlString = "INSERT INTO datos (nombre,empleo) VALUES ('" + dgvEjemplo.Rows(i).Cells(0).Value.ToString() + "','" + dgvEjemplo.Rows(i).Cells(1).Value.ToString() + "','" + dgvEjemplo.Rows(i).Cells(2).Value.ToString() + "')"
El índice estaba fuera del intervalo. Debe ser un valor no negativo e inferior al tamaño de la colección. Nombre del parámetro: index
ayuda porfa