Saturday, July 10, 2021

Create Connection String in C# and VB.Net

Create Connection String in C# and VB.Net

In this article I will explain with an example, how to create Connection String in C# and VB.Net.

This article will illustrate how to create connection string for Windows and SQL Server based authentication for Windows Forms and Console Applications in C# and VB.Net.

SQL Server Connection String for Windows Authentication in App.Config file

SQL Server Connection String for Windows Authentication in App.Config file is defined as follows and it consists of the following properties.

Data Source - The name of the SQL Server and its Instance.

Initial Catalog - The name of the Database.

Integrated Security - By default False. If set true it signifies that Windows Authentication needs to be used.

<configuration>

    <connectionStrings>

        <add name="ConString" connectionString="Data Source=Mudassar-PC\SQL2005;Initial Catalog=Northwind;Integrated Security=true" />

    </connectionStrings>

</configuration>

SQL Server Connection String for SQL Server Authentication in App.Config file

SQL Server Connection String for SQL Server Authentication in App.Config file is defined as follows and it consists of the following properties.

Data Source – The name of the SQL Server and its Instance.

Initial Catalog – The name of the Database.

User Id – The User Id of the SQL Server.

Password – The Password of the SQL Server.

<connectionStrings>

    <add name="ConString" connectionString="Data Source=Mudassar-PC\SQL2005;Initial Catalog=Northwind;User ID=sa;Password=pass1234"/>

</connectionStrings>

Adding System.Configuration reference

In order to access the SQL Server Connection String for and SQL Server Authentication from App.Config file in code behind, the very first thing you need to do is to add reference of the System.Configuration Assembly to the project in the following way.

1. Right click on the project and click Add Reference option from the Context Menu.

Create Connection String in C# and VB.Net
Create Connection String in C# and VB.Net

From the Add Reference dialog box, click on .Net Tab and look for System.Configuration assembly. Once you find it simply select and click OK.

Create Connection String in C# and VB.Net
Create Connection String in C# and VB.Net

Once the reference is added, it will be shown in the References folder of the Solution Explorer.

Namespaces

You will need to import the following namespace.

C#

using System.Configuration;

 

VB.Net

Imports System.Configuration

 

Reading ConnectionString from App.Config file using C# and VB.Net

Once the reference is added, you can read the SQL Server Connection String for Windows Authentication and SQL Server Authentication from the App.Config file in the following way.

C#

string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;

 

VB.Net

Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString


No comments:

Post a Comment

DotNet Latest Technologies

  I'm not sure exactly what you are looking for or how "recent" the technologies that you might wan to look into are, but I...