---
title: "Database connectivity in C#"  
description: "In this blog, I’m going to describe how to establish connection with database in C#. There are very easy steps, using that you can get data from your"  
author: "AVADHESH PATEL"  
published: 2013-03-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/462/database-connectivity-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Database connectivity in C#

In this blog, I’m going to describe how to establish connection with [database in C#](https://www.mindstick.com/forum/160512/how-to-retrieve-data-from-the-database-in-c-sharp-using-dataadapter). There are very easy steps, using that you can get data from your database. In C# for [database connectivity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach), C# provides [app.Config file](https://www.mindstick.com/forum/281/how-to-get-connection-string-from-app-config-file-in-vc-sharp). \

##### Database connectivity steps are given below.

Step 1: Open your windows application.

Step 2: Go to solution explorer and add “Application [Configuration File](https://www.mindstick.com/forum/155707/what-is-the-usage-of-asp-dot-net-configuration-file)”. After that you see “App.Config” file are added into [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project).

Step 3: Now open “App.Config” file and write below code into <configuration> tag

```
<configuration>  <appSettings>    <add key="dbconnection" value="data source=.;initial catalog=Demo;integrated security=yes;"/>  </appSettings></configuration>
```

\

**Step 4:**Now, open your .cs page where you [access database](https://www.mindstick.com/forum/385/access-database-datatype-problem). For establishing connection, System.Configuration namespace is required, hence go to solution explorer and [check System](https://www.mindstick.com/forum/145520/how-to-check-system-configuration-in-windows-10).Configuration.dll file’s reference is added in your project or not. If it is added that’s good. If not then [add reference](https://www.mindstick.com/forum/548/how-to-add-reference-to-system-web-optimization) through right click on References in solution explorer and select Add Reference… option. Then select .Net tag and choose System.Configuration or System.Configuration.dll file.

Step 5: Come back on your .cs page and include below namespaces.

```
using System.Configuration;using System.Data;using System.Data.SqlClient;
```

Step 6: Now go to your event blog where you put connection. For demonstration I have [define connection](https://www.mindstick.com/forum/33864/how-to-define-connection-string-for-ms-access-files) into Form1_Load() block as below.

```
private void Form1_Load(object sender, EventArgs e){   using (var con = new SqlConnection(ConfigurationManager.AppSettings["dbconnection"]))     {       if (con.State == ConnectionState.Closed)        con.Open();        var cmd = new SqlCommand("select * from EmpInfo", con);        // put your code here        if (con.State == ConnectionState.Open)                    con.Close();       }  }
```

\

This blog is completed here. I hope you enjoy this.

---

Original Source: https://www.mindstick.com/blog/462/database-connectivity-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
