gpt4 book ai didi

asp.net - 如何将字符串列表传递给存储过程

转载 作者:行者123 更新时间:2023-12-01 14:33:54 26 4
gpt4 key购买 nike

  public DataSet ViewQuizDetails(List<string> name)
{
DataSet dsGrades = null;
var dbCon = new DBConnection("Quiz");
foreach(string val in name)
{
dbCon.AddParameter("@name", val);
dsGrades = dbCon.Execute_DataSet("spViewQuizDetails", null);
}
return dsGrades;
}

ALTER PROCEDURE [dbo].[spViewQuizDetails]
-- Add the parameters for the stored procedure here
@name varchar(30)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
--SELECT * FROM QuizDetails INNER JOIN Elearning
--ON QuizDetails.ElearningId=Elearning.ElearningId ;
SELECT *
FROM QuizDetails INNER JOIN Elearning
ON QuizDetails.ElearningId=Elearning.ElearningId where ElearningName=@name
END

当我传递多个值时,出现以下错误。

Error received: Procedure or function spViewQuizDetails has too many arguments specified.

最佳答案

将名称连接在一起的参数传递给您的存储过程。

在您的 C# 代码中构建这个连接的名称字符串

Declare @Name VARCHAR(1000) = 'Name1,Name2,Name3';

ALTER PROCEDURE [dbo].[spViewQuizDetails]
-- Add the parameters for the stored procedure here
@name VARCHAR(1000)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @xml xml;

set @xml = N'<root><r>' + replace(@name,',','</r><r>') + '</r></root>'

-- Insert statements for procedure here
--SELECT * FROM QuizDetails INNER JOIN Elearning ON QuizDetails.ElearningId=Elearning.ElearningId ;
SELECT * FROM QuizDetails
INNER JOIN Elearning
ON QuizDetails.ElearningId = Elearning.ElearningId
where ElearningName IN (select t.value('.','varchar(max)')
from @xml.nodes('//root/r') as a(t) )
END

关于asp.net - 如何将字符串列表传递给存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com