我正在使用sql store程序获取数据列表,但目前我收到的错误为An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll
,此程序应该能够获取今天的数据。这是我的模式:
这是我的代码;
public List<ClassName> GetStaffs()
{
List<ClassName> staff = new List<ClassName>();
SqlConnection connection = new SqlConnection(ConfigurationManager.etc...);
SqlCommand mycomm = new SqlCommand("ProcedureName",connection);
mycomm.CommandType = CommandType.StoredProcedure;
mycomm.Parameters.AddWithValue("dates",DateTime.Today);
SqlDataReader reader;
connection.Open();
reader = mycomm.ExecuteReader();
connection.Close();
return staff;
}
在这一行中,我收到错误reader = mycomm.ExecuteReader();
我也不确定代码是否会获取我请求的人员数据列表。有人可以引导我走向正确的方向。这是我使用link
SQL 2008中的存储过程
USE databaseName
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ProcedureName] @dates Datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT ID, fName, sName, dates,
COUNT(CASE TotalStaff WHEN 0 THEN 0 ELSE NULL END) AS TotalStaffs,
FROM dbo.DatabaseName WHERE dates>= @dates
GROUP BY ID, fName, sName, dates
END
更新 我设法解决错误,但我没有从我的商店程序中获取数据。
答案 0 :(得分:1)
好的,在您的SP中,我通过在日期中投射&#34; date&#34; 列来忽略条件中的时间部分。所以现在你的SP看起来像这样 -
ALTER PROCEDURE [dbo].[ProcedureName]
@dates Datetime
AS
BEGIN
SET NOCOUNT ON;
SELECT ID, fName, sName, dates,
COUNT(CASE TotalStaff WHEN 0 THEN 0 ELSE NULL END) AS TotalStaffs,
FROM dbo.DatabaseName WHERE CAST(dates As Date) >= CAST(@dates As Date)
GROUP BY ID, fName, sName, dates
END
在您的c#代码中,您尚未在列表中添加对象。所以这是修改后的代码。
public List<ClassName> GetStaffs()
{
List<ClassName> staff = new List<ClassName>();
SqlConnection connection = new SqlConnection(ConfigurationManager.etc...);
SqlCommand mycomm = new SqlCommand("ProcedureName",connection);
mycomm.CommandType = CommandType.StoredProcedure;
mycomm.Parameters.AddWithValue("dates",DateTime.Today);
SqlDataReader reader;
connection.Open();
reader = mycomm.ExecuteReader();
while (reader.Read())
{
staff.Add(new ClassName { fName = Convert.ToString(reader["fName"]), sName = Convert.ToString(reader["sName"]), ..so-on });
}
connection.Close();
return staff;
}
希望这会对你有帮助......