首页 / C# / C#-找到用数字签入的方法
C#-找到用数字签入的方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-找到用数字签入的方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4007字,纯文字阅读大概需要6分钟。
内容图文
您能帮我解决我的问题吗?我使用的是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);
}
内容总结
以上是互联网集市为您收集整理的C#-找到用数字签入的方法全部内容,希望文章能够帮你解决C#-找到用数字签入的方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。