none
get conection string from other project in solution RRS feed

  • Question

  • HI

    I have a WinForm Solution with two project

    In the first Project I create a DB and Tables and save Connection string with MySettings.

    Now I need to retrive the saved connection string from a form in the second Project

    How can I get the connection string .

    Claudio

    Monday, December 18, 2017 10:00 AM

Answers

  • In short it can be done many ways, here is one.

    These classes can be in the same project or different projects. Note how the connection string is done between the two classes.

    Imports System.Data.SqlClient
    
    Public Class CustomerOperations
        Private mConnectionString As String
        Public ReadOnly Property ConnectionString As String
            Get
                Return mConnectionString
            End Get
        End Property
        Public Sub New(ByVal pConnectionString As String)
            mConnectionString = pConnectionString
        End Sub
        Public Function Read() As DataTable
            Dim dt As New DataTable
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    '
                    ' do work
                    '
                End Using
            End Using
            Return dt
        End Function
        Public Function GetOrders(ByVal pCustomerIdentifier As Integer) As DataTable
            Dim ops As New OrdersOperations(ConnectionString)
            Return ops.GetCustomerOrders(pCustomerIdentifier)
        End Function
    End Class

    Then

    Imports System.Data.SqlClient
    Public Class OrdersOperations
        Private mConnectionString As String
        Public ReadOnly Property ConnectionString As String
            Get
                Return mConnectionString
            End Get
        End Property
        Public Sub New(ByVal pConnectionString As String)
            mConnectionString = pConnectionString
        End Sub
        Public Function GetCustomerOrders(ByVal pCustomerIdentifier As Integer) As DataTable
            Dim dt As New DataTable
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    '
                    ' create SQL SELECT to get orders for customer via pCustomerIdentifier
                    '
                End Using
            End Using
            Return dt
        End Function
    End Class
    

    Of course we can move away from DataTable and or DataSet objects and use classes e.g. a Customer class and Order class etc. or combine both DataTable and or DataSet with classes where we can use single instances of a class or a List(Of Customer) etc. and have sub classes (also known as Data Transfer Objects).

    Connection string could be stored in a class project which any other project can access so it's easy to get at the connection strings. I would not store connection strings in My.Settings but as properties in a class, perhaps

    Public Class SolutionConnections
        Private mAccountingDatabaseConnectionstring As String = "Place connection string here"
        Public ReadOnly Property Accounting As String
            Get
                Return mAccountingDatabaseConnectionstring
            End Get
        End Property
        Private mCustomerDatabaseConnectionString As String = "Place connection string here"
        Public ReadOnly Property Customer As String
            Get
                Return mCustomerDatabaseConnectionString
            End Get
        End Property
    End Class

    Or read them in from say an xml file or comma delimited file

    Public Class SolutionConnections
        Private mAccountingDatabaseConnectionstring As String
        Public ReadOnly Property Accounting As String
            Get
                Return mAccountingDatabaseConnectionstring
            End Get
        End Property
        Private mCustomerDatabaseConnectionString As String
        Public ReadOnly Property Customer As String
            Get
                Return mCustomerDatabaseConnectionString
            End Get
        End Property
        Public Sub New()
            '
            ' read connection strings from a file and set the private variables here
            '
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Monday, December 18, 2017 3:44 PM
    Monday, December 18, 2017 1:51 PM
    Moderator

All replies

  • Did you add project one to project 2 to make from it one solution?

    Success
    Cor

    Monday, December 18, 2017 11:32 AM
  • Yes of course, even the reference 
    Monday, December 18, 2017 11:59 AM
  • The following was done about seven years ago, it was an experiment for replying to a similar question.

    https://1drv.ms/u/s!AtGAgKKpqdWjiQ-moQynJpxlLvYr

    It works for either MS-Access or SQL-Server but with MS-Access it has an issue as the connection string will have a variable in the connection string that will not resolve in this case without more work while with SQL-Server it works just fine granted there is one connection string, if more than one connection string in a project it will still work but you will need to modify the code. Note the OpenDialog IntialPath that is used, modify to suit your PC.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, December 18, 2017 12:14 PM
    Moderator
  • It seems there is a virus inside
    Monday, December 18, 2017 12:31 PM
  • It seems there is a virus inside

    Who are you speaking too? If you are speaking to me, there are no executable files in the zip file plus I use extremely good anti-virus software.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, December 18, 2017 12:37 PM
    Moderator
  • Sorry Karen , I dont know what I did
    Monday, December 18, 2017 1:04 PM
  • Karen give me a suggestione before I start to devolop all forms for The business application (customer, orders, suppliers , invoices and so on)

    Is it  better to have all forms that Interface with user in the same project (Front end) and all Classes to manipulate data in another Project (back end), or

    it is better to have several Project, one for the  customer,including front end and back end , one project for orders (frontend and backend ..) and so on. ?

    My question about passing connection string between Project seems to be strange and rare

    Claudio

    Monday, December 18, 2017 1:15 PM
  • In short it can be done many ways, here is one.

    These classes can be in the same project or different projects. Note how the connection string is done between the two classes.

    Imports System.Data.SqlClient
    
    Public Class CustomerOperations
        Private mConnectionString As String
        Public ReadOnly Property ConnectionString As String
            Get
                Return mConnectionString
            End Get
        End Property
        Public Sub New(ByVal pConnectionString As String)
            mConnectionString = pConnectionString
        End Sub
        Public Function Read() As DataTable
            Dim dt As New DataTable
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    '
                    ' do work
                    '
                End Using
            End Using
            Return dt
        End Function
        Public Function GetOrders(ByVal pCustomerIdentifier As Integer) As DataTable
            Dim ops As New OrdersOperations(ConnectionString)
            Return ops.GetCustomerOrders(pCustomerIdentifier)
        End Function
    End Class

    Then

    Imports System.Data.SqlClient
    Public Class OrdersOperations
        Private mConnectionString As String
        Public ReadOnly Property ConnectionString As String
            Get
                Return mConnectionString
            End Get
        End Property
        Public Sub New(ByVal pConnectionString As String)
            mConnectionString = pConnectionString
        End Sub
        Public Function GetCustomerOrders(ByVal pCustomerIdentifier As Integer) As DataTable
            Dim dt As New DataTable
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    '
                    ' create SQL SELECT to get orders for customer via pCustomerIdentifier
                    '
                End Using
            End Using
            Return dt
        End Function
    End Class
    

    Of course we can move away from DataTable and or DataSet objects and use classes e.g. a Customer class and Order class etc. or combine both DataTable and or DataSet with classes where we can use single instances of a class or a List(Of Customer) etc. and have sub classes (also known as Data Transfer Objects).

    Connection string could be stored in a class project which any other project can access so it's easy to get at the connection strings. I would not store connection strings in My.Settings but as properties in a class, perhaps

    Public Class SolutionConnections
        Private mAccountingDatabaseConnectionstring As String = "Place connection string here"
        Public ReadOnly Property Accounting As String
            Get
                Return mAccountingDatabaseConnectionstring
            End Get
        End Property
        Private mCustomerDatabaseConnectionString As String = "Place connection string here"
        Public ReadOnly Property Customer As String
            Get
                Return mCustomerDatabaseConnectionString
            End Get
        End Property
    End Class

    Or read them in from say an xml file or comma delimited file

    Public Class SolutionConnections
        Private mAccountingDatabaseConnectionstring As String
        Public ReadOnly Property Accounting As String
            Get
                Return mAccountingDatabaseConnectionstring
            End Get
        End Property
        Private mCustomerDatabaseConnectionString As String
        Public ReadOnly Property Customer As String
            Get
                Return mCustomerDatabaseConnectionString
            End Get
        End Property
        Public Sub New()
            '
            ' read connection strings from a file and set the private variables here
            '
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Claudio111 Monday, December 18, 2017 3:44 PM
    Monday, December 18, 2017 1:51 PM
    Moderator
  • Thanks again Karen, problem solved

    Claudio

    Monday, December 18, 2017 3:44 PM
  • Thanks again Karen, problem solved

    Claudio

    Great, happy to hear this :-)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, December 18, 2017 4:10 PM
    Moderator