My Blog List

Saturday, September 3, 2011

How To connect Visual Studio + Microsoft SQL Server

Hi fellows , i m using Microsoft visual studio 2008 vs Microsoft SQL server 2005 database . I think this will help you lot :) :)

So first of all i will show you how to connect database to stand alone application. ( not web application). here are some steps to consider when you designing the application. You must have a proper interface to the application.
The basic steps to designing an interface as follows.


    1. Operations on DataGridView
    2. Data Grid View
    3. Using Meaningful names to user Controllers
In fact , you can use  any tools in V.S 2008 and create your own interface.

After design the interface , now you have to get the database connection. Before connect the database you must create some tables and insert some records. (Ex:- a bank system - you may have more tables like Bank, Branch , Accounts, Account Type etc.) . Then only you can create interface or  application to insert data, delete data, update data and so on.
 When we come again to connecting the database ( assume we already have create tables on MS SQL Server with records.) there are some steps to follows.


    1. Importing App.config file - You can create new file called App.config
    2. Implementing Connection String Inside the file.- how to implement the C.S ??
    3. Creating new class Connection Manager - what is this Class used for ??
    4. Importing references System.Configuration - What is this references??
    5. Create new SQL connection. - how to create new sql connection.??

First of all you must create App.config file ,

Right click on project name in solution explore , Add , New Item


Then select Application Configuration file and clicl Add.


Open the App.config file. The Database connection string can be mentioned here.

     ConnectionManager class

To access database objects through our program we need to have a connection to the database from the program. In order to do this a new class called ConfigurationManager will be created. The purpose of having a separate class for this is to develop the project in a structured manner, which will help avoid having to develop the same code in multiple places of the program.


Right click on project name in solution explore , Add , New Item , Class


     To use SQL connection in .Net, it is required to import some Reference classes.

To do this, right click on the project file  Add Reference… 
Right click on project name in solution explore , Add Reference




      Also some namespaces needs to be imported in this class.

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



1.      Implement ConnectionManager class.

A sql connection will be created in ConnectionManager class, and a connection created in GetConnection method will be returned

     Implement DBAccess class.

Class constructor is modified, in a way that a database connection will be created when an object of DBAcess class is created in the Form.
Notice that GetConnection method is used here, which is a static method to establish the connection.

In this class methods will be implemented to access database and return them to the form.

To obtain data from the database through DBAccess class, it is required to create an object of DBAccess class, in the Form class.




2 comments: