C# 如何使用SqlDataReader获取存储过程返回的分组数据?
我有一个由定义的存储过程C# 如何使用SqlDataReader获取存储过程返回的分组数据?,c#,sql,asp.net,sql-server,asp.net-mvc,C#,Sql,Asp.net,Sql Server,Asp.net Mvc,我有一个由定义的存储过程 CREATE PROCEDURE GetQuestionsGroupedBySection @survey_id INT AS SELECT S.title, Q.qtext FROM Sections AS S INNER JOIN Questions AS Q ON S.id = Q.section_id WHERE S.survey_id = @survey_id GROUP BY S
CREATE PROCEDURE GetQuestionsGroupedBySection
@survey_id INT
AS
SELECT S.title, Q.qtext
FROM Sections AS S INNER JOIN Questions AS Q
ON S.id = Q.section_id
WHERE S.survey_id = @survey_id
GROUP BY S.title;
其中,相关表格由
CREATE TABLE Questions (
id INT IDENTITY(1,1),
section_id INT,
qtext NVARCHAR(300) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (section_id) REFERENCES Sections(id) ON DELETE CASCADE
);
及
现在,我试图从服务器端代码中调用它,以获取类型元素的列表中的分组
public class QuestionsBySection
{
public string SectionTitle { get; set; }
public List<string> SecionQuestions;
}
public class QuestionsBySection
{
公共字符串SectionTitle{get;set;}
公开名单问题;
}
(或者是否有更好的数据结构可供使用?)。我所拥有的是
public List<QuestionsBySection> GetQuestionsAndSection (int survid)
{
// survid: survey id
List<QuestionsBySection> AllQuestions = new List<QuestionsBySection>();
using (SqlCommand cmd = new SqlCommand("GetQuestionsGroupedBySection", this._Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@survey_id", survid);
this._Conn.Open();
using ( SqlDataReader dataReader = cmd.ExecuteReader() )
{
while ( dataReader.Read() )
{
// ...
}
}
this._Conn.Close();
}
return AllQuestions;
}
public List getquestionsandssection(intsurvid)
{
//survid:调查id
列出所有问题=新建列表();
使用(SqlCommand cmd=newsqlcommand(“GetQuestionsGroupedBySection”,this.\u Conn))
{
cmd.CommandType=CommandType.storedProcess;
cmd.Parameters.AddWithValue(“@survey_id”,survid);
这个。_Conn.Open();
使用(SqlDataReader=cmd.ExecuteReader())
{
while(dataReader.Read())
{
// ...
}
}
这个。_Conn.Close();
}
返回所有问题;
}
但是我不知道如何填写/..
。有任何提示吗?Sql分组依据错误
结果中有2列,但组中只有1列。您可以将第二列作为“分组依据”字段添加,也可以使用agregate(例如min()或max())
DataReader
只需使用字段位置作为索引参数即可获取字符串:
var sTitle = dataReader.GetString(0);
var sQuestion = dataReader.GetString(1);
SQL查询对SQL Server无效;使用group by子句时,所选的所有字段必须包含在group by子句中,或者是聚合字段。根据您的预期结果,有两种方法可以返回问题集ysection
一个选项是返回平展的结果集,然后在应用层上进行聚合
存储过程应删除group by
子句:
SELECT S.title, Q.qtext
FROM Sections AS S INNER JOIN Questions AS Q
ON S.id = Q.section_id
WHERE S.survey_id = @survey_id
检索时,需要评估每一行,以查看标题是否已被使用:
var questions = new List<QuestionsBySection>();
// create sql connection
// execute command
while (dataReader.Read())
{
string title = dataReader.GetString("title");
string question = dataReader.GetString("qtext");
QuestionsBySection qbs = questions.FirstOrDefault(x => x.SectionTitle.Equals(title , StringComparison.OrdinalIgnoreCase))
if (qbs != null)
{
qbs.SecionQuestions.Add(question);
continue;
}
qbs = new QuestionsBySection
{
SectionTitle = title,
SecionQuestions = new List<string>{ question }
}
questions.Add(qbs);
}
var问题=新列表();
//创建sql连接
//执行命令
while(dataReader.Read())
{
string title=dataReader.GetString(“title”);
字符串问题=dataReader.GetString(“qtext”);
QuestionsBySection qbs=questions.FirstOrDefault(x=>x.SectionTitle.Equals(title,StringComparison.OrdinalIgnoreCase))
如果(qbs!=null)
{
qbs.SecionQuestions.Add(问题);
继续;
}
qbs=新问题部分
{
SectionTitle=标题,
SecionQuestions=新列表{question}
}
添加问题(qbs);
}
还有其他几种方法可以实现结果,但这会让您更好地了解如何聚合结果。您的存储过程似乎不是有效的SQL(缺少分组或在Q.qtext上聚合)@JoachimIsaksson I haveGROUP BY S.title代码>SQL Server提供的列“Questions.qtext”在选择列表中无效,因为在尝试创建过程时,它既不包含在聚合函数中,也不包含在GROUP BY子句中。只有MySQL允许您选择一个字段,而无需对其进行聚合或分组。
SELECT S.title, Q.qtext
FROM Sections AS S INNER JOIN Questions AS Q
ON S.id = Q.section_id
WHERE S.survey_id = @survey_id
var questions = new List<QuestionsBySection>();
// create sql connection
// execute command
while (dataReader.Read())
{
string title = dataReader.GetString("title");
string question = dataReader.GetString("qtext");
QuestionsBySection qbs = questions.FirstOrDefault(x => x.SectionTitle.Equals(title , StringComparison.OrdinalIgnoreCase))
if (qbs != null)
{
qbs.SecionQuestions.Add(question);
continue;
}
qbs = new QuestionsBySection
{
SectionTitle = title,
SecionQuestions = new List<string>{ question }
}
questions.Add(qbs);
}