FANDOM


Conexión con la base de datos Editar

Option Explicit On

Imports System.Data.SqlClient

Public Class Form1

  Private Sub Form1_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles MyBase.Load

    Dim conDiscoteca As New SqlConnection

    conDiscoteca.ConnectionString = _
      "Data Source=.\SQLEXPRESS;" & _
      "AttachDbFilename=C:\Users\Juan Diego\Documents\discoteca.mdf;" & _
      "Integrated Security=True;" & _
      "User Instance=True;" & _
      "Connect Timeout=30"

    Try
      conDiscoteca.Open()
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

    ' Aquí podríamos realizar las operaciones de
    ' consulta o modificación de datos.

    If conDiscoteca.State = ConnectionState.Open Then
      conDiscoteca.Close()
    End If

  End Sub

End Class

Recuperación de un conjunto de registros Editar

Option Explicit On

Imports System.Data.SqlClient

Public Class Form1

  Private Sub Form1_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles MyBase.Load

    Dim conDiscoteca As New SqlConnection

    conDiscoteca.ConnectionString = _
      "Data Source=.\SQLEXPRESS;" & _
      "AttachDbFilename=C:\Users\Juan Diego\Documents\discoteca.mdf;" & _
      "Integrated Security=True;" & _
      "User Instance=True;" & _
      "Connect Timeout=30"

    Try
      conDiscoteca.Open()
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

    Dim sConsulta As String
    sConsulta = "SELECT * FROM grabaciones"
    Dim cmdConsulta = New SqlCommand(sConsulta, conDiscoteca)

    Dim adpGrabaciones As New SqlDataAdapter
    adpGrabaciones.SelectCommand = cmdConsulta

    Dim dstGrabaciones As New DataSet
    adpGrabaciones.Fill(dstGrabaciones, "grabaciones")

    If (dstGrabaciones.Tables("grabaciones").Rows.Count = 0) Then
      MessageBox.Show("La tabla de grabaciones está vacía.")
    Else
      dgrdGrabaciones.DataSource = dstGrabaciones.Tables("grabaciones")
    End If

    If conDiscoteca.State = ConnectionState.Open Then
      conDiscoteca.Close()
    End If

  End Sub

End Class

Editor de consultas SQL Editar

Option Explicit On

Imports System.Data.SqlClient

Public Class frmConsultas

  ' Consultas predeterminadas.
  Const sConsultaGrabaciones As String = _
    "SELECT " & vbCrLf & _
    "  titulo " & vbCrLf & _
    "FROM " & vbCrLf & _
    "  grabaciones"

  Const sConsultaInterpretes As String = _
    "SELECT " & vbCrLf & _
    "  nombre " & vbCrLf & _
    "FROM " & vbCrLf & _
    "  interpretes"

  Const sConsultaTodo As String = _
    "SELECT " & vbCrLf & _
    "  titulo, " & vbCrLf & _
    "  nombre, " & vbCrLf & _
    "  nacionalidad " & vbCrLf & _
    "FROM " & vbCrLf & _
    "  grabaciones, " & vbCrLf & _
    "  interpretes " & vbCrLf & _
    "WHERE " & vbCrLf & _
    "  grabaciones.idinterprete=" & vbCrLf & _
    "  interpretes.Id"

  Dim conDiscoteca As New SqlConnection

  Private Sub MostrarError(ByVal Excepcion As Exception)

    ' Mostramos un mensaje que describa el error.
    Call MsgBox( _
      "Ha ocurrido un error:" & vbCrLf & _
      Excepcion.Message, _
      vbExclamation, _
      "Atención" _
    )

  End Sub

  Private Sub frmConsultas_FormClosed( _
    ByVal sender As Object, _
    ByVal e As System.Windows.Forms.FormClosedEventArgs _
  ) Handles Me.FormClosed

    If conDiscoteca.State = ConnectionState.Open Then
      conDiscoteca.Close()
    End If

  End Sub

  Private Sub frmConsultas_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles MyBase.Load

    conDiscoteca.ConnectionString = _
      "Data Source=.\SQLEXPRESS;" & _
      "AttachDbFilename=C:\Users\Juan Diego\Documents\discoteca.mdf;" & _
      "Integrated Security=True;" & _
      "User Instance=True;" & _
      "Connect Timeout=30"

    Try
      conDiscoteca.Open()
    Catch ex As Exception
      MostrarError(ex)
    End Try

  End Sub

  Private Sub RealizarConsulta(ByVal Consulta As String)

    Dim cmdConsulta = New SqlCommand(Consulta, conDiscoteca)

    Dim adpGrabaciones As New SqlDataAdapter
    adpGrabaciones.SelectCommand = cmdConsulta

    Dim dstGrabaciones As New DataSet
    Try
      adpGrabaciones.Fill(dstGrabaciones, "consulta")
      dgrdGrabaciones.DataSource = _
        dstGrabaciones.Tables("consulta")
    Catch ex As Exception
      MostrarError(ex)
    End Try

  End Sub

  Private Sub btnConsultar_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles btnConsultar.Click

    RealizarConsulta(txtConsulta.Text)

  End Sub

  Private Sub radGrabaciones_CheckedChanged( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles radGrabaciones.CheckedChanged

    ' Mostrarmos el texto correspondiente a la
    ' consulta en el cuadro de texto.
    txtConsulta.Text = sConsultaGrabaciones

  End Sub

  Private Sub radInterpretes_CheckedChanged( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles radInterpretes.CheckedChanged

    ' Mostrarmos el texto correspondiente a la
    ' consulta en el cuadro de texto.
    txtConsulta.Text = sConsultaInterpretes

  End Sub

  Private Sub radTodo_CheckedChanged( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs _
  ) Handles radTodo.CheckedChanged

    ' Mostrarmos el texto correspondiente a la
    ' consulta en el cuadro de texto.
    txtConsulta.Text = sConsultaTodo

  End Sub

End Class
El contenido de la comunidad está disponible bajo CC-BY-SA a menos que se indique lo contrario.