找到计数登记的方式

问题描述 投票:3回答:1

能帮我解决一下我的问题。我正在使用一个SQL Server数据库,它是一个体操项目,我想在他来健身房时检查客户端,我有两种方式提供第一种方式是按月方式,第二种是每日方式,第一个我没有问题,我用这个代码签入;

using (SqlCommand com = new SqlCommand("select count(*)from enddate where ID=@ID and startdate <=@C1 and endDate >=@C2", con))
                {

                    com.Parameters.AddWithValue("@ID", ID.Text);
                    com.Parameters.AddWithValue("@C1", DateTime.Now);
                    com.Parameters.AddWithValue("@C2", DateTime.Now);

                    int count = (int)com.ExecuteScalar();
                    if (count > 0)
                    {
                        using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time,username) VALUES (@ID,@time,@username)", con))
                        {
                            com1.Parameters.AddWithValue("@ID", ID.Text);

                            com1.Parameters.AddWithValue("@time", txttime.Text);

                            com1.Parameters.AddWithValue("@username", txtusername.Text);
                            com1.ExecuteNonQuery();
                        }
                        MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ................... ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MetroFramework.MetroMessageBox.Show(this, "this ID Expired .....................", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);


                    }
                    con.Close();
                }

我想在此代码中添加第二个条件(每日报价)我有enddate表格;

| ID | Startdate | month | day | enddate |          offer       |
| 1  | 20-3-2019 |   3   |null |20-6-2019|( summer ) monthly    |
| 2  | 20-3-2019 | null  | 5   |20-3-2019|( student )  daily    |

在这种情况下,第一个可以随时出现3个月,在第二个身份证中他只能来5次。

我的签到表;

| ID |   Time   | username |
| 1  | 21-3-2019| test     |
| 1  | 25-3-2019| test     |
| 2  | 27-3-2019| test 2   | 

我可以算一下他来健身房的时间,但我不知道如何在我的代码中添加它

c# winforms checkin
1个回答
2
投票

我想你可能想重新思考解决问题的方法。如果我是你我会:

  1. 使用ID.text获取enddate表中的记录,我假设这是您的客户提供表。因此,您拥有此客户ID的STARTDATE,ENDDATE,Offer和其他信息。
  2. 如果ENDDATE为null且Offer = dayly,则ENDDATE = DATE(Datetime.Now)
  3. 使用ID.text从签入表中计算记录。因此,您可以使用以下语句获得一次访问。

SELECT COUNT(*) From checkin WHERE Time >= STARTDATE and (Time <= ENDDATE)

  1. 您现在有了访问次数,您可以设置一个条件来检查客户是否已经用完5天的优惠。

花了一点时间后,我试图用C#完成你的整个逻辑:

var goodForVisit = false;
int visitedCount;
int offerDayCount;
var endDate = DateTime.MinValue;
DateTime startDate = DateTime.MinValue;

using (SqlCommand com = new SqlCommand("select * from [enddate] where ID=@ID", con))
{
    com.Parameters.AddWithValue("@ID", ID.Text);
    using (SqlDataReader reader = com.ExecuteReader())
    {
        if (reader.Read())
        {
            //get information from enddate table
            var offer = “”;
            if(reader[“offer”] != null)
                  offer = reader["offer"].ToString();
            if (reader[“day”] != null)
                  offerDayCount = (int)reader["day"];
            startDate = (DateTime)reader["Startdate"];
            if (reader["enddate"] != null)
                endDate = (DateTime)reader["enddate"];


            if (reader["enddate"] == null && offer == "dayly")
            {
                endDate = DateTime.Now.Date;
            }

            //count the visit from checkin table
            using (var com2 = new SqlCommand("SELECT COUNT(*) as count From checkin WHERE Time >= @STARTDATE and (Time <= @ENDDATE)"))
            {
                com.Parameters.AddWithValue("@STARTDATE", startDate);
                com.Parameters.AddWithValue("@ENDDATE", endDate);

                using (SqlDataReader reader2 = com2.ExecuteReader())
                {
                    if (reader2.Read())
                    {
                        visitedCount = (int)reader2["count"];
                        if (offer == "dayly" && visitedCount < offerDayCount)
                            goodForVisit = true;

                        if (offer == "monthly" && DateTime.Now >= startDate && DateTime.Now <= endDate)
                            goodForVisit = true;
                    }
                }
            }
        }
    }
}

if (goodForVisit)
{
    using (SqlCommand com1 = new SqlCommand("INSERT INTO [checkin] (ID,time,username) VALUES (@ID,@time,@username)", con))
    {
        com1.Parameters.AddWithValue("@ID", ID.Text);

        com1.Parameters.AddWithValue("@time", txttime.Text);

        com1.Parameters.AddWithValue("@username", txtusername.Text);
        com1.ExecuteNonQuery();
    }
    MetroFramework.MetroMessageBox.Show(this, "Check In Sucssesfuly ................... ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
    MetroFramework.MetroMessageBox.Show(this, "this ID Expired .....................", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
© www.soinside.com 2019 - 2024. All rights reserved.