Anda di halaman 1dari 2

C:\Users\Juan Nava\Desktop\proyectos\EXCELTOSQLCSHARP\EXCELTOSQLVB\EXCELTOSQLVB\MySQL.

vb 1
Imports System.Data.SqlClient
Imports System.Data
Imports System.Text.RegularExpressions

Public Class MySQL


Public cnn As SqlConnection
Private mMessages As String
Public dataAdapter As New SqlDataAdapter()
Public table As New DataTable()
Public bindingSource1 As New BindingSource()

Public Sub New(ByVal pServer As String, ByVal pDatabase As String, ByVal pUserId As
String, _
ByVal pPassword As String)

Dim strCnn As SqlConnectionStringBuilder = _


New SqlConnectionStringBuilder()

If pUserId = "Ing. Juan Nava Chalacha" Then


pUserId = "Juan"
Else
End If

strCnn.DataSource = pServer
strCnn.InitialCatalog = pDatabase
strCnn.UserID = pUserId
strCnn.Password = pPassword
strCnn.IntegratedSecurity = (pUserId.Trim() = "")
cnn = New SqlConnection(strCnn.ConnectionString)
AddHandler cnn.InfoMessage, AddressOf cnn_InfoMessage
End Sub
Public Function ParseScriptToCommands(ByVal script As String) As String()
Dim commands() As String
commands = Regex.Split(script, _
"GO\r\n", _
RegexOptions.IgnoreCase)
Return commands
End Function
Public Sub GetData(ByVal selectCommand As String, ByVal tabla As String)

Try
' cadena de conexion
Dim connectionString As String = cnn.ConnectionString

' Crea un nuevo DataAdapter basado en la consulta especificada en la función.


Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)

' Crear un command builder para generar los commandos SQL update, insert, y
' delete basado en selectCommand. These are used to
' update the database.
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)

' Populate a new data table and bind it to the BindingSource.


'Public table As New DataTable()
table.TableName() = tabla
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table

Catch ex As SqlException
MessageBox.Show("To run this example, replace the value of the " + _
"connectionString variable with a connection string that is " + _
"valid for your system.")
End Try

End Sub
Public Sub LlenarDataGridView(ByVal DataGridView1 As DataGridView, ByVal comando As
String, ByVal TablaDelComando As String)
With DataGridView1
C:\Users\Juan Nava\Desktop\proyectos\EXCELTOSQLCSHARP\EXCELTOSQLVB\EXCELTOSQLVB\MySQL.vb 2
' alternar color de filas
.AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
.DefaultCellStyle.BackColor = Color.Beige
' Establecer el origen de datos para el DataGridview
.DataSource = bindingSource1
' Resize the DataGridView columns to fit the newly loaded content.
.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)

End With

GetData(comando, TablaDelComando) ' comando = "select * from tabla"

End Sub

Public Sub CheckIdent(ByVal Table As String, _


ByVal NewReseedValue As Integer)

'Se declaran la variables a utilizar


Dim cmd As SqlCommand = _
New SqlCommand( _
"DBCC CHECKIDENT(@Table,RESEED,@NewReseedValue)")

'Se agregan los parámetros @Table y @NewReseedValue


cmd.Parameters.Add("@Table", _
SqlDbType.VarChar, 150).Value = Table
cmd.Parameters.Add("@NewReseedValue", _
SqlDbType.Int).Value = NewReseedValue
'Se asigna la conexión al command
cmd.Connection = cnn
'Se intentará la ejecución del código
Try
'Se abre la conexión
cnn.Open()
'Se ejecuta la instrucción
cmd.ExecuteNonQuery()
Catch ex As Exception
'Se capturan los mensajes de error
mMessages = ex.Message
Finally
'se cierra la conexión
cnn.Close()
End Try
End Sub

Private Sub cnn_InfoMessage(ByVal sender As Object, ByVal e As System.Data.SqlClient.


SqlInfoMessageEventArgs)

mMessages = e.Message

End Sub

Public ReadOnly Property Messages() As String


Get
Return mMessages
End Get
End Property
End Class

Anda mungkin juga menyukai