• Javascript
  • Python
  • Go

Setting a Connection String Config Programmatically in .NET

Setting a Connection String Config Programmatically in .NET When working with databases in .NET, one of the most important tasks is to set u...

Setting a Connection String Config Programmatically in .NET

When working with databases in .NET, one of the most important tasks is to set up a connection string. A connection string is a string of information that contains all the necessary details for establishing a connection between your application and the database. In this article, we will discuss how to set a connection string config programmatically in .NET.

Step 1: Create a New Project

To begin, open your Visual Studio and create a new project. Choose the type of project you want to create, for example, a Console Application or a Windows Forms Application.

Step 2: Add a Configuration File

Next, add a new configuration file to your project. Right-click on your project name in the Solution Explorer and select "Add" > "New Item". Choose the "Application Configuration File" option and name it "App.config".

Step 3: Add Connection String

Open the App.config file and add the following code:

<connectionStrings>

<add name="MyConnectionString" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />

</connectionStrings>

In this code, we have added a connection string with the name "MyConnectionString". You can change the name according to your preference. Also, make sure to replace the "ServerName" and "DatabaseName" with your actual server and database names.

Step 4: Access Connection String in Code

Now, we will access this connection string in our code. In your code, add the following line of code to retrieve the connection string:

string conString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Step 5: Modify Connection String at Runtime

One of the advantages of setting a connection string config programmatically is that you can modify it at runtime. For example, if you want to change the server name or database name based on certain conditions, you can do so by modifying the connection string.

To modify the connection string, you can use the Configuration class. Add the following code to your project:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

ConnectionStringSettings conSettings = config.ConnectionStrings.ConnectionStrings["MyConnectionString"];

conSettings.ConnectionString = "Data Source=NewServerName;Initial Catalog=NewDatabaseName;Integrated Security=True;";

config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("connectionStrings");

In this code, we are using the Configuration class to open the configuration file and retrieve the connection string. Then, we are modifying the connection string and saving the changes. Finally, we are refreshing the section to update the connection string in our code.

Step 6: Test the Connection String

To test the connection string, you can use the SqlConnection class. Add the following code to your project:

using (SqlConnection con = new SqlConnection(conString))

{

try

{

con.Open();

Console.WriteLine("Connection Successful!");

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

In this code, we are creating a new SqlConnection object using the connection string we retrieved earlier. Then, we are trying to open the connection and if there is an error, we are displaying the error message.

Step 7: Run the Application

Finally, run your application and you should see the "Connection Successful!" message if the connection string is set up correctly. If there is an error, make sure to check your connection string for any ty

Related Articles