SMO和Sproc类生成

时间:2013-12-31 14:46:57

标签: .net sql-server vb.net smo

我正在尝试为自己构建一个DAL生成器。虽然大部分时间都很完美,但是当涉及到我的sprocs时,我有点卡住了。

在我的数据库中,我有一组sprocs是简单的select语句,例如:

ALTER PROCEDURE [Details].[Page]

    @ID         BigInt

AS
BEGIN
    SET NOCOUNT ON;

    Select PageID, SiteID, TemplateID, Parent, Updated, Created, Settings, PublishDate, Publish, Page, LinkAlias, Keywords, Description, HomePage, Title
    From Selects.Pages
    Where PageID = @ID

END

我可以通过Imports Microsoft.SqlServer.Management.Smo

获取架构,名称和参数

如何获取程序返回的列?

这是我的代码:

Private Sub GrabProcedures()
    Dim _Db As Database = Srv.Databases(DBName)
    Dim _Procs As ParallelQuery = _Db.StoredProcedures.AsParallel()
    Dim _i As Integer = 0
    For Each proc As StoredProcedure In _Procs
        If Not proc.IsSystemObject Then
            _i += 1

            _Procedures.Add(New ProcedureTyping() With {
                        .ID = _i,
                        .Name = proc.Name,
                        .Schema = proc.Schema,
                        .Parameters = ProcessParameters(proc.Parameters),
                        .Include = True,
                        .GenerateSelect = False})
        End If
    Next
    _SPCount = _Procedures.Count
End Sub

Private Function ProcessParameters(_params As StoredProcedureParameterCollection) As List(Of ParameterTyping)
    Dim _L As New List(Of ParameterTyping)
    Dim _p As Integer = 0
    For Each param As StoredProcedureParameter In _params
        _p += 1
        _L.Add(New ParameterTyping() With {
               .ID = _p,
               .Name = param.Name,
               .Type = param.DataType.SqlDataType,
               .Length = param.DataType.MaximumLength,
               .OutParam = param.IsOutputParameter,
               .DefaultValue = param.DefaultValue})
    Next
    Return _L
End Function

Partial Public Class ProcedureTyping
    Public Property ID As Integer
    Public Property Name As String
    Public Property Schema As String
    Public Property Parameters As List(Of ParameterTyping)
    Public Property Include As Boolean
    Public Property GenerateSelect As Boolean
End Class

Partial Public Class ParameterTyping
    Public Property ID As Integer
    Public Property Name As String
    Public Property Type As SqlDataType
    Public Property Length As Integer
    Public Property OutParam As Boolean
    Public Property DefaultValue As String
End Class

请假设_Db_Procs正确填充(因为它们)。

编辑 - 尝试通过sp_describe_first_result_set

获取Sproc列
Private Function ProcessProcColumns(ByVal _Name As String, ByVal _Schema As String) As List(Of ColumnTyping)
    Dim _sql As String = "Exec sp_describe_first_result_set N'" & If(_Schema.Length > 0, _Schema & ".", "") & _Name & "'"
    Dim _rs As SqlDataReader = Srv.ConnectionContext.ExecuteReader(_sql)
    Dim _i As Integer = 0
    If _rs IsNot Nothing Then
        While _rs.Read()
            _i += 1
            _ProcColumns.Add(New ColumnTyping() With {
                                .ID = _i,
                                .Name = _rs(2),
                                .Type = _rs(5),
                                .Length = _rs(6),
                                .DefaultValue = String.Empty,
                                .Precision = _rs(7),
                                .Scale = _rs(8),
                                .IsPrimary = _rs(27)})
        End While
    End If
End Function

1 个答案:

答案 0 :(得分:1)

SQL Server 2012中的一个新功能是sys.sp_describe_first_result_set,它返回有关第一个结果集的元数据。

我认为任何类似的功能都不会通过SMO公开,但如果您的SP只是简单的SELECT语句,您可以尝试解析TextBody