This section describes how to create a Database Mirroring endpoint in an SMO program by using Microsoft Visual Basic .NET.
The code example demonstrates how to create a Database Mirroring endpoint in SMO. This is necessary before you create a database mirror. Use the IsMirroringEnabled and other properties on the Database object to create a database mirror.
Using Transactions
- 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: - Microsoft.SqlServer.ConnectionInfo.dll - Microsoft.SqlServer.Smo.dll - Microsoft.SqlServer.SqlEnum.dll - Microsoft.SqlServer.SmoEnum.dll 
- 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. 
示例
'Set up a database mirroring endpoint on the server before setting up a database mirror.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define an Endpoint object variable for database mirroring.
Dim ep As Endpoint
ep = New Endpoint(srv, "Mirroring_Endpoint")
ep.ProtocolType = ProtocolType.Tcp
ep.EndpointType = EndpointType.DatabaseMirroring
'Specify the protocol ports.
ep.Protocol.Http.SslPort = 5024
ep.Protocol.Tcp.ListenerPort = 6666
'Specify the role of the payload.
ep.Payload.DatabaseMirroring.ServerMirroringRole = ServerMirroringRole.All
'Create the endpoint on the instance of SQL Server.
ep.Create()
'Start the endpoint.
ep.Start()
Console.WriteLine(ep.EndpointState)