none
Setting connection string in Class library for Datasets RRS feed

  • Question

  • Hi,

    I've made a class library that's doing my backend work for my ASP.NET site.  I can successfully get the connection string that I want the class library to use (using the Configuration manager) but I can't see a nice way of setting this for the dataset (and therefore the table adaptors in it).  I could set the connection string every time I create a Table adaptor but is there not a way of setting the connection string that all the table adapters get when they are created. The connection string I used at design time was the development server so will obviously be wrong on the production system.

    Any help would be very grateful.
    Wednesday, February 10, 2010 9:56 AM

Answers

  • Hello Ross,

     

    Welcome to MSDN Forums!

     

    Based on your description, I think you are using typed dataset, right?   By default, the typed dataset retrieve the connection string from the .config file via Application Settings.   Through this way, every application can only access its top-level .config file.   So the class library will also reads our application’s .config file instead of its own .config file.   If the certain connection string node cannot be found, the Application Settings will use the default value:

    =================================================================================

            [global::System.Configuration.ApplicationScopedSettingAttribute()]

            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

            [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]

            [global::System.Configuration.DefaultSettingValueAttribute("Data Source=localhost;Initial Catalog=Test;Persist Security Info" +

                "=True;User ID=UserName;Password=123456")]

            public string TestConnectionString {

                get {

                    return ((string)(this["TestConnectionString"]));

                }

            }

    =================================================================================

     

    In your post, you mentioned that you are using ConfigurationManager to retrieve the connection string.  Here, are you calling ConfigurationManager.OpenExeConfiguration method to open a specific .config file (may be other than the application’s .config file)?   If so, I recommend you edit the typed dataset’s design file to manually set the connection string for every Table Adapters in the InitConnection() method:

    =================================================================================

            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

            private void InitConnection() {

                this._connection = new global::System.Data.SqlClient.SqlConnection();

                this._connection.ConnectionString =

            }

    =================================================================================

     

    For both the development and deployment environments, we can use this method.  The only difference is the connection string in the class library’s .config file.

     

    Does this method work for you?  

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, February 11, 2010 2:15 AM
    Moderator

All replies

  • Hello Ross,

     

    Welcome to MSDN Forums!

     

    Based on your description, I think you are using typed dataset, right?   By default, the typed dataset retrieve the connection string from the .config file via Application Settings.   Through this way, every application can only access its top-level .config file.   So the class library will also reads our application’s .config file instead of its own .config file.   If the certain connection string node cannot be found, the Application Settings will use the default value:

    =================================================================================

            [global::System.Configuration.ApplicationScopedSettingAttribute()]

            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

            [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]

            [global::System.Configuration.DefaultSettingValueAttribute("Data Source=localhost;Initial Catalog=Test;Persist Security Info" +

                "=True;User ID=UserName;Password=123456")]

            public string TestConnectionString {

                get {

                    return ((string)(this["TestConnectionString"]));

                }

            }

    =================================================================================

     

    In your post, you mentioned that you are using ConfigurationManager to retrieve the connection string.  Here, are you calling ConfigurationManager.OpenExeConfiguration method to open a specific .config file (may be other than the application’s .config file)?   If so, I recommend you edit the typed dataset’s design file to manually set the connection string for every Table Adapters in the InitConnection() method:

    =================================================================================

            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

            private void InitConnection() {

                this._connection = new global::System.Data.SqlClient.SqlConnection();

                this._connection.ConnectionString =

            }

    =================================================================================

     

    For both the development and deployment environments, we can use this method.  The only difference is the connection string in the class library’s .config file.

     

    Does this method work for you?  

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, February 11, 2010 2:15 AM
    Moderator
  • Hi Ross,

     

    I am writing to check the status of the issue on your side.  Would you mind letting me know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, February 15, 2010 1:38 AM
    Moderator