Classes

Discussion in 'C#' started by kingdomoflegends, Jun 18, 2007.

  1. #1
    Hi

    I hope someone can help me.

    I'm writing a asp system and I'm using mostly classes for my rules, etc... But I would like to know if there is a better way to "declare" my connection string. Typing it out in every class for every "Action" can be a bit messy later on if I want to change my connection string.
     
    kingdomoflegends, Jun 18, 2007 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Classic ASP? .NET?

    Just have an include or have a class that gets inherited by all classes like Config class. Or have it in the Web.Config and get it by ConfigurationManager.AppSettings("ConnStr") or keep it in a registry setting or well.. you get the picture ;)
     
    ccoonen, Jun 18, 2007 IP
  3. PooPoly

    PooPoly Peon

    Messages:
    404
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    declare your Connection String In Your Assimbly file As Internal

    PooPoly
     
    PooPoly, Jun 27, 2007 IP
  4. kingdomoflegends

    kingdomoflegends Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you all for the replies :) I'm using .NET.

    What I've done is, I've made a connection class and in each class just calls the connection class... So far it's working good :) I want too know if you all think what I've done is the correct way?
     
    kingdomoflegends, Jul 10, 2007 IP
  5. yugolancer

    yugolancer Well-Known Member

    Messages:
    320
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Well it is only a matter of taste. For example what i am doing for some of my applications is also creating a class connector ... like following

    Imports System.Data.SqlClient
    
    Public Class Connector
        Public Shared Function GetConnection() As SqlConnection
            Dim connectionString As String _
            = "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
            Return New SqlConnection(connectionString)
       End Function
    End Class
    Code (markup):
    Then you can use the shared func wherever you need ... as it follows:

    Dim connection As SqlConnection = Connector.GetConnection()
    Code (markup):
    HTH
    Regards :)
     
    yugolancer, Jul 10, 2007 IP
  6. kingdomoflegends

    kingdomoflegends Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Cool, thank you, that is more or less what I've done :) So now I know I'm on the right track.
     
    kingdomoflegends, Jul 17, 2007 IP
  7. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Normally would put it in the web.config file as an application setting as these sit constantly in the system memory and saves calling a function every time you want to use it
     
    AstarothSolutions, Jul 18, 2007 IP