以下は、ストアド プロシージャ (GetTimesWithCustomerNames) を使用してから、クエリによって取得されたデータを収集する ArrayList を使用する例です。
/*
GetTimesWithCustomerNames 3571, 6, 2012, 1
GetTimesWithCustomerNames '3571', '6', '2012', '0'
*/
ALTER PROCEDURE [dbo].[GetTimesWithCustomerNames]
@userid int=0, @month int=0, @year int=0,@reasonid int=0
AS
BEGIN
SET NOCOUNT ON;
if @userid!=0 begin
create table #tmp (tId int, UserId int,
TimeIn1 smalldatetime, [TimeOut1] smalldatetime,
TimeIn2 smalldatetime, [TimeOut2] smalldatetime, tId2 int,
TimeIn3 smalldatetime, [TimeOut3] smalldatetime, tId3 int,
ActiveDate smalldatetime, ReasonID int, Name nvarchar(100), ReasonType nvarchar(100),
TotalMins int)
insert into #tmp (tId, UserId, TimeIn1, TimeOut1, ActiveDate, ReasonID, Name, ReasonType)
SELECT
t1.tId, t1.UserId, t1.TimeIn, t1.[TimeOut], t1.ActiveDate, t1.ReasonID, tblCustomers.Name,
(select reasontype from tblTimeReas where ReasonID=t1.ReasonID) as ReasonType
FROM tblTime t1
inner join tblCustomers on t1.UserId=tblCustomers.custID
where (t1.userid=@userid)
and (DATEPART(MONTH,t1.timein)=@month or @month=0)
and (DATEPART(YEAR,t1.timein)=@year or @year=0)
and (t1.reasonid = @reasonid or @reasonid=0)
and
(select COUNT(1) from tblTime t2 where userid=@userid and datediff(day,t2.TimeIn,t1.TimeIn)=0 and t2.tId<t1.tId)=0
update #tmp
set tId2 = (select top 1 tId from tblTime t2 where userid=@userid and DATEDIFF(day,t2.timein,#tmp.timein1)=0
and t2.tId>#tmp.tId order by tId asc)
update #tmp
set tId3 = (select top 1 tId from tblTime t3 where userid=@userid and DATEDIFF(day,t3.timein,#tmp.timein2)=0
and t3.tId>#tmp.tId2 order by tId asc)
update #tmp
set TimeIn2 = (select TimeIn from tblTime where tId=tId2),
TimeOut2 = (select [TimeOut] from tblTime where tId=tId2),
TimeIn3 = (select TimeIn from tblTime where tId=tId3),
TimeOut3 = (select [TimeOut] from tblTime where tId=tId3)
update #tmp set TotalMins = (
isnull(DATEDIFF(minute,timein1,timeout1),0)+
isnull(DATEDIFF(minute,timein2,timeout2),0)+
isnull(DATEDIFF(minute,timein3,timeout3),0)
)
select * from #tmp order by TimeIn1
drop table #tmp
end
END
特定のユーザー ID について、すべてのデータを ArrayList に返すことができることを知りたいです。ユーザー ID ごとに一連の値を取得する方法は何ですか? この手順のような1 つだけではなく、arrayList でユーザー (少数) ごとにデータを返すことは可能ですか?
このクエリデータでは、TimeIn
TimeOut
ActiveDate
etc...
- 再編集 私の質問は、タスクが foreach ループ内のコード ビハインド用であるべきか、または複数のユーザー ID を受け入れるように手順を変更できるかということだと思います。
コードビハインドにデータを保存するために使用しているコードは次のとおりです。
public static ArrayList loadData(string sql)
{
DBManager dbManager = new DBManager(DataProvider.SqlServer, "Data Source=(local);Initial Catalog=databaseName;Integrated Security=True");
ArrayList data = new ArrayList();
try
{
dbManager.Open();
dbManager.ExecuteReader(System.Data.CommandType.Text, sql);
while (dbManager.DataReader.Read())
{
Hashtable x = new Hashtable();
for (int i = 0; i < dbManager.DataReader.FieldCount; i++)
{
x.Add(dbManager.DataReader.GetName(i), dbManager.DataReader.GetValue(i));
}
x.Add("COUNTER", data.Count+1);
data.Add(x);
}
}
catch { data = null; }
finally
{
dbManager.Dispose();
}
return data;
}