Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Tip/Trick

Connecting Windows Form Application With ADO.NET in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (14 votes)
2 Dec 2012CPOL3 min read 137.7K   14   12
Connecting Windows Form Application With ADO.NET in C#

Introduction

When you work with database applications, it always needs to connect your application with a database like MSSQL or ORACLE or any others. But for beginners, it's hard to make database connection with the application. Once I faced this problem. So for beginners, now I am going to show you how to connect your application with MSSQL express edition using ADO.NET.

Using the Code

At first, create a winform project. Then go the View menu and click Server Explorer. Right click on the label named Data Connection and click on Add Connection

Image 1

A new window will open. Select Microsoft SQL Server and click continue.

Image 2

Then another new window will open. Write your server name on the first red rectangle area. Server name will be yourPCname\sqlexpress. Here my PC name is bikashpc. You can get your server name by clicking on the Servers node in Server Explorer window. If there is only one server, then there is no confusion.

Then give a name to your database on the second red rectangle area. Here, I give my database name bksdb. And then click ok.

Image 3

Your application is now connected with the database. Let's go test it.
Let's create a form like this:

Image 4

The task is you will put your friend's roll number in the text box and by clicking Show Name button you will get the name of that friend in the name text box. So let's create a table for storing friends' name and roll number.
To create a table, go the Server Explorer and click on the + at the left side of your newly created database. It will expand and many other nodes will be shown including Tables,Views and so forth. Right click on the table and click Add New Table.

Image 5

Then a new tab will open in which you have to put your column name and data type. Write and save table by right clicking on the tab like this:

Image 6

Here I add two columns, name and roll and name my table student. Let's put some data manually in the table. To do that, click the + node of the left side of the table. You can see your table here. Right click on your table name and click Show Table Data.

Image 7

Add some name and roll.

Image 8

Sometimes, you need to change your table definitions: changing column name, adding column, changing data type and so on. To do that again, right click on your table name and click Open Table Definition.

Image 9

Then modify what you want.
Let's write some program now for showing data into the text box. Double click on the button Show Name. In the code file, it will look like this:

C#
private void button1_Click(object sender, EventArgs e)
        {
        }

Add namespace for sqlclient on the top of the code file writing the code below:

C#
using System.Data.SqlClient; 

Now you have to connect your application by writing some code. In the button action, write:

C#
private void button1_Click(object sender, EventArgs e)
        {
           string connectionString = @"Data Source=bikashpc\sqlexpress;Initial Catalog=bksdb;     
                                                            Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connectionString);
        }

Your connection is complete.
The question is how can you get your connection string. Let's find out your Connectionstring. Again, right click in your created database and click on the Properties:

Image 10

Properties window will open.

Image 11

Copy your Data Source and paste it into Data Source of connectionString. So you can now go to fetch data from your database. To do that, you have to open your connection by writing:

C#
sqlCon.Open();

Remember each time you open your connection, you have to close it by writing:

C#
sqlCon.Close();

So let's add some code to your button action.

C#
private void button1_Click(object sender, EventArgs e)
        {
           string connectionString = @"Data Source=bikashpc\sqlexpress;Initial Catalog=bksdb;    
                                                       Integrated Security=True";
          SqlConnection sqlCon = new SqlConnection(connectionString);
            sqlCon.Open();
          string commandString = "select name from student where roll='" + textBox1.Text + "'";
            SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);            
            SqlDataReader read = sqlCmd.ExecuteReader();           
            while (read.Read())
            {
                textBox2.Text = read["name"].ToString(); // it will show your friend's name 
            }
          sqlCon.Close();
        }

Colored portion is used to fetch data from your database and show it in the texbox. Run your program and write a roll number in the textbox and then click the Show Name button. Wow!!!! What you see is your friend's name shown in the name texbox.

Image 12

This is all to inform you. I think it will help you a lot.

Have fun with C#.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Bangladesh Bangladesh
Bikash Karmokar is a software engineer who studied in computer science and engineering in Khulna University of Engineering and Technology, Bangladesh.
He is working on ASP.NET(MVC,C#),CSS3,HTML5, jQuery, Javascript and MSSQL 2008/12.
He loves microsoft technology and want to stay with the latest release of microsoft's products regarding programming.

"bikashkarmokar.wordpress.com" : This is his personal blog. He writes here whenever gets some time.

Comments and Discussions

 
QuestionConnecting windows Form pplication with ADO.NET in C# Pin
Jerofad5-Nov-13 9:41
Jerofad5-Nov-13 9:41 
QuestionMy vote of 5 Pin
nigina18-May-13 1:30
nigina18-May-13 1:30 
AnswerRe: My vote of 5 Pin
Bikash Karmokar21-May-13 5:37
Bikash Karmokar21-May-13 5:37 
GeneralMy vote of 5 Pin
sajjad nawaz13-Mar-13 2:09
sajjad nawaz13-Mar-13 2:09 
GeneralRe: My vote of 5 Pin
Bikash Karmokar13-Mar-13 3:20
Bikash Karmokar13-Mar-13 3:20 
GeneralMy vote of 5 Pin
mitaljoshi20-Jan-13 1:02
mitaljoshi20-Jan-13 1:02 
GeneralRe: My vote of 5 Pin
Bikash Karmokar20-Jan-13 16:15
Bikash Karmokar20-Jan-13 16:15 
GeneralMy vote of 1 Pin
Selvin2-Dec-12 22:35
Selvin2-Dec-12 22:35 
GeneralMy vote of 1 Pin
Wan--Vevi2-Dec-12 2:44
Wan--Vevi2-Dec-12 2:44 
GeneralMy vote of 5 Pin
Prokash Karmokar2-Dec-12 1:45
Prokash Karmokar2-Dec-12 1:45 
easily understandable for the imges
GeneralRe: My vote of 5 Pin
J4$PER2-Dec-12 2:13
J4$PER2-Dec-12 2:13 
QuestionMy vote of 5 Pin
Prokash Karmokar2-Dec-12 1:38
Prokash Karmokar2-Dec-12 1:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.