Variables class with ConnectionStrings-VBForums
Results 1 to 2 of 2

Thread: Variables class with ConnectionStrings

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Location
    San Marcos, TX
    Posts
    177

    Variables class with ConnectionStrings

    I have 4 connection string, 1 for LocalDB, MySQL, SQL and SQLite. I also have a variables class, I want to be able to pull the connection strings from the applications local settings and use the DbConnectionStringBuilder to get specific data from those connection strings and store them in different variables that I can use in other places/classes of my application. I'm still trying to learn class usage, so this might be really jumping the gun in my learning process. If you can suggest a better structure on how I should do this please let me know.

    Here's what I have so far.

    Code:
    public class variables
    {
        private static System.Data.Common.DbConnectionStringBuilder bldrLocalDB = new System.Data.Common.DbConnectionStringBuilder();
    
        public static System.Data.Common.DbConnectionStringBuilder dbcsbLocalDB
        {
            get { return dbcsbLocalDB.ConnectionString.ToString(); }
            set { dbcsbLocalDB.ConnectionString = Properties.Settings.Default.csLocalDB; }
        }
        public static string csLocalDB
        {
            get { return bldrLocalDB.ToString(); }
        }
        public static string dsLocalDB
        {
            get { return bldrLocalDB["Data Source"].ToString(); }
        }
        public static string dbLocalDB
        {
            get { return bldrLocalDB["Initial Catalog"].ToString(); }
        }
        public static string unLocalDB
        {
            get { return bldrLocalDB["User ID"].ToString(); }
        }
        public static string pwLocalDB
        {
            get { return bldrLocalDB["Password"].ToString(); }
        }
    }
    Currently the line "get { return dbcsbLocalDB.ConnectionString.ToString(); }" gives me the error "Cannot implicitly convert type 'string' to 'System.Data.Common.DbConnectionStringBuilder'".

    Am I even going about this in the right way?

    Thanks!

  2. #2
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: Variables class with ConnectionStrings

    You get the error because you delcared the property System.Data.Common.DbConnectionStringBuilder. This would say that is returns and takes a DbConnectionStringBuilder object. Yet you try to convert it to a String object, which it is not.

    I suppose I don't understand the use of the class itself. It seems like a wrapper that isn't needed. You could just as easily use a Dictionary<string, DbConnection>. This way you could do like so:

    Code:
    //...
    var dbConnections = new Dictionary<string, DbConnection>();
    
    dbConnections.Add("LocalDb", new SqlConnection("myconnectionstring");
    
    DbConnection localConnection = dbConnections["LocalDb"];
    
    //OR
    
    dbConnections["LocalDb"].Open();
    
    var cmd = new SqlCommand("Select * FROM Mytable", dbConnections["LocalDb"]);
    
    //.....logic.....
    
    dbConnections["LocalDb"].Close();
    
    //...
    Seems a little easier. Also, is there any perticular reason for the user of DbConnectionStringBuidler?
    Last edited by wakawaka; Dec 10th, 2012 at 05:50 PM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.