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

Assignment Help:

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.


Related Discussions:- How do we connect to sql server ?

Different types of triggers in sql, What are the different types of trigger...

What are the different types of triggers in SQl There are 2 types of triggers in the SQL server 2000 :- 1) INSTEAD OF triggers: The INSTEAD OF triggers fire in place of

Calculating the sum of digits - c# program, Calculating the Sum of Digits -...

Calculating the Sum of Digits - C# Program Hi, Can any of you, tell me how can i calculate the sum of digits of a given number?

Mention different types of data providers available in .net, Mention differ...

Mention different types of data providers available in .NET Framework. .NET Framework Data Provider for SQL Server - Give access to Microsoft SQL Server 7.0 or later version. I

Implementing page fragment caching, How will you implement Page Fragment Ca...

How will you implement Page Fragment Caching? The Page fragment caching includes the caching of a fragment of the page, rather than the whole page. When the parts of the page a

Create and populate a checkboxlist control, Question: (a) The ObjectDa...

Question: (a) The ObjectDataSource control is an alternative design approach to using AccessDataSource or SqlDataSource controls. Explain and illustrate how an ObjectDataSourc

Service oriented architecture, What is Service Oriented architecture? T...

What is Service Oriented architecture? The "Services" are the components which expose well defined interfaces and these interfaces communicate through XML messages. By Using th

Benefits and limitation of viewstate for state management, What are benefit...

What are benefits and Limitation of using Viewstate for state management The benefits of using Viewstate are as follows:- 1)    There is no server resources required because

what is ildasm, ILDASM When it comes to the understanding of the inter...

ILDASM When it comes to the understanding of the internals nothing can beat ILDASM. ILDASM  converts all the exe or dll into IL code. To run ILDASM you have to go to "C:\Progra

Difference in stored procedure and user defined function, What is the diffe...

What is the difference between Stored Procedure (SP) and User Defined Function (UDF)? Some of major differences between a stored procedure and user defined are as follows:-

I want simple code for outlook, I want simple code for Outlook (express,200...

I want simple code for Outlook (express,2003,2007,2010,2013) Add-In with functionality to routinely send CC to the specified email(s), this system should have -Code for every th

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd