IN THIS TASK
In this article, connections are set up both programmatically and using the Visual Studio .NET Server Explorer. The code samples in this article use the SqlConnection, SqlCommand, and SqlDataReader ADO.NET objects.
- Microsoft SQL Server
- Visual Basic .NET
This article assumes that you are familiar with the following topics:
- ADO.NET concepts
- SQL Server concepts and Transact-SQL (T-SQL) syntax
- Northwind sample database
- Start Visual Studio .NET, and create a new Visual Basic Windows Application project named SQLDataAccess.
- Open Form1. In the first line of Form1.vb, add a reference to the ADO.NET namespace as follows:
- From the Windows Start menu, point to Programs, point to Microsoft SQL Server, and then click SQL Server Service Manager to ensure that the SQL Server service is running on your computer.
- Set the Server property to the name of your computer, and set the Services property to MSSQLServer.
- If the service is not running, click Start.
- Close the SQL Server Service Manager dialog box.
Public Class Form1
'Create ADO.NET objects.
Private myConn As SqlConnection
Private myCmd As SqlCommand
Private myReader As SqlDataReader
Private results As String
- To set up the connection string of the SqlConnection object, add the following code to the Form1_Load event procedure:
'Create a Connection object.
myConn = New SqlConnection("Initial Catalog=Northwind;" & _
"Data Source=localhost;Integrated Security=SSPI;")
- To set up the Command object, which contains the SQL query, add the following code to the Form1_Load event procedure:
'Create a Command object.
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT FirstName, LastName FROM Employees"
'Open the connection.
- Add the following code to the Form1_Load event procedure:
myReader = myCmd.ExecuteReader()
- When the myCmd.ExecuteReader method is executed, SqlCommand retrieves two fields from the Employees table and creates a SqlDataReader object.
- To display the query results, add the following code to the
Form1_Load event procedure:The myReader.Read method returns a boolean value, which indicates whether there are more records to be read. The results of the SQL query are displayed in a message box.
'Concatenate the query result into a string.
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab & _
myReader.GetString(1) & vbLf
- To close the SqlDataReader and SqlConnection objects, add the following code to the Form1_Load event procedure:
'Close the reader and the database connection.
- Save and run the project.
- On the View menu, click Server Explorer.
- Right-click Data Connections, and then click Add connection.
- In the Data Link Properties dialog box, click localhost in the Select or enter a server namebox.
- Click Windows NT Integrated Security to log on to the server.
- Click Select the database on the server, and then select Northwind database from the list.
- Click Test Connection to validate the connection, and then click OK.
- In the Server Explorer, click to expand the Data Connections tree so that the Employees table node expands. The properties of individual fields appear in the Properties window.
- View Form1 in Design view.
- Drag the FirstName and Last Name database fields from Employees table in Server Explorer, and drop these fields onto Form1. A SqlConnection and SqlDataAdapter object are created on the form.
- From the View menu, click Toolbox.
- On the Data tab, drag a DataSet object (DataSet1), and drop it onto the form.
- In the Add Dataset dialog box, click Untyped dataset, and then click OK.
- Insert a line of code before the DataReader and Connection objects are closed in the Form1_Load event procedure. The end of the procedure should appear as follows:
- On the Window Forms tab of the toolbox, drag a DataGrid control, and drop it onto Form1.
- To bind the DataGrid to the DataSet that you created earlier, add the following code to the Form1_Load event procedure before the "myReader.close()" line of code:
- Save and run the project.
ID do Artigo: 308656 - Última Revisão: 23 de mar de 2009 - Revisão: 1