如何使用SMO创建用户定义的类型(例如:Point(X int,Y int)? 我使用VB.NET(或C#.NET)并希望使用SMO进行创建? 我知道必须在SMO中使用“UserDefinedType”但我不知道如何?! 感谢。
答案 0 :(得分:0)
通过使用以下代码示例,您可以使用SMO创建UserDefinedType:
Dim oServer As Microsoft.SqlServer.Management.Smo.Server
Dim oSMOServer As Microsoft.SqlServer.Management.Smo.Server
Dim oSMODatabase As Microsoft.SqlServer.Management.Smo.Database
Dim sUDT As String = "udt_RecordID"
Try
oServer = New Microsoft.SqlServer.Management.Smo.Server()
''Set ServerName
oServer.ConnectionContext.ServerInstance = pServerInstanceName
''Set is Windows Authentication
oServer.ConnectionContext.LoginSecure = pIsWinAuth
''Set Server UserName & Password, If not Windows Authentication
If pIsWinAuth = False Then
oServer.ConnectionContext.Login = pUsername
oServer.ConnectionContext.Password = pPassword
End If
Dim oUDTs = oSMOServer.Databases(pDatabaseName).UserDefinedTableTypes
''Add UserDefinedType if not exist
If Not oUDTs.Contains(sUDT) Then
oSMODatabase = oSMOServer.Databases(pDatabaseName)
Dim udtt As UserDefinedTableType
udtt = New UserDefinedTableType(oSMODatabase, sUDT)
''Add necessary columns
udtt.Columns.Add(New Microsoft.SqlServer.Management.Smo.Column(udtt, "RecordID", DataType.NVarChar(50)))
''Create UserDefinedType
udtt.Create()
End If
Catch ex As Exception
''Handle Exception
End Try
如果需要,请随时询问。