我有一种情况,我需要能够返回具有可变列名的可变数量的列。我创建了一个在SSMS中工作的存储过程,但是当我从代码执行它时只返回零。 这是存储过程:
CREATE PROCEDURE [dbo].[uSp_CR_ComplianceList]
@UserID VARCHAR(30),
@OwnRecords VARCHAR(2),
@BusinessUnitTitle VARCHAR(80),
@showComplianceRisk VARCHAR(5),
@useComplianceTitle VARCHAR(5),
@Consequences VARCHAR(50),
@Likelihood VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @SQL NVARCHAR(MAX)
DECLARE @custom CURSOR
SET @custom = CURSOR FOR SELECT tblCustomFields.fieldName, tblCustomFields.rptSeq FROM dbo.tblCustomFields WHERE relatedTo = 'CP'
DECLARE @fieldName VARCHAR(30)
DECLARE @rptSeq VARCHAR(3)
SET @SQL = 'Select tblProcess.id, tblProcess.ProcessNumber, '
IF @useComplianceTitle = 'True'
SET @SQL = @SQL + 'tblProcess.title AS [Title],'
SET @SQL = @SQL + ' tblProcess.Process AS [Description], '
IF @showComplianceRisk = 'True'
SET @SQL = @SQL + 'tblProcess.Risk, tblProcess.MRR AS [Risk Rating],'
SET @SQL = @SQL + 'tblConsequences.Comment AS [' + @Consequences + '], tblLikelihood.Title AS [' + @Likelihood + '], tblBusiness.Activity AS [' + @BusinessUnitTitle + '],
tblProcessCategory.Category, p.Title AS [Responsible Officer], dbo.actionedBy_Positions(tblProcess.id, ''P'') AS [Actioned By],
rs.statusDescription AS [Status], dbo.Selected_Tag_List(tblProcess.linkedTags) AS Tags'
OPEN @custom
FETCH NEXT FROM @custom INTO @fieldName, @rptSeq
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = @SQL + ', dbo.Get_Custom_Field_Value_Compliance(' + @rptSeq + ',tblProcess.id) AS [' + @fieldName + ']'
FETCH NEXT FROM @custom INTO @fieldName, @rptSeq
END
CLOSE @custom
DEALLOCATE @custom
SET @SQL = @SQL + 'FROM tblProcess
CROSS JOIN dbo.Security_Compliance_Record_List(' + @UserID + ', ' + @OwnRecords + ', 0) AS p1
INNER JOIN
tblBusiness ON tblProcess.BusinessActivity = tblBusiness.id INNER JOIN
tblProcessCategory ON tblProcess.Category = tblProcessCategory.id INNER JOIN
tblPeople p ON tblProcess.Responsibility = p.id INNER JOIN
tblConsequences ON tblProcess.Consequences = tblConsequences.Consequence INNER JOIN
tblLikelihood ON tblProcess.Likelihood = tblLikelihood.Likelihood INNER JOIN
tblRecordStatus rs ON tblProcess.recordStatus = rs.id INNER JOIN
tblRiskMatrixNames ON tblProcess.MRR = tblRiskMatrixNames.displayName
WHERE (tblProcess.id = p1.id) ORDER BY tblProcess.ProcessNumber'
EXEC sp_executesql @SQL
END
我尝试了两种不同的方法来执行存储过程,但它们都只返回零。 应用程序在VB中,我们使用LINQ进行数据访问。 使用LINQ的第一个例子:
Dim ldb As CaRMSDataContext
ldb = New CaRMSDataContext(sessionHandler.connection.ToString)
Dim ls = ldb.uSp_CR_ComplianceList("1", "0", sessionHandler.BusinessUnitTitle, "True", "True", sessionHandler.consequencesText, sessionHandler.likelihoodText)
第二个例子是向DevExpress控件添加数据源。 DevExpress控件与我的问题无关,因为非动态的其他存储过程可以完美地工作。
Dim connectionParameters As New MsSqlConnectionParameters(sessionHandler.DBServer, sessionHandler.Catalog, sessionHandler.DBUser, sessionHandler.DBPassword, MsSqlAuthorizationType.SqlServer)
Dim ds As New DevExpress.DataAccess.Sql.SqlDataSource(connectionParameters)
Dim spParameters As New List(Of QueryParameter)(New QueryParameter() {New QueryParameter() With {.Name = "@UserID", .Type = Type.[GetType]("System.String"), .Value = sessionHandler.personID},
New QueryParameter() With {.Name = "@OwnRecords", .Type = Type.[GetType]("System.String"), .Value = seeOwn},
New QueryParameter() With {.Name = "@BusinessUnitTitle", .Type = Type.GetType("System.String"), .Value = sessionHandler.BusinessUnitTitle},
New QueryParameter() With {.Name = "@showComplianceRisk", .Type = Type.GetType("System.String"), .Value = sessionHandler.showComplianceRisk.ToString},
New QueryParameter() With {.Name = "@useComplianceTitle", .Type = Type.GetType("System.String"), .Value = sessionHandler.useComplianceTitle.ToString},
New QueryParameter() With {.Name = "@Consequences", .Type = Type.GetType("System.String"), .Value = sessionHandler.consequencesText},
New QueryParameter() With {.Name = "@Likelihood", .Type = Type.GetType("System.String"), .Value = sessionHandler.likelihoodText}})
Dim compliance As New StoredProcQuery("Compliance", "uSp_CR_ComplianceList", spParameters)
以下是SSMS中执行的存储过程的屏幕截图: SSMS example 任何人都可以建议如何在代码中返回数据。
由于 詹姆斯
答案 0 :(得分:0)
如何从命令对象执行sp并将结果分配给数据集?它又回归了什么?
Using connection As New SqlClient.SqlConnection("<your connection string>")
Dim command As SqlClient.SqlCommand
command = connection.CreateCommand
With command
.CommandText = "uSp_CR_ComplianceList"
.CommandType = CommandType.StoredProcedure
With .Parameters
' add your parameters here
End With
Dim adapter As New SqlClient.SqlDataAdapter(command)
Dim dataset As New DataSet
adapter.Fill(dataset)
End With
End Using