【发布时间】: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