In this article, I will explain how to create a one-time connection string in a Console application without using the ConfigurationManager properties or app.config. Suppose you want to create a connection string in the app.config file.
Note that most experienced programmers prefer to use this approach. Use the following procedure.
Step 1
Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click Console Application under Visual C#.
Give the name of your application as "Database_Connection_Application" and then click OK.
Step 2
After Step 1, a new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the properties, App.config file, References and Program.cs file. Properties has only the single node AssemblyInfo.cs as in the following:
Step 3
Just go to the "Properties" node then double-click on "Settings.Settings". You will then get a grid with columns for Name, Type, Scope and Value as in the following:
Step 4
Now, specify a name, then click in the Type cell. In the drop-down box choose "Connection String". Then set the Scope to "Application".
Step 5
Then click in the value cell and an ellipsis ("...") will appear in the right. Click on the ellipsis and create the connection string.
Then in the program, access (use) the connection string using: Properties.Setting.Default.(name) where (name) is the name you provided in the name column.
Step 6
When you are done with all the steps, now again open the properties node and you will see that it contains two nodes, Setting and AssemblyInfo. The Setting node has another one node Setting.Designer.cs. This node has all the information about the data connection.
Example
In this example, we will display a record.
Coding
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Database_Connection_Application
{
class Program
{
static void Main(string[] args)
{
SqlConnection con;
SqlDataReader reader;
try
{
int id;
con = new SqlConnection(Properties.Settings.Default.connectionStr);
con.Open();
Console.WriteLine("Enter Employee Id");
id = int.Parse(Console.ReadLine());
reader = new SqlCommand("select * from EmpSalary_Info where EmpId=" + id, con).ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("EmpID | EmpName | EmpSalary \n {0} | {1} | {2}", reader.GetInt32(0),
reader.GetString(1), reader.GetInt32(2));
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
App.config
You see that the Connection String is automatically included.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Database_Connection_Application.Properties.Settings.connectionStr"
connectionString="Data Source=.;Initial Catalog=EmpDetail;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Output 1
Enter EmpId:
Output 2
Output 3
If we don't enter an EmpId then:
For more information, download the attached sample application.