How to: Create a Synonym in Visual Basic .NET
This section describes how to create a synonym by using Visual Basic .NET.
The code example shows how to create a synonym or an alternate name for a schema scoped object. Client applications can use a single reference for the base object via a synonym instead of using a multiple part name to reference the base object.
Creating a Synonym
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.
'Connect to the local, default instance of SQL Server. Dim srv As Server srv = New Server() 'Reference the AdventureWorks database. Dim db As Database db = srv.Databases("AdventureWorks") 'Define a Synonym object variable by supplying the parent database, name, and schema arguments in the constructor. 'The name is also a synonym of the name of the base object. Dim syn As Synonym syn = New Synonym(db, "Shop", "Sales") 'Specify the base object, which is the object on which the synonym is based. syn.BaseDatabase = "AdventureWorks" syn.BaseSchema = "Sales" syn.BaseObject = "Store" syn.BaseServer = srv.Name 'Create the synonym on the instance of SQL Server. syn.Create()