2014-01-06 43 views
1

如何使用SMO創建用戶定義的類型(例如:Point(X int,Y int)? 我使用VB.NET(或C#.NET)並希望使用SMO創建? 知道必須SMO使用「UserDefinedType」但我不知道如何? 感謝在SMO中創建用戶定義的類型

回答

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 

隨意如果需要的話要問。

+0

嗨,我的朋友。感謝您的答案,但一個問題:此代碼是爲創建一個用戶定義的表類型。我想用SMO在SQL Server中創建一個用戶定義的類型。非常感謝。 – user3164943

相關問題