シノニムの使用

シノニムは、スキーマ スコープ オブジェクトの別名です。SMO では、シノニムは Synonym オブジェクトで表現します。Synonym オブジェクトは、Database オブジェクトの子です。これは、シノニムは、そのシノニムが定義されているデータベースのスコープ内でのみ有効であることを意味しています。ただし、シノニムは、別のデータベース上または SQL Server のリモート インスタンス上のオブジェクトを参照することができます。

別名を持つオブジェクトは、ベース オブジェクトと呼ばれます。Synonym オブジェクトの名前プロパティは、ベース オブジェクトの別名です。

次のコード例では、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。詳細については、「Visual Studio .NET で Visual Basic SMO プロジェクトを作成する方法」および「Visual Studio .NET で Visual C# SMO プロジェクトを作成する方法」を参照してください。

Visual Basic でのシノニムの作成

このコード例では、シノニム、またはスキーマ スコープ オブジェクトの別名を作成する方法を示します。クライアント アプリケーションは、マルチパート名を使用してベース オブジェクトを参照するのではなく、シノニムを使用することによって、ベース オブジェクトの単一参照を使用することができます。

'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()

Visual C# でのシノニムの作成

このコード例では、シノニム、またはスキーマ スコープ オブジェクトの別名を作成する方法を示します。クライアント アプリケーションは、マルチパート名を使用してベース オブジェクトを参照するのではなく、シノニムを使用することによって、ベース オブジェクトの単一参照を使用することができます。

{ 
//Connect to the local, default instance of SQL Server. 
   Server srv = default(Server); 
   srv = new Server(); 
   //Reference the AdventureWorks database. 
   Database db = default(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. 
   Synonym syn = default(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(); 
}