Results 1 to 4 of 4

Thread: Is This Ciode Correct?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2021
    Posts
    46

    Is This Ciode Correct?

    Hi Guys,

    I don't know if anyone here is fluent in both C# and VB.Net. If so I would appreciate a spot check of this code. It was copied from a MS Web Page found herehttps://docs.microsoft.com/en-us/dot...roviderfactory and then converted to VB.Net using an online conversion tool. I don't know how reliable those are and I could not find VB.Net examples. If the code is reliable my intention is to use it to modify some existing VB.Net code that is SQL Server specific into a more Factory based generic version. Thanks In Advance.
    Code:
        ' Retrieve a connection string by specifying the providerName.
        ' Assumes one connection string per provider in the config file.
        Private Shared Function GetConnectionStringByProvider(ByVal providerName As String) As String
    
            ' Return null on failure.
            Dim returnValue As String = Nothing
            ' Get the collection of connection strings.
            Dim settings As ConnectionStringSettingsCollection = ConfigurationManager.ConnectionStrings
            ' Walk through the collection and return the first
            ' connection string matching the providerName.
            If (Not (settings) Is Nothing) Then
                For Each cs As ConnectionStringSettings In settings
                    If (cs.ProviderName = providerName) Then
                        returnValue = cs.ConnectionString
                    End If
                    Exit For
                Next
            End If
            Return returnValue
    
        End Function
        ' Given a provider name and connection string,
        ' create the DbProviderFactory and DbConnection.
        ' Returns a DbConnection on success; null on failure.
        Private Shared Function CreateDbConnection(ByVal providerName As String, ByVal connectionString As String) As DbConnection
            ' Assume failure.
            Dim connection As DbConnection = Nothing
            ' Create the DbProviderFactory and DbConnection.
            If (Not (connectionString) Is Nothing) Then
                Try
                    Dim factory As DbProviderFactory = DbProviderFactories.GetFactory(providerName)
                    connection = factory.CreateConnection
                    connection.ConnectionString = connectionString
                Catch ex As Exception
                    ' Set the connection to null if it was created.
                    If (Not (connection) Is Nothing) Then
                        connection = Nothing
                    End If
    
                    Console.WriteLine(ex.Message)
                End Try
            End If
            ' Return the connection.
            Return connection
    
        End Function
        ' This example assumes a reference to System.Data.Common.
        Private Shared Function GetProviderFactoryClasses() As DataTable
            ' Retrieve the installed providers and factories.
            Dim table As DataTable = DbProviderFactories.GetFactoryClasses
            ' Display each row and column value.
            For Each row As DataRow In table.Rows
                For Each column As DataColumn In table.Columns
                    Console.WriteLine(row(column))
                Next
            Next
            Return table
        End Function

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Is This Ciode Correct?

    The Microsoft site allows you to pick a language (C# or VB, and sometimes others) via a drop-down at the top-right of the page.

    In this case, if you select VB on that page the examples are:
    Code:
    ' This example assumes a reference to System.Data.Common.
    Private Shared Function GetProviderFactoryClasses() As DataTable
    
        ' Retrieve the installed providers and factories.
        Dim table As DataTable = DbProviderFactories.GetFactoryClasses()
    
        ' Display each row and column value.
        Dim row As DataRow
        Dim column As DataColumn
        For Each row In table.Rows
            For Each column In table.Columns
                Console.WriteLine(row(column))
            Next
        Next
    
        Return table
    End Function
    Code:
    ' Retrieve a connection string by specifying the providerName.
    ' Assumes one connection string per provider in the config file.
    Private Shared Function GetConnectionStringByProvider( _
        ByVal providerName As String) As String
    
        'Return Nothing on failure.
        Dim returnValue As String = Nothing
    
        ' Get the collection of connection strings.
        Dim settings As ConnectionStringSettingsCollection = _
            ConfigurationManager.ConnectionStrings
    
        ' Walk through the collection and return the first 
        ' connection string matching the providerName.
        If Not settings Is Nothing Then
            For Each cs As ConnectionStringSettings In settings
                If cs.ProviderName = providerName Then
                    returnValue = cs.ConnectionString
                    Exit For
                End If
            Next
        End If
    
        Return returnValue
    End Function
    Code:
    ' Given a provider, create a DbProviderFactory and DbConnection.
    ' Returns a DbConnection on success; Nothing on failure.
    Private Shared Function CreateDbConnection( _
        ByVal providerName As String, ByVal connectionString As String) _
        As DbConnection
    
        ' Assume failure.
        Dim connection As DbConnection = Nothing
    
        ' Create the DbProviderFactory and DbConnection.
        If Not connectionString Is Nothing Then
            Try
                Dim factory As DbProviderFactory = _
                   DbProviderFactories.GetFactory(providerName)
    
                connection = factory.CreateConnection()
                connection.ConnectionString = connectionString
    
            Catch ex As Exception
                ' Set the connection to Nothing if it was created.
                If Not connection Is Nothing Then
                    connection = Nothing
                End If
                Console.WriteLine(ex.Message)
            End Try
        End If
    
        ' Return the connection.
        Return connection
    End Function
    ...which in this case are near enough identical to what you came up with.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Is This Ciode Correct?

    Interesting thing about both of those. They both produced a variant of this line:

    If Not connectionString Is Nothing Then

    Which is an awkward way of writing:

    If connectionString IsNot Nothing Then
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2021
    Posts
    46

    Re: Is This Ciode Correct?

    Thanks for that. I totally missed the drop down. Was starting to wonder why I could never find VB examples on the MS web pages. Am not fond of C in any form and have NO intention of coding in it. I just went with their examples. Hopefully I will be able to tweak the code and achieve my goal. The code I'm trying to apply this to looks like it will make a good basis for a DAL for my media database. I'm using SQL Server but I thought it would be nice to build in some extra functionality if possible.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width