[ Close ]
Username
Password


Forgotten your password?
 
Custom Search
Computer/IT Projects
 Projects Q A, Guidance for your projects Projects Forum Computer/IT Projects
Message Icon Topic: how to switch connection dynamically Post Reply Post New Topic
Found this useful? Give a +1   Add to Facebook: Add to Facebook
Free Project Downloads
CS / IT Project Downloads
.Net Project Downloads
JAVA Project Downloads
PHP Project Downloads
Networking Proj Downloads
Project Topics
Computer(CS/IT) Projects
Software Mini Project Topics
Android Project Topics
Networking Project Ideas
Dot Net Project Topics
Latest IEEE Projects List
IEEE JAVA Project Topics
Project Presentation Tips
How to choose Project Topic
Tips for Final Year Project
Steps for FYP, Mini Project
Preparing a Project Report
Problems faced in Projects
 
Get Free Software CS / IT Project Downloads:

Enter your email address:  

Author Message
tilu
Newbie
Newbie


Joined: 16-Jul-2012
Online Status: Offline
Posts: 0
Quote tilu Replybullet Topic: how to switch connection dynamically
    Posted: 17-Jul-2012 at 12:19am
how can we switch connection dynamically.how we implement this topic on android platform.

 
IP IP Logged
Aruna
Groupie
Groupie


Joined: 21-Sep-2008
Online Status: Offline
Posts: 0
Quote Aruna Replybullet Posted: 25-Jul-2012 at 5:54pm
You can update the app.config file at runtime with new string settings. The only thing you have to know is the full name of the connection string.

        public static void ChangeConnectionString(string strConn)
        {             
             Configuration _config = ConfigurationManager.OpenExeConfiguration(
                        ConfigurationUserLevel.None);

            //the full name of the connection string can be found in the app.config file
            // in the "name" attribute of the connection string
             _config.ConnectionStrings.ConnectionStrings[FULL NAME OF CONNECTION STRING HERE].ConnectionString = strConn;

            //Save to file
            _config.Save(ConfigurationSaveMode.Modified);
            
            //force changes to take effect so that we can start using
            //this new connection string immediately
             ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name);
            Properties.Settings.Default.Reload();
        }

 
IP IP Logged
fanofchennai
Groupie
Groupie


Joined: 21-Sep-2008
Online Status: Offline
Posts: 0
Quote fanofchennai Replybullet Posted: 25-Jul-2012 at 5:59pm
The below code snippet allows the connection string to be set at runtime if required. Changing the generated code won't work unless you want to change it again every time you use the designer.

Create a factory class to create instances of table adapters. Here is some sample code.

namespace Myapp.Data.MyappDataSetTableAdapters
{
    internal class AdapterFactory
    {
        private string _connectionString = "";

        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        internal MytableTableAdapter GetMytableTableAdapter(string connectionString)
        {
            MytableTableAdapter result = new MytableTableAdapter();
            result.Connection.ConnectionString = connectionString;
            return result;
        }

        internal MytableTableAdapter GetMytableTableAdapter(System.Data.OleDb.OleDbConnection connection)
        {
            MytableTableAdapter result = new MytableTableAdapter();
            result.Connection = connection;
            return result;
        }

        internal MytableTableAdapter GetMytableTableAdapter()
        {
            return GetMytableTableAdapter(_connectionString);
        }
}

In the factory you need to create methods to return each type of table adapter. When the factory creates a new instance of a table adapter that instance will initially get the default connection string but the string will never be used. It is immediately overwritten by the connection string you assign to the adapter factory. The connection string isn't used until the first time you try to access the database using the table adapter.

You can use this with code like:

Myapp.Data.MyappDataSetTableAdapters.AdapterFactory adapterFactory =
new Myapp.Data.MyappDataSetTableAdapters.AdapterFactory();
adapterFactory.ConnectionString = myConnectionString;
Myapp.Data.MyappDataSetTableAdapters.MytableTableAdapter myTableAdapter = adapterFactory.GetMytableTableAdapter();
IP IP Logged

 
Post Reply Post New Topic

Free Programming Courses in JAVA, .Net and PHP (Register Now!)

Java Free Online Course

.Net Free Online Course

PHP Free Online Course


 
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.032 seconds.
© ProjectsQA.com   |   Privacy policy