How to: Create a Visual C# SMO Project in Visual Studio .NET
Mis à jour : 5 décembre 2005
This section describes how to build a simple SMO console application.
This example imports namespaces, which enables the program to reference SMO types. The import of the Agent namespace is optional. The Agent namespace is required only for applications that use SQL Server Agent functionality. The Common namespace is required to establish a secure connection to the instance of SQL Server. The SqlClient namespace is used to debug SQL exception errors.
Creating a Visual C# SMO project in Visual Studio.NET
Start Visual Studio 2005.
On the File menu, click NewProject. The New Project dialog box appears.
In Project Types dialog box, select Visual C# Projects. In the Templates pane, select Windows Application.
(Optional) In Name, type the name of the new application
Select the Visual C# application type. For the examples that follow select Console Application.
On the Project menu, select Add Reference. The Add Reference dialog box appears.
Select Browse, locate the SMO assemblies in the C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies folder, and select the following files:
Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.SmoEnum.dll
Microsoft.SqlServer.SqlEnum.dll
Remarque : Use the Ctrl key to select more than one file. Add the additional SMO assemblies if you are specifically programming Service Broker or WMI:
Microsoft.SqlServer.ServiceBrokerEmum.dll
Microsoft.SqlServer.WmiEmum.dll
Click Open.
On the View menu, click Code.-Or-Select the Program1.cs [Design] Windows and double click the windows form to display the code window.
In the code, before the namespace statement, type the following using statements to qualify the types in the SMO namespace:
using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common;
SMO has various namespaces under Microsoft.SqlServer.Management.Smo, such as Microsoft.SqlServer.Management.Smo.Agent. Add these namespaces as needed.
You can now add your SMO code.