Database Connection state change event : Collection « Data Structure « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Data Structure » CollectionScreenshots 
Database Connection state change event
Database Connection state change event

Imports System
Imports System.Data
Imports System.Data.SqlClient


public class MainClass
   Shared Sub Main()
      Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
          "integrated security=sspi;database=MyDatabase")

      ' Sql Query
      Dim sql As String = "SELECT FirstName, LastName From Employee"

      ' Create command
      Dim thisCommand As New SqlCommand(sql, thisConnection)

      ' Add second handler to StateChange event
      AddHandler thisConnection.StateChange, AddressOf StateIsChanged

      Try

         ' Open connection and fire two statechange events
         thisConnection.Open()

         ' Create datareader
         Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()

         ' Display rows in event log
         While thisReader.Read()
            Console.WriteLine(thisReader.GetString(0_
               "-" + thisReader.GetString(1))
         End While
      Catch ex As SqlException
         Console.WriteLine(ex.Message)
      Finally
         ' Remove second handler from StateChange event
         RemoveHandler thisConnection.StateChange, AddressOf StateIsChanged

         ' Close connection and fire one StateChange event
         thisConnection.Close()
      End Try
   End Sub
   Shared Private Sub StateIsChanged(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs)
      ' Event handler for the StateChange Event
      Console.WriteLine("Entering Second StateChange EventHandler")
      Console.WriteLine("Sender = " + sender.ToString())
      Console.WriteLine("Original State = " + e.OriginalState.ToString())
      Console.WriteLine("Current State = " + e.CurrentState.ToString())
      Console.WriteLine("Exiting Second StateChange EventHandler")
   End Sub

End Class

           
       
Related examples in the same category
1.Store Objects in Collection and retrieveStore Objects in Collection and retrieve
2.Loop through CollectionLoop through Collection
3.For Each loop through a CollectionFor Each loop through a Collection
4.Do While loop through a CollectionDo While loop through a Collection
5.Store Class in a Collection and Retrieve by NameStore Class in a Collection and Retrieve by Name
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.