How to: Connect to the Local Instance of SQL Server by Using Windows Authentication in Visual Basic .NET
This section describes how to write an SMO application in Visual Basic .NET that connects to the local instance of SQL Server by using Windows Authentication.
Connecting to the local instance of SQL Server does not require much code. Instead, it relies on default settings for authentication method and server. The first operation that requires data to be retrieved will cause a connection to be created.
Connecting to a local instance of SQL Server with Windows Authentication
Start Visual Studio 2005.
From the File menu, select New Project. The New Project dialog box appears.
In the Project Types pane, select Visual Basic. In the Templates pane, select Console Application.
(Optional) In the Name box, type the name of the new application.
Click OK to load the Visual Basic console application template.
On the Project menu, select Add Reference item. The Add Reference dialog box appears. Select Browse and locate the SMO assemblies in the C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies folder. Select the following files:
On the View menu, click Code.-Or-Select the Module1.vb window to display the code window.
In the code, before any declarations, type the following Imports statements to qualify the types in the SMO namespace:
Imports Microsoft.SqlServer.Management.Smo Imports Microsoft.SqlServer.Management.Common
Insert the code that follows this procedure into the main program.
Run and build the application.
The example is Visual Basic .NET code that connects to the local instance of SQL Server by using Windows Authentication.
'Connect to the local, default instance of SQL Server. Dim srv As Server srv = New Server 'The connection is established when a property is requested. Console.WriteLine(srv.Information.Version) 'The connection is automatically disconnected when the Server variable goes out of scope.