VB.NET: SqlConnection Connection String

VB.NET: SqlConnection Connection String

Purpose: – Illustrates using .
Prerequistes:

  1. Install Visual Basic (Express or Standard Edition)
  2. Install SQL Server Express
  3. Download Northwind Database
  4. Attach Northwind Database to Databases in Sql Express

Notes:

  • Console Application is used to simplify things, but Windows Forms or Web Forms could also be used
  • You can build a library of syntax examples by using same project over and over and just commenting out what you do not want to execute in Module1.vb

Instructions:

  1. Use VB 2008 (Express or Standard) Edition
  2. Create new project; select Console Application; name of Project could be VBNET_Syntax.
  3. Right-click project name in solution explorer; add new folder; name of folder could be DatabaseADONET
  4. Right-click folder; add class; class name could be clsSqlConnectionConnectionString.vb
  5. Copy code into clsSqlConnectionConnectionString.vb
  6. Copy code into Module1.vb
  7. Click green arrow to start with debugging

Step 1: Use View Plain to Cut-n-paste code into clsSqlConnectionConnectionString.vb

Imports System.Data
Imports System.Data.SqlClient
 
Public Class clsSQLConnectionConnectionString
    Shared WithEvents con As SqlConnection
 
    Sub Main()
        con = New SqlConnection("Server=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=SSPI")
 
        Dim cmd As New SqlCommand()
        cmd.CommandText = "PRINT('This is how to create an InfoMessage event')"
        cmd.Connection = con
 
        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Finally
            con.Close()
        End Try
        Console.ReadLine()
    End Sub
 
    Shared Sub con_InfoMessage(ByVal Sender As Object, _
                 ByVal E As SqlInfoMessageEventArgs) Handles con.InfoMessage
        Console.WriteLine( _
                   "{0} - {1}", "InfoMessage", E.Errors.Item(0).ToString())
    End Sub
End Class

Step 2: Use View Plain to Cut-n-paste code into Module1.vb

Module Module1
 
    Sub Main()
        '***** DataBase-ADO-NET *************
 
 
        Dim mySQLConnectionConnectionString As New clsSQLConnectionConnectionString
        mySQLConnectionConnectionString.Main()
 
 
    End Sub
 
End Module