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.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.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
6.Add string element to String Collection
7.Search item with IndexOf and Contains
8.Insert element at index
9.Assign value by index
10.Remove element and clear the collection
w__w___w.___j__a__v___a_2_s__.___c_o__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.