November 17, 2003

AddHandlerByName – Attaching to an object an event handler by name

‘ A generic routine that takes an object, an event name, and a list of potential’ delegates to event handlers, and selects the first delegate that matches the’ expected signature of the event handler.’ ‘ Example:’ AddHandlerByName(da, “RowUpdated”, ‘ New OleDbRowUpdatedEventHandler’ (AddressOf OnOleDbRowUpdated), ‘ New SqlRowUpdatedEventHandler(AddressOf OnSqlRowUpdated))Sub AddHandlerByName(ByVal obj As

CreateConnection – Creating a suitable connection object for a given connection string

‘ Create a suitable connection object for a given connection string.’ Requires Imports for System.Data, System.Data.Common, System.Data.OleDb,’ System.Data.SqlClient and System.Data.Odbc” Example: Dim cn As IDbConnection = CreateConnection(connStr)Function CreateConnection(ByVal connString As String) As IDbConnection If connString.ToLower.IndexOf(“provider=”) >= 0 Then Return New OleDbConnection(connString) ElseIf connString.ToLower.IndexOf(“driver”) >= 0 Then Return New OdbcConnection(connString) Else

BlobToFile – Saving a BLOB field to a file

‘ Reusable routine that save a BLOB field to a file’ Requires Imports for System.Data and System.Data.Common’ Example: BlobToFile(dr, 2, “c:xxxx.bmp”)Sub BlobToFile(ByVal dr As IDataReader, ByVal fieldIndex As Integer, _ ByVal filename As String) Const CHUNK_SIZE As Integer = 200 Dim buffer(CHUNK_SIZE – 1) As Byte Dim stream As New

FileToBlob – Loading a file into a SQL Server’s BLOB

‘ Reusable routine that loads a file into a SQL Server’s BLOB.’ Requires Imports for System.Data and System.Data.SqlClient’ (It supports only tables with one key field.)” Example:’ FileToBlob(cn, “pub_info”, “logo”, “pub_id”, “0877”, “c:Example.bmp”)Sub FileToBlob(ByVal cn As SqlConnection, ByVal tableName As String, _ ByVal blobField As String, ByVal keyField As String,

CreateDataAdapter – Creating a suitable DataAdapter object for a given connection object

‘ Create a suitable DataAdapter object for a given connection object.’ Requires Imports for System.Data, System.Data.Common, System.Data.OleDb,’ System.Data.SqlClient, System.Data.Odbc and System.Data.OracleClient” Example:’ Dim da As DbDataAdapter = CreateDataAdapter(“SELECT * FROM Titles”, cn)Function CreateDataAdapter(ByVal sql As String, ByVal cn As IDbConnection) As _ DbDataAdapter If TypeOf cn Is OleDbConnection Then ‘

CreateDataAdapter – Creating a DataAdapter for a given connection object, via Reflection

‘ Create a suitable DataAdapter object for a given connection object.’ Example:’ Dim da As DbDataAdapter = CreateDataAdapter(cn, “SELECT * FROM Titles”)Function CreateDataAdapter(ByVal cn As IDbConnection, _ ByVal sql As String) As DbDataAdapter ‘ We must box CN to invoke the GetType method. Dim cnType As Type = CObj(cn).GetType() ‘