Skip to content

Commit a9965e4

Browse files
authored
Merge pull request #9286 from haywoodsloan/dotnet-freshness-2025-batch1
CI#6370: .NET Framework freshness review - Batch 1
2 parents df7735c + 4a747c8 commit a9965e4

File tree

7 files changed

+256
-268
lines changed

7 files changed

+256
-268
lines changed
Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
---
2-
title: Open SQL database by VB .NET
3-
description: Introduces how to open SQL Server databases by using SQL Server .NET Data Provider with Visual Basic .NET.
4-
ms.date: 05/12/2020
2+
title: Open SQL Database By Using VB .NET
3+
description: Introduces how to open SQL Server databases by using SQL Server .NET Data Provider together with Visual Basic .NET.
4+
ms.date: 07/07/2025
55
ms.topic: how-to
66
ms.custom: sap:Class Library Namespaces
7-
---
8-
# Open SQL Server database by using SQL Server .NET Data Provider with Visual Basic .NET
97

10-
This article provides information about how to open SQL Server databases by using SQL Server .NET Data Provider with Visual Basic .NET.
8+
#customer intent: As a developer, I want to open SQL Server databases by using the SQL Server .NET Data Provider together with Visual Basic .NET so that I can integrate SQL Server with my applications.
9+
---
10+
# Open SQL Server database by using SQL Server .NET Data Provider with Visual Basic .NET
1111

12-
_Original product version:_   Visual Basic .NET
13-
_Original KB number:_   308656
12+
This article provides information about how to open Microsoft SQL Server databases by using SQL Server .NET Data Provider together with Visual Basic .NET. You can use ADO.NET to open a SQL Server database by using the SQL Server .NET Data Provider. ADO.NET gathers all the classes that are required for data handling.
1413

15-
## Summary
14+
The `System.Data.SqlClient` namespace describes a collection of classes that are used to programmatically access a SQL Server data source. You can access ADO classes through the `System.Data.OleDb` namespace to provide support for OLE DB databases.
1615

17-
This article describes how you can use ADO.NET to open a SQL Server database by using the SQL Server .NET data provider. ADO.NET gathers all of the classes that are required for data handling. The `System.Data.SqlClient` namespace describes a collection of classes that are used to programmatically access a SQL Server data source. You can access ADO classes through the `System.Data.OleDb` namespace to provide support for OLE DB databases.
16+
In this article, connections are set up both programmatically and by using the Visual Studio .NET Server Explorer. The code samples in this article use the `SqlConnection`, `SqlCommand`, and `SqlDataReader` ADO.NET objects.
1817

19-
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.
18+
_Applies to:_ Visual Basic .NET
2019

21-
## Requirements
20+
_Original KB number:_ 308656
2221

23-
The following list outlines the required hardware, software, network infrastructure, and service packs that you need:
22+
## Prerequisites
2423

2524
- Microsoft SQL Server
2625
- Visual Basic .NET
2726

28-
> [!NOTE]
29-
> SQL Server and Visual Basic .NET must be installed and running on the same computer. In addition, the user must be able to use Windows Integrated Security to connect to SQL Server.
27+
> [!NOTE]
28+
> SQL Server and Visual Basic .NET must be installed and running on the same computer. Additionally, the user must be able to use Windows Integrated Security to connect to SQL Server.
3029
3130
This article assumes that you're familiar with the following topics:
3231

3332
- ADO.NET concepts
3433
- SQL Server concepts and Transact-SQL (T-SQL) syntax
35-
- *Northwind* sample database
34+
- The _Northwind_ sample database
3635

3736
## Create Visual Basic .NET Windows application
3837

39-
1. Start Visual Studio .NET, and create a new Visual Basic Windows Application project named *SQLDataAccess*.
40-
2. Open Form1. In the first line of *Form1.vb*, add a reference to the ADO.NET namespace as follows:
38+
1. In Visual Studio .NET, create a Visual Basic Windows Application project, and name it _SQLDataAccess_.
39+
2. Open Form1.
40+
3. In the first line of _Form1.vb_, add a reference to the ADO.NET namespace, as follows:
4141

4242
```vb
4343
Imports System.Data.SqlClient
4444
```
4545

46-
3. 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.
47-
4. Set the **Server** property to the name of your computer, and then set the **Services** property to *MSSQLServer*.
48-
5. If the service isn't running, click **Start**.
49-
6. Close the **SQL Server Service Manager** dialog box.
46+
4. To make sure that the SQL Server service is running on your computer, select **Start**, point to **Programs**, point to Microsoft SQL Server, and then select **SQL Server Service Manager**.
47+
5. Set the **Server** property to the name of your computer, and then set the **Services** property to _MSSQLServer_.
48+
6. If the service isn't running, select **Start**.
49+
7. Close the **SQL Server Service Manager** dialog box.
5050

5151
## Create ADO.NET objects
5252

@@ -74,7 +74,7 @@ The `SqlConnection` object establishes a database connection, the `SqlCommand` o
7474
"Data Source=localhost;Integrated Security=SSPI;")
7575
```
7676

77-
2. To set up the `Command` object, which contains the SQL query, add the following code to the `Form1_Load` event procedure:
77+
2. To set up the `Command` object that contains the SQL query, add the following code to the `Form1_Load` event procedure:
7878

7979
```vb
8080
'Create a Command object.
@@ -85,7 +85,7 @@ The `SqlConnection` object establishes a database connection, the `SqlCommand` o
8585
myConn.Open()
8686
```
8787

88-
`SqlConnection` uses your Windows logon details to connect to the *Northwind* database on your computer.
88+
`SqlConnection` uses your Windows logon details to connect to the _Northwind_ database on your computer.
8989

9090
## Use the SqlDataReader object to retrieve data from SQL Server
9191

@@ -95,8 +95,9 @@ The `SqlConnection` object establishes a database connection, the `SqlCommand` o
9595
myReader = myCmd.ExecuteReader()
9696
```
9797

98-
2. When the `myCmd.ExecuteReader` method is executed, `SqlCommand` retrieves two fields from the `Employees` table and creates a `SqlDataReader` object.
99-
3. To display the query results, add the following code to the `Form1_Load` event procedure:
98+
When the `myCmd.ExecuteReader` method runs, `SqlCommand` retrieves two fields from the `Employees` table and creates a `SqlDataReader` object.
99+
100+
2. To display the query results, add the following code to the `Form1_Load` event procedure:
100101

101102
```vb
102103
'Concatenate the query result into a string.
@@ -108,52 +109,53 @@ The `SqlConnection` object establishes a database connection, the `SqlCommand` o
108109
MsgBox(results)
109110
```
110111

111-
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.
112+
The `myReader.Read` method returns a boolean value that indicates whether there are more records to be read. The results of the SQL query are displayed in a message box.
112113

113-
4. To close the `SqlDataReader` and `SqlConnection` objects, add the following code to the `Form1_Load` event procedure:
114+
3. To close the `SqlDataReader` and `SqlConnection` objects, add the following code to the `Form1_Load` event procedure:
114115

115116
```vb
116117
'Close the reader and the database connection.
117118
myReader.Close()
118119
myConn.Close()
119120
```
120121

121-
5. Save and run the project.
122+
4. Save and run the project.
122123

123124
## View database in Server Explorer
124125

125-
1. On the **View** menu, click Server Explorer.
126-
2. Right-click **Data Connections**, and then click **Add connection**.
127-
3. In the **Data Link Properties** dialog box, click localhost in the **Select or enter a server name** box.
128-
4. Click **Windows NT Integrated Security** to log on to the server.
129-
5. Click **Select the database on the server**, and then select *Northwind* database from the list.
130-
6. Click **Test Connection** to validate the connection, and then click **OK**.
131-
7. 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.
126+
1. On the **View** menu, select **Server Explorer**.
127+
2. Right-click **Data Connections**, and then select **Add connection**.
128+
3. In the **Data Link Properties** dialog box, select **localhost** in the **Select or enter a server name** box.
129+
4. To log on to the server, select **Windows NT Integrated Security**.
130+
5. Select **Select the database on the server**, and then select the _Northwind_ database in the list.
131+
6. To validate the connection, select **Test Connection**, and then select **OK**.
132+
7. In the Server Explorer, select to expand the **Data Connections** tree so that the `Employees` table node expands. The properties of individual fields appear in the **Properties** window.
132133

133134
## Use Server Explorer to open SQL Server connection
134135

135136
1. View Form1 in Design view.
136-
2. Drag the **FirstName** and **LastName** database fields from `Employees` table in Server Explorer, and drop these fields onto Form1. A `SqlConnection` and `SqlDataAdapter` object are created on the form.
137-
3. From the **View** menu, click **Toolbox**.
138-
4. On the **Data** tab, drag a `DataSet` object (DataSet1), and drop it onto the form.
139-
5. In the **Add Dataset** dialog box, click **Untyped dataset**, and then click **OK**.
140-
6. 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:
137+
2. From the `Employees` table in Server Explorer, drag and drop the **FirstName** and **LastName** database fields onto Form1. This action creates `SqlConnection` and `SqlDataAdapter` objects on the form.
138+
3. On the **View** menu, select **Toolbox**.
139+
4. On the **Data** tab, drag and drop a `DataSet` object (DataSet1) onto the form.
140+
5. In the **Add Dataset** dialog box, select **Untyped dataset**, and then select **OK**.
141+
6. In the `Form1_Load` event procedure, insert a line of code before the `DataReader` and `Connection` objects are closed to end the procedure, as follows:
141142

142143
```vb
143144
SqlDataAdapter1.Fill(DataSet1, "Employees")
144145
myReader.Close()
145146
myConn.Close()
146147
```
147148

148-
7. On the **Window Forms** tab of the toolbox, drag a DataGrid control, and drop it onto Form1.
149-
8. To bind the DataGrid to the `DataSet` object that you created earlier, add the following code to the `Form1_Load` event procedure before the `myReader.close()` line of code:
149+
7. On the **Window Forms** tab of the toolbox, drag and drop a DataGrid control onto Form1.
150+
8. In the `Form1_Load` event procedure, add the following code before the `myReader.close()` line to bind the DataGrid to the `DataSet` object that you created earlier:
150151

151152
```vb
152153
DataGrid1.SetDataBinding(DataSet1, "Employees")
153154
```
154155

155156
9. Save and run the project.
156157

157-
## References
158+
## Related content
158159

159-
For more information about using ADO.NET, refer to the **Data** section of the **Visual Basic** topic in the Visual Studio .NET Help documentation.
160+
- [ADO.NET](/dotnet/framework/data/adonet/)
161+
- [Accessing data in Visual Basic applications](/dotnet/visual-basic/developing-apps/accessing-data)

0 commit comments

Comments
 (0)