Sunteți pe pagina 1din 11

0

CONSULTAS EN BASE DE DATOS

Cdigo fuente de mi Traductor


Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Public nt, na As Integer
Public evento As String
Private Sub cargar_listabd(ByVal cmb As ComboBox, ByVal cmb_server As ComboBox)
cmb.Items.Clear()
'Asignamos la lista de servidores como origen de datos
' del combobox
Dim cadenaConexion As String
Dim selectSQL As String
'Componemos la cadena de conexin con el servidor seleccionado y
' seguridad integrada
' si la autenticacin se hace con usuario y password hay
' que cambiar la cadena
cadenaConexion = "Data Source=" & cmb_server.Text & " ;Integrated
Security=True;Initial Catalog=master"
Using con As New SqlConnection(cadenaConexion)
' Abrimos la conexin
con.Open()
'Obtenemos los nombres de las bases de datos que
' haya en el servidor
selectSQL = "select name from sys.databases;"
'selectSQL = "use prueba; select codigo from t1;"
Dim com As SqlCommand = New SqlCommand(selectSQL, con)
Dim dr As SqlDataReader = com.ExecuteReader()
' Recorremos el dataReader
While (dr.Read())
cmb.Items.Add(dr(0).ToString())
End While
'Asignamos la lista de bases de datos como origen
'de datos del combobox
End Using
End Sub
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim servidores As SqlDataSourceEnumerator
'Obtenemos el enumerador SQL
Dim tablaServidores As DataTable
'Dimenesionamos la tabla que guardara los datos
servidores = SqlDataSourceEnumerator.Instance
'Obtenemos las instancias sql
tablaServidores = New DataTable()
tablaServidores = servidores.GetDataSources()
'Copiamos las instancias al datatable
Dim rowServidor As DataRow
'Dimensionamos una fila para recorrer el datatable
For Each rowServidor In tablaServidores.Rows
If String.IsNullOrEmpty(rowServidor("InstanceName").ToString()) Then
'El siguiente codigo aade las instancias
cmb_server_bd.Items.Add(rowServidor("ServerName").ToString())
Else

cmb_server_bd.Items.Add(rowServidor("ServerName") & "\\" &


rowServidor("InstanceName"))
End If
Next
End Sub
Private Sub cmb_server_bd_SelectedIndexChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cmb_server_bd.SelectedIndexChanged
'Cuando se cambia el servidor, cambiamos la base de datos
cargar_listabd(cmb_bd_bd, sender)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles
Button1.Click
Dim consultax As String
Dim cadenaConexion As String
Dim selectSQL As String
cadenaConexion = "Data Source=" & cmb_server_bd.Text & " ;Integrated
Security=True;Initial Catalog=master"
Using con As New SqlConnection(cadenaConexion)
con.Open()
'Obtenemos los nombres de las bases de datos que
' haya en el servidor
'selectSQL = "select name from sys.databases;"
consultax = conv_cadema(TextBox1.Text)
If consultax <> "" Then
selectSQL = "use " & cmb_bd_bd.Text & " ; " & consultax & "; "
Dim com As SqlCommand = New SqlCommand(selectSQL, con)
Try
Dim dr As SqlDataReader = com.ExecuteReader()
Label4.Text = ""
While (dr.Read())
For i = 0 To nt - 1
Label4.Text += " " & dr(i).ToString()
Next
Label4.Text += vbCrLf
End While
If Label4.Text = "" Then
'If dr.Read = Nothing Then
If evento = "SELECCIONAR" Then
MsgBox("ERROR, No existen registros")
End If
If evento = "BORRAR" Or evento = "ELIMINAR" Then
MsgBox("Registro(s) eliminados")
End If
'End If
End If
Catch ex As Exception
MessageBox.Show("ERROR de consulta:" & vbCrLf & _
ex.Message, "ERROR", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Using
End Sub
Function conv_cadema(x As String)
Dim Atributos(100), Condiciones(100), temp1, tabla, consulta As String
Dim pos1, pos2 As Integer

consulta = ""
'quitamos espacios en blanco a los lados
x = UCase(Trim(x))
'evaluamos el evento
pos1 = InStr(x, " ")
If pos1 = 0 Then
MsgBox("Error, Sintaxis incorrecta no se encuentra evento")
consulta = ""
Return consulta
End If
temp1 = Mid(x, 1, pos1 - 1)
x = Trim(Mid(x, pos1, Len(x) - pos1 + 1))
If temp1 = "SELECCIONAR" Then
evento = temp1
consulta += "SELECT"
'Console.WriteLine(consulta)
ElseIf temp1 = "BORRAR" Or temp1 = "ELIMINAR" Then
evento = temp1
consulta += "DELETE"
pos1 = InStr(x, "DE ")
If pos1 = 0 Then
MsgBox("Error, Sintaxis incorrecta no se encuentra tabla a seleccionar")
consulta = ""
Return consulta
End If
If pos1 = 1 Then
temp1 = Mid(x, 1, pos1)
x = Trim(Mid(x, pos1 + 3, Len(x) - pos1 + 1))
GoTo q1
Else
MsgBox("Error, Sintaxis incorrecta")
consulta = ""
Return consulta
End If
'Console.WriteLine(consulta)
Else
'Console.WriteLine("ERROR, NO SE RECONOCE EL EVENTO " & temp1)
MsgBox("ERROR, NO SE RECONOCE EL EVENTO " & temp1)
consulta = ""
Return consulta
End If
'evaluamos los campos
pos1 = InStr(x, " DE ")
If pos1 = 0 Then
'MsgBox("Error, Sintaxis incorrecta no se encuentra tabla a seleccionar")
MsgBox("Error, Sintaxis incorrecta")
consulta = ""
Return consulta
End If
temp1 = Mid(x, 1, pos1)
x = Trim(Mid(x, pos1 + 4, Len(x) - pos1 + 1))
'Console.WriteLine(x)
Dim band, nc As Integer
band = 0
nt = 0
na = 0

While band = 0
nt += 1
pos1 = InStr(temp1, ",")
If pos1 = 0 Then
Atributos(nt) = Trim(temp1)
If Atributos(nt) = "*" Then
na += 1
Else
If evalua_nombre(Atributos(nt)) = 0 Then
MsgBox("ERROR en sintaxis en atributo --> " & Atributos(nt))
consulta = ""
Return consulta
End If
End If
band = 1
Else
Atributos(nt) = Trim(Mid(temp1, 1, pos1 - 1))
If Atributos(nt) = "*" Then
na += 1
Else
If evalua_nombre(Atributos(nt)) = 0 Then
MsgBox("ERROR en sintaxis en atributo --> " & Atributos(nt))
consulta = ""
Return consulta
End If
End If
temp1 = Mid(temp1, pos1 + 1, Len(temp1) - pos1)
End If
End While
For i = 1 To nt - 1
consulta += " " & Atributos(i) & ","
Next
consulta += " " & Atributos(nt)
q1:

'evaluamos tabla
If Len(x) = 0 Then
' Console.WriteLine("ERROR, no se especific una tabla para la consulta")
'MsgBox("ERROR, no se especific una tabla para la consulta")
MsgBox("ERROR de sintaxis")
consulta = ""
Return consulta
End If
pos1 = InStr(x, " DONDE ")
If pos1 = 0 Then
temp1 = x
tabla = temp1
If evalua_nombre(temp1) = 0 Then
MsgBox("ERROR de sintaxis" & temp1)
consulta = ""
Return consulta
End If
consulta += " FROM " & temp1
MsgBox("CONSULTA EN SQL: " & vbCrLf & " " & consulta)
GoTo q2
Else
temp1 = Trim(Mid(x, 1, pos1))
tabla = temp1

If evalua_nombre(temp1) = 0 Then
MsgBox("ERROR de sintaxis en el nombre de la tabla --> " & temp1)
consulta = ""
Return consulta
End If
x = Trim(Mid(x, pos1 + 7, Len(x) - pos1 - 6))
consulta += " FROM " & temp1 & " WHERE"
'evaluamos condiciones
band = 0
nc = 0
'Console.WriteLine(x)
While band = 0
nc += 1
pos1 = InStr(x, " Y ")
pos2 = InStr(x, " O ")
If pos1 = 0 Then
If pos2 = 0 Then
Condiciones(nc) = Trim(x)
If evalua_condicion(Condiciones(nc)) = "" Then
MsgBox("ERROR de sintaxis en condicin --> " & Condiciones(nc))
consulta = ""
Return consulta
End If
consulta += " " & Condiciones(nc)
band = 1
Else
Condiciones(nc) = Trim(Mid(x, 1, pos2 - 1))
If evalua_condicion(Condiciones(nc)) = "" Then
MsgBox("ERROR de sintaxis en condicin --> " & Condiciones(nc))
consulta = ""
Return consulta
End If
x = Mid(x, pos2 + 3, Len(x) - pos2 + 2)
consulta += " " & Condiciones(nc) & " OR"
End If
Else
Condiciones(nc) = Trim(Mid(x, 1, pos1 - 1))
If evalua_condicion(Condiciones(nc)) = "" Then
MsgBox("ERROR de sintaxis en condicin --> " & Condiciones(nc))
consulta = ""
Return consulta
End If
x = Mid(x, pos1 + 3, Len(x) - pos1 + 2)
consulta += " " & Condiciones(nc) & " AND"
End If
End While

q2:

MsgBox("CONSULTA EN SQL: " & vbCrLf & " " & consulta)
End If

Dim cadenaConexion As String


Dim selectSQL As String
cadenaConexion = "Data Source=" & cmb_server_bd.Text & " ;Integrated
Security=True;Initial Catalog=master"
Using con As New SqlConnection(cadenaConexion)
con.Open()

selectSQL = "use " & cmb_bd_bd.Text & " ; SELECT COUNT(*) FROM
information_schema.columns WHERE table_name = '" & tabla & "'; "
Try
Dim com As SqlCommand = New SqlCommand(selectSQL, con)
nt -= na
nt += (na) * CInt(com.ExecuteScalar())
Catch ex As Exception
MessageBox.Show("ERROR de consulta:" & vbCrLf & _
ex.Message, "ERROR", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
Return consulta
End Function
Sub evalua(ss)
If ss = 0 Then
'Console.WriteLine("Error, Sintaxis incorrecta")
MsgBox("Error, Sintaxis incorrecta")
'Console.ReadLine()
End
End If
End Sub
Function evalua_nombre(aa As String)
Dim i, e, b As Integer
If aa = "SELECCIONAR" Or aa = "ELIMINAR" Or aa = "BORRAR" Or aa = "DE" Or aa =
"DONDE" Then
b=0
Return b
End If
i=1
e=1
b=0
While i <= Len(aa)
Select Case e
Case 1
Select Case Mid(aa, i, 1)
Case "."
e=2
b=0
Case "#", "_", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
e=3
b=1
Case Else
b=0
i = Len(aa) + 1
End Select
Case 2
Select Case Mid(aa, i, 1)
Case "#", "_", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
e=3
b=1
Case Else
b=0
i = Len(aa) + 1

End Select
Case 3
Select Case Mid(aa, i, 1)
Case "#", "_", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "0"
e=3
b=1
Case Else
b=0
i = Len(aa) + 1
End Select
Case Else
End Select
i += 1
End While
Return b
End Function
Function evalua_condicion(gg As String)
Dim xx, res As String
Dim p As Integer
res = ""
p = InStr(gg, "<=")
If p <> 0 Then
xx = Trim(Mid(gg, 1, p - 1))
gg = Trim(Mid(gg, p + 2, Len(gg) - 1))
If evalua_nombre(xx) = 0 Then
MsgBox("ERROR en sintaxis en condicin --> " & xx)
res = ""
Return res
End If
res += xx & " <= "
Else
p = InStr(gg, ">=")
If p <> 0 Then
xx = Trim(Mid(gg, 1, p - 1))
gg = Trim(Mid(gg, p + 2, Len(gg) - 1))
If evalua_nombre(xx) = 0 Then
MsgBox("ERROR en sintaxis en condicin --> " & xx)
res = ""
Return res
End If
res += xx & " >= "
Else
p = InStr(gg, "<")
If p <> 0 Then
xx = Trim(Mid(gg, 1, p - 1))
gg = Trim(Mid(gg, p + 1, Len(gg) - 1))
If evalua_nombre(xx) = 0 Then
MsgBox("ERROR en sintaxis en condicin --> " & xx)
res = ""
Return res
End If
res += xx & " < "
Else
p = InStr(gg, ">")
If p <> 0 Then

xx = Trim(Mid(gg, 1, p - 1))
gg = Trim(Mid(gg, p + 1, Len(gg) - 1))
If evalua_nombre(xx) = 0 Then
MsgBox("ERROR en sintaxis en condicin --> " & xx)
res = ""
Return res
End If
res += xx & " > "
Else
p = InStr(gg, "=")
If p <> 0 Then
xx = Trim(Mid(gg, 1, p - 1))
gg = Trim(Mid(gg, p + 1, Len(gg) - 1))
If evalua_nombre(xx) = 0 Then
MsgBox("ERROR en sintaxis en condicin --> " & xx)
res = ""
Return res
End If
res += xx & " = "
Else
'Console.WriteLine("ERROR en sintaxis de condicion - " & gg)
MsgBox("ERROR en sintaxis de condicion --> " & gg)
End
End If
End If
End If
End If
End If
If Mid(gg, 1, 1) = "'" Then
If Mid(gg, Len(gg), 1) = "'" Then
Return res
Else
res = ""
Return res
End If
Else
'MsgBox("1" & gg)
Dim b, i, e As Integer
i=1
e=1
b=0
While i <= Len(gg)
Select Case Mid(gg, i, 1)
Case "1", "2", "3", "4", "5", "6", "7", "8", "9"
b=1
Case Else
b=0
i = Len(gg) + 1
End Select
i += 1
End While
If b = 1 Then
Return res
Else
res = ""
Return res
End If

End If
Return res
End Function
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles
Button2.Click
Me.Close()
End Sub
End Class

S-ar putea să vă placă și