C#-找到用数字签入的方法
作者:互联网
您能帮我解决我的问题吗?我使用的是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 |
我可以计算出他来健身房的时间,但我不知道如何在代码中添加它
解决方法:
我认为您可能需要重新考虑解决问题的方法.如果我是你我会:
>使用ID.text获取enddate表中的记录,我假设这是您的客户报价表.因此,您具有该客户ID的数据STARTDATE,ENDDATE,Offer和其他信息.
>如果ENDDATE为空,并且Offer = day,则ENDDATE = DATE(Datetime.Now)
>使用ID.text从签入表中计数记录.因此,通过使用以下语句,您获得了多次访问.
从检入中选择COUNT(*)WHERE Time> = STARTDATE and(Time< = ENDDATE)
>您现在有了访问次数,您可以设置一个条件来检查客户是否已经用完“天”(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);
}
标签:checkin,c,winforms 来源: https://codeday.me/bug/20191211/2105612.html