CodeSmith Community
Your Code. Your Way. Faster!

Connection String at start up

Latest post 03-04-2008 7:29 AM by MarChin. 4 replies.
  • 03-03-2008 4:16 AM

    • MarChin
    • Not Ranked
    • Joined on 02-29-2008
    • Posts 3
    • Points 75

    Connection String at start up

    Hi,

     I have a windows app that uses the default .netTiers connection string at startup (netTiersConnectionString), however, as this is connection string in the application I am unable to change this during runtime.  I need to find a way to be able to edit the connection string during runtime and it use it at startup.

     I intended to use a string in the app.settings which works fine for my datasets that are created in .NET itself, but I do not know how to set my connection string for .netTiers look up, for example, my string in app.settings.

     Help would be greatly appriciated!

     Thanks

    Mark

    • Post Points: 35
  • 03-03-2008 5:48 AM In reply to

    • fweeee
    • Top 25 Contributor
    • Joined on 08-08-2007
    • Posts 187
    • Points 4,608

    Re: Connection String at start up

    I'm assuming that you want to dynamically change the database that your windows app is running off at run time. I let users do this in my app - I just had a quick look, and I think the important lines of code are:

                string connString = "server=" + server + ";database=" + database + ";User ID=" + username + ";Password=" + password + ";Integrated Security=SSPI;";
                lock (syncRoot)
                {
                    try
                    {

                        //Add dynamic netTiers provider data to Providers collection
                        NetTiersProvider provider = DecEzy.Data.SqlClient.Utils.AddDataProvider(connName, connString);

                        //change provider to be current data provider
                        ConnectionScope.Current.DataProvider = provider;

    • Post Points: 35
  • 03-03-2008 7:22 AM In reply to

    • MarChin
    • Not Ranked
    • Joined on 02-29-2008
    • Posts 3
    • Points 75

    Re: Connection String at start up

    Hi, thanks for the info, I have tried to implement what you suggest, however I dont have a Data.SqlClient.Utils.AddDataProvider(connName, connString) method, there is no Utils in my generated code? 

    I have inherited this project from someone who left our team and I am finding it very difficult to build the connection string and use it upon the application start up.  I don't think this is uncommon or a big ask so I must be missing something....

    Thanks

     Mark

    • Post Points: 35
  • 03-03-2008 2:38 PM In reply to

    • fweeee
    • Top 25 Contributor
    • Joined on 08-08-2007
    • Posts 187
    • Points 4,608

    Re: Connection String at start up

     So your right. That was actually calling other custom code. Sorry - I was quickly copying and pasting that before I went to bed. Whoops.

     The bit you want is something like:

                 NetTiersProvider provider = null;

                     provider = new SqlNetTiersProvider();
                    NameValueCollection collection = new NameValueCollection();
                    collection.Add("ConnectionString", connectionString);
                    collection.Add("ConnectionStringName", key);
                    collection.Add("UseStoredProcedure", "false");
                    collection.Add("EnableEntityTracking", "false");
                    collection.Add("EntityCreationalFactoryType", "<project name>.Entities.EntityFactory");
                    collection.Add("EnableMethodAuthorization", "false");
                    collection.Add("ProviderInvariantName", "System.Data.SqlClient");

                    provider.Initialize(providerKey, collection);
                    DataRepository.LoadProvider(provider, true);

    • Post Points: 35
  • 03-04-2008 7:29 AM In reply to

    • MarChin
    • Not Ranked
    • Joined on 02-29-2008
    • Posts 3
    • Points 75

    Re: Connection String at start up

    Thanks for that, worked a treat once I had managed to get the data provider in the app early enough during startup before it was trying to connect anywhere!  Now I can read a user definable string in and use that for all my data connections.  Much better Smile

     

    Thanks again,

     Mark

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems