gpt4 book ai didi

C# 登录系统到期日期

转载 作者:行者123 更新时间:2023-11-29 12:08:24 26 4
gpt4 key购买 nike

这是我用于 C# mysql 登录系统的代码

    try
{
MySqlConnection connection = new MySqlConnection("Server=localhost; database=member; UID=root; Pwd=");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM member.member WHERE username='" + this.metroTextBox1.Text + "' AND password='" + this.metroTextBox2.Text + "' ;", connection);
MySqlDataReader myReader;
connection.Open();
myReader = cmd.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
Form2 frm = new Form2();
frm.username = metroTextBox1.Text;
frm.Show();
this.Visible = false;
}
else
{
MetroMessageBox.Show(this, "Incorrect login credentials. Failed to login!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

这是我的数据库 http://i.imgur.com/K831atR.png

基本上,如果过期日期已过,我想阻止登录。但我不知道该怎么办。

最佳答案

试试这个:

try
{
MySqlConnection connection = new MySqlConnection("Server=localhost; database=member; UID=root; Pwd=");
connection.Open();
// Using parameters to prevent SQL Injections
MySqlCommand cmd =
new MySqlCommand("SELECT * FROM member.member WHERE username=@username AND password=@password ;",
connection);
cmd.Parameters.AddWithValue("@username", this.metroTextBox1.Text);
cmd.Parameters.AddWithValue("@password", this.metroTextBox2.Text);

var myReader = cmd.ExecuteReader();
if (myReader.HasRows)
{
//Record found
myReader.Read();
// Assuming there's always a valid date in your table otherwise
// an exception will be thrown while trying to convert to a DateTime object
var expiryDate = myReader.GetDateTime("expirationdate");
// Use this to show the remaining days on your label
int remainingDays = (int) (DateTime.Now - expiryDate).TotalDays;
string labelCaption = String.Format("You have {0} day(s) left.", remainingDays);
if (DateTime.Now > expiryDate)
{
//Login Expired
}
else
{
//Login is still valid
Form2 frm = new Form2();
frm.username = metroTextBox1.Text;
frm.Show();
this.Visible = false;
}
}
else
{
//No record found
MetroMessageBox.Show(this, "Incorrect login credentials. Failed to login!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception)
{
// Do your exception handling
}

关于C# 登录系统到期日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com