-1

下面是使用存储过程 (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 的一组值的方法是什么?是否有可能在 arrayList 中为每个用户(少数)返回一个数据,而不是像这个过程一样?

在此查询中,数据是TimeIn TimeOut ActiveDate等...

  • 重新编辑我认为我的问题是该任务是否应该针对 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;
    }
4

1 回答 1

0
    public static List<Dictionary<string, object>> loadData(string sql)
    {

    itson = (sql.StartsWith("GetTimesWith"));
    DBManager dbManager = new DBManager(DataProvider.SqlServer, "Data Source=(local);Initial Catalog=hental;Integrated Security=True");

    //ArrayList data = new ArrayList();
    List<Dictionary<string, object>> data = new List<Dictionary<string,object>>();
    try
    {
        dbManager.Open();
        dbManager.ExecuteReader(System.Data.CommandType.Text, sql);
        while (dbManager.DataReader.Read())
        {
           //Hashtable x = new Hashtable();
           Dictionary<string, object> x = new Dictionary<string, object>();
            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);
            ncx = data.Count;

        }
    }
    catch { data = null; }
    finally
    {
        dbManager.Dispose();
    }

    return data;
}
于 2012-10-03T23:59:51.873 回答