How to get connection string from app.config in C#
By FoxLearn 11/13/2024 7:18:23 AM 9
To retrieve a connection string from the app.config file in C#, you can use the ConfigurationManager class.
Make sure that your project references the System.Configuration
assembly.
You can add System.Configuration
to your project by right-clicking the "References" node in Solution Explorer and selecting "Add Reference...". Then, choose System.Configuration
.
In your app.config
file, define a connection string inside the <connectionStrings>
element.
For example:
<configuration> <connectionStrings> <add name="cn" connectionString="Server=.;Database=myDb;User Id=sa;Password=123@qaz;" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration>
Use ConfigurationManager.ConnectionStrings
to access the connection string.
string connectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
Ensure that the connection string name in the ConfigurationManager.ConnectionStrings["cn"]
matches the name attribute in the app.config
.
- How to capture image from webcam in C#
- How to restart program in C#
- How to call SQL Function in C#
- How to convert GUID to Integer in C#
- Curly Braces in string.Format in C#
- How to get process handle from process name in C#
- How to check the size of HTTP resource in C#
- How to Convert long date to short date in C#
Categories
Popular Posts
Simple Responsive Login Page
11/11/2024
Responsive Animated Login Form
11/11/2024
tsParticles Authentication Template
11/11/2024
Free Responsive HTML5 & CSS3 Login Page
11/11/2024
Bootstrap 4 Login Page Template
11/11/2024