Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, usually I put the connection string in App.config and call it to create connection which is usually static.

But what if I want to make this connection string dynamically changeable, so that when I put the application on another computer I just use a form to input database information or call them from from a class or even file, to make it changeable easy so what is the best way to do that, and how to get the connection string after doing that.
Posted

The idea behind storing in a file is also just that.
You only need to change configuration in the app file and your connection will work against another database.
 
Share this answer
 
This code in my project I defined a method for send command
You can analyse this method

C#
public static bool SendCommand(string IP, string DatabaseName, string UserName, string Password, string SqlQueryScript)
       {
           bool result = false;
           try
           {
           SqlConnection connection = new SqlConnection("Data Source=" + IP + ";Initial Catalog=" + DatabaseName + ";User ID=" + UserName + ";Password=" + Password);
           string[] commandTextArray = SqlQueryScript.Split(new string[] { "GO" }, StringSplitOptions.RemoveEmptyEntries);
           connection.Open();
           foreach (string commandText in commandTextArray)
           {
               if (commandText.Trim() == string.Empty) continue;
               SqlCommand command = new SqlCommand(commandText, connection);
               command.ExecuteNonQuery();
               connection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage);
           }
           connection.Close();


           result = true;
            }
           catch(Exception ex)
           {

               ErrorMessage.errorMessage = ex.Message + "\n (" + DateTime.Now.ToLongTimeString()+")";
           }
           return result;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900