【问题标题】:SMO and Sproc Class GenerationSMO 和 Sproc 类生成
【发布时间】:2013-12-31 14:46:57
【问题描述】:

我正在尝试为自己构建一个 DAL 生成器。虽然大部分都完美无缺,但我在存储过程中有点卡住了。

在我的数据库中,我有一组简单的选择语句的存储过程,例如:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我遇到的问题似乎几乎与权限相关......但是,我确保为我连接到数据库的用户提供 dbo、所有者、读取、写入访问权限所有模式、过程、视图和表。由于它是系统存储过程,我是否有可能无法简单地访问此过程?我遇到了一个奇怪的错误...Cannot find procedure 'Details.Page' 确实存在...
    • 抓权限的东西...我刚刚通过 SMSS 登录到数据库,使用相同的用户/密码并且 proc 运行,所以不是它...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多