0

我有一个人事角色表,其中为员工分配了每日角色或具有特定开始和结束日期的角色。经理们要求提供一种人力计划表,其中列出了员工的日常角色以及我如何生成该表

private string CreateHTMLTable(Int32 month)
{
    StringBuilder strBuilder = new StringBuilder();
    System.Data.DataTable dtAllStaff = new System.Data.DataTable();
    //get all staff
    PersonelApplication.Classes.PersonelClass PersonnelClass = new PersonelClass();

    dtAllStaff = PersonnelClass.GetAllPersonel();

    //create manpower data table
    System.Data.DataTable dtManPowerDataTable = new System.Data.DataTable();

    //create montlhy dt
    //get number of days in month
    int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, month);
    //get first day in month
    DateTime firstDayInMonth = new DateTime(DateTime.Now.Year, month, 1);
    //get last day in month
    DateTime lastDayInMonth = new DateTime();
    lastDayInMonth = firstDayInMonth.AddMonths(1).AddDays(-1);



    //start table
    strBuilder.Append("<table>");

    //create header based on number of days in the month
    //append tr strat
    strBuilder.Append("<tr>");
    //add name header for personnle
    strBuilder.Append("<th>");
    strBuilder.Append("Staff");
    strBuilder.Append("</th>");
    for (int i = 1; i <= lastDayInMonth.Day; i++)
    {
        strBuilder.Append("<th>");
        strBuilder.Append(i.ToString() + "/" + month.ToString());
        strBuilder.Append("</th>");
    }

    //append tr end to header row
    strBuilder.Append("</tr>");
    System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection();

    sqlConn.ConnectionString = ConnectionClass.CreateConnection.getConnectionString();
    using (sqlConn = ConnectionClass.CreateConnection.publicGetConn())
    {
        sqlConn.ConnectionString = ConnectionClass.CreateConnection.getConnectionString();
        try
        {
            sqlConn.Open();
            if (sqlConn.State == ConnectionState.Open)
            {

                foreach (DataRow row in dtAllStaff.Rows)
                {
                    string personnelName = "";
                    string personnelCode = "";

                    Int32 personnelID = 0; ;

                    personnelCode = row[1].ToString();
                    strBuilder.Append("<tr>");
                    strBuilder.Append("<td>");

                    strBuilder.Append(personnelCode);
                    strBuilder.Append("</td>");
                    for (int i = 1; i <= lastDayInMonth.Day; i++)
                    {
                        //here get the each employee's planned role as well
                        //as actual role 
                    }
                    strBuilder.Append("</tr>");
                }

            }
        }
        catch (Exception ex)
        {
            //pouplate later
        }
        finally
        {

        }

    }

    //end table
    strBuilder.Append("</table>");


    return strBuilder.ToString();
}

我的问题是 SQL 函数,它将返回特定日期的员工角色。

--actual end date for this role is '08-18-2012'
declare @sdate date
set @sdate= '08-14-2012'
SELECT 
    CONVERT(date,startdate,101)
    ,CONVERT(date,EndDate,101)
    ,StartDate
    ,EndDate
     ,fk_PersonelID
     ,fk_RoleID
  FROM [dbo].JobRolesTable
  where @sdate between StartDate and EndDate 
    and fk_PersonelID = 40

但是如果我搜索第二天是“08-15-2012”,我会得到 nada 基本上我想在一个月中的任何一天返回员工的角色,如果没有我不想要的则返回“na”为此使用光标,但有另一种方法可以实现这一点

4

2 回答 2

0
DECLARE @sdate DATE = '20120814';

SELECT 
    CONVERT(DATE,StartDate,101) -- what is the purpose of 101 here?
    ,CONVERT(DATE,EndDate,101)  -- what is the purpose of 101 here?
    ,StartDate
    ,EndDate
     ,fk_PersonelID
     ,fk_RoleID
  FROM [dbo].JobRolesTable
  WHERE @sdate >= StartDate 
    AND @sdate < DATEADD(DAY, 1, EndDate) 
    AND fk_PersonelID = 40;
于 2012-09-11T14:25:13.547 回答
0

您可能错误地填充了参数。

如果您的查询是形式

SELECT * 
FROM Table
WHERE (@SearchDate BETWEEN @StartDate AND @EndDate) AND Id=@Id

(你的似乎是),那么只要正确指定了日期,它就会从数据库返回正确的值。

您能否显示您实际尝试使用您在发布的代码中打开的 SqlConnection 的代码?

于 2012-09-11T14:26:42.747 回答