Click here to Skip to main content
15,895,809 members
Articles / Web Development / XHTML

SQL Simple Utilities

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
22 Sep 2016CPOL15 min read 35.7K   1.8K   32  
This project provides utilities for SQL server, such as executing a list of SQL scripts, exporting data to an SQL script, and displaying relationships between records.
Imports System.Data.SqlClient

Public Class ctrlConnection

    Public Event DBChanged(ByVal db As String)

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        ComboboxSetTextBoxWithHistory(ComboBoxServer, ComboBoxUserId)
    End Sub

    Private Sub MBD(ByVal s As String)
        Throw New Exception("The " & s & " must be defined!")
    End Sub

    Private Sub MBD(ByVal t As TextBoxBase, ByVal s As String)
        If t.Text = "" Then
            MBD(s)
        End If
    End Sub

    Private Sub MBD(ByVal t As ComboBox, ByVal s As String)
        If t.Text = "" Then
            MBD(s)
        End If
    End Sub

    Private Sub ValidateSUP()
        MBD(ComboBoxServer, "server")
        MBD(ComboBoxUserId, "user id")
        MBD(TextBoxPassword, "password")
    End Sub

    Public ReadOnly Property ConnectionString As String
        Get
            ValidateSUP()
            MBD(ComboBoxDatabases, "database")
            Return GetConnectionString(ComboBoxServer.Text, ComboBoxUserId.Text, TextBoxPassword.Text, ComboBoxDatabases.Text)
        End Get
    End Property

    Public Sub LoadDatabases()
        ValidateSUP()
        Dim cn As New SqlConnection
        cn.ConnectionString = _
            GetConnectionString(ComboBoxServer.Text, ComboBoxUserId.Text, TextBoxPassword.Text, "master")
        Using cn
            Dim cmd As New SqlCommand("SELECT name FROM sys.databases ORDER BY name", cn)
            RemoveHandler ComboBoxDatabases.SelectedIndexChanged, AddressOf ComboBoxDatabases_SelectedIndexChanged
            ComboBoxDatabases.DisplayMember = "name"
            ComboBoxDatabases.ValueMember = "name"
            ComboBoxDatabases.DataSource = GetDataset(cmd).Tables(0)
            AddHandler ComboBoxDatabases.SelectedIndexChanged, AddressOf ComboBoxDatabases_SelectedIndexChanged
        End Using
        RaiseEvent DBChanged(ComboBoxDatabases.SelectedValue)
    End Sub

    Private Sub ButtonGetDatabases_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGetDatabases.Click
        Try
            LoadDatabases()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub ComboBoxDatabases_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxDatabases.SelectedIndexChanged
        RaiseEvent DBChanged(ComboBoxDatabases.SelectedValue)
    End Sub

    Private Sub TextBoxPassword_Enter(sender As System.Object, e As System.EventArgs) Handles TextBoxPassword.Enter
        TextBoxPassword.SelectAll()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Unisystems
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions