VB.NET: SqlConnection Connection String
VB.NET: SqlConnection Connection String
Purpose: – Illustrates using SqlConnection Connection String.
Prerequistes:
- Install Visual Basic (Express or Standard Edition)
- Install SQL Server Express
- Download Northwind Database
- 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:
- Use VB 2008 (Express or Standard) Edition
- Create new project; select Console Application; name of Project could be VBNET_Syntax.
- Right-click project name in solution explorer; add new folder; name of folder could be DatabaseADONET
- Right-click folder; add class; class name could be clsSqlConnectionConnectionString.vb
- Copy code into clsSqlConnectionConnectionString.vb
- Copy code into Module1.vb
- 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 |