簡體   English   中英

找到計數的方法

[英]find the way to check in with count

您能幫我解決我的問題嗎? 我使用的是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個月來,而在第二個ID中,他只能來5次。

我的簽到表;

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

我可以計算出他來健身房的時間,但我不知道如何在代碼中添加它

我認為您可能需要重新考慮解決問題的方法。 如果我是你我會:

  1. 使用ID.text獲取enddate表中的記錄,我假設這是您的客戶報價表。 因此,您具有該客戶ID的數據STARTDATE,ENDDATE,Offer和其他信息。
  2. 如果ENDDATE為空,並且Offer = day,則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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM