2012-09-12 45 views
-1

下面是使用存储过程(GetTimesWithCustomerNames)的示例然后经由ArrayList的收集由查询检索数据:SQL Server 2008中,通过存储的过程修改查询到.NET的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 ([email protected]) 
     and (DATEPART(MONTH,t1.timein)[email protected] or @month=0) 
     and (DATEPART(YEAR,t1.timein)[email protected] or @year=0) 
     and (t1.reasonid = @reasonid or @reasonid=0)   
     and 
     (select COUNT(1) from tblTime t2 where [email protected] 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 [email protected] 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 [email protected] 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 

我想知道,对于给定的用户标识符,我可以将所有数据返回到ArrayList中 - 如何为每个用户标识符检索一组值?是否有可能在arrayList中为每个用户返回一个数据(很少),并且不只是一个像这样的过程?

在此查询数据为TimeInTimeOutActiveDate等等

  • 重新编辑 我想我的问题是应该的任务是为后面的代码在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; 
    } 
+0

为什么你会使用'ArrayList'?您是否使用.NET 1.1或更低版本? –

+0

@JohnSaunders根本没有(4.0)。这是我正在重复使用的代码。新的任务是获取更多的一个用户数据集,然后通过每个用户数据填充一个表,什么是正确的方式? – LoneXcoder

+0

突然间我以为我回到了10年前的时间:)。对不起,我不明白你想要达到什么目的。 – Marcote

回答

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; 
} 
相关问题