How do we connect to sql server ?, DOT NET Programming

How do we connect to SQL SERVER, which namespace do we use ?

The code is shown below, after that i will  give the explanation for it. For this sample we will also require a SQL Table setup which I have imported using the DTS wizard.

Private Sub LoadData()

' note :- with and end with makes your code more readable

Dim strConnectionString As String

Dim objConnection As New SqlConnection

Dim objCommand As New SqlCommand Dim objReader As SqlDataReader Try

file.

' this gets the connectionstring from the app.config

' note if this gives error see where the MDB file is

stored in your pc and point to that strConnectionString =

AppSettings.Item("ConnectionString")

' take the connectiostring and initialize the connection

object

Employees")

With objConnection

.ConnectionString = strConnectionString

.Open() End With

objCommand = New SqlCommand("Select FirstName from

With objCommand

.Connection = objConnection objReader = .ExecuteReader()

End With

' looping through the reader to fill the list box

Do While objReader.Read()

lstData.Items.Add(objReader.Item("FirstName")) Loop

Catch ex As Exception

Throw ex

Finally objConnection.Close()

End Try

ID=sa;Database=Employees"/>

For setting up the sample of the SQL table we can use the DTS import wizard to import the table. See the figure below which is using a data source as Microsoft Access.While importing the database author had provide the database name as "Employees".

1685_SQL server.png

Figure:- Loading "Nwind.mdb" in SQL SERVER for the sample

416_SQL server1.png

Figure :- Load only the Employee table

To make it easy we will only import the employee table as that is the only thing required in our sample code.

459_SQL server2.png

Figure :- View of loaded Employee table

Now from the interview point of view definitely you are not going to say the whole source code. The Interviewer expects only the wide answer of what are the steps required to connect to SQL SERVER. For fundamental the whole source code is explained below. In brief you have to describe the "LoadData" method in a broaderway. The steps to connect to SQL SERVER are shown below:-

1)The very First step is to import the namespace "System.Data.SqlClient".

2)The second step is to Create a connection object as shown in "LoadData" method.

With objConnection

.ConnectionString = strConnectionString

.Open() End With

3)The third step is to Create the command object with the SQL. Also assign the created connection object to the command object & execute the reader.

objCommand = New SqlCommand("Select FirstName from Employees") With objCommand

.Connection = objConnection objReader = .ExecuteReader()

            End With

4)Finally loop through the reader & fill the list box. If old VB programmers are expecting the movenext command it is replaced by Read() which returns true if there is any data to be read. If the .Read() return's false which  means that it is the end of datareader and there is no more data to be read.

Do While objReader.Read() lstData.Items.Add(objReader.Item("FirstName")) Loop

5) Do not  neglect  to close the connection object.

Posted Date: 9/24/2012 2:36:42 AM | Location : United States







Related Discussions:- How do we connect to sql server ?, Assignment Help, Ask Question on How do we connect to sql server ?, Get Answer, Expert's Help, How do we connect to sql server ? Discussions

Write discussion on How do we connect to sql server ?
Your posts are moderated
Related Questions
Concept of Boxing and Unboxing The Boxing permits any value type to be implicitly converted to type object or to any other interface type implemented by the value type. The Box

The costumer is an application that tracks production costuming, structure and other related pieces. We discussed this in detail and you have commentary and video to define the pro

HOW TO DESIEN THE CALENDER IN ASP . NET

What's difference between "Optimistic" and "Pessimistic" locking? In the pessimistic locking when user wants to update a data it locks the record and till then no one can updat

What is Language Integrated Query (LINQ)? LINQ is a programming model that is the composition of general-purpose standard query operators that permit you to work with data, reg

Update present website backend (asp.NET) as well as build Rest API to expose data I will soon build an HTML5 cross-platform mobile app. I previously have a website with a backen

What is the difference among synchronous postback and asynchronous postback? The difference between synchronous and asynchronous postback is as follows: Asynchronous postbac

Project Description: Looking for an internal-use, Manufacturing/ ERP Website developed for use at a Paper Mill. Modules/areas would include Customers, Scheduling, Sales Orders,

What are function points? The FPA is breaking huge systems into smaller pieces and analyzing them. The Software application is a combination of set of elementary processes. The

Can you give a practical implementation of FAÇADE patterns? The Façade pattern sits on the top of lot of the subsystems & makes access easy to interfaces of these subsystems. T