更新了上次登录字段的 C# 登录
C# login with last login field updated
在 table 中检查用户和密码后,我尝试了几种不同的方法来更新列,尽管我没有任何运气来更新列 Last_login 有或没有 UserId (这是我的主键)除非我要将用户名更改为主键,否则我在不包含 Last_login sql 命令的情况下无法登录,尽管我更愿意包含它。
private void Connect()
{
SqlConnection connection = new SqlConnection();
//Imports the methods from Custom Security class
Custom_Security security = new Custom_Security();
string userid = "";
string time = "";
try
{
connection.ConnectionString = connectionPath;
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Logins WHERE Username = @Username (UserId, Password) VALUES (@UserID, @Password)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@Username", txtuser.Text);
cmd.Parameters.AddWithValue("@Password", security.AES(security.Hashstring(txtpass.Text)));
cmd.Parameters.AddWithValue("@UserId", userid);
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
DialogResult dlgResult;
dlgResult = MessageBox.Show(
"Welcome: " + txtuser.Text,
"Login sucessful",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
//Closes the data reader
dr.Close();
//Clears previous SQL command
cmd.Parameters.Clear();
DateTime timeNow = DateTime.UtcNow;
time = timeNow.ToShortTimeString();
//Inserts current time into field last login to update the column
cmd.CommandText = "INSERT INTO Logins (UserId, Last_login) VALUES (@UserId, @Last_login)";
cmd.Parameters.AddWithValue("@UserId", userid);
cmd.Parameters.AddWithValue("@Last_login",time);
cmd.ExecuteNonQuery();
this.Hide();
//loads the protected menu
Protected protectedform = new Protected(txtuser.Text);
protectedform.Show();
}
else
{
DialogResult dlgResult;
dlgResult = MessageBox.Show(
"Please try again",
"Login unsucessful",
MessageBoxButtons.OK,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
}
connection.Close();
connection.Dispose();
}
catch (SqlException sql)
{
MessageBox.Show(sql.Message);
}
}
执行示例:
http://imgur.com/88IziLt
如果有人能发现或知道解决此问题的方法,请告诉我:)。
非常感谢,
10gez10
您的第一个查询应该只是一个带有参数 @UserName
和 @Password
的简单 SELECT
using(SqlCommand cmd = new SqlCommand(@"SELECT * FROM Logins
WHERE Username = @Username
AND Password = @Password", connection);
{
cmd.Parameters.AddWithValue("@Username", txtuser.Text);
cmd.Parameters.AddWithValue("@Password", security.AES(security.Hashstring(txtpass.Text)));
using(SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
....
}
}
现在,如果您找到了您的用户,您应该使用 userid
值的更新查询来设置 lastlogin
.....
dr.Close();
//Clears previous SQL command
cmd.Parameters.Clear();
DateTime timeNow = DateTime.UtcNow;
time = timeNow.ToShortTimeString();
//Inserts current time into field last login to update the column
cmd.CommandText = @"UPDATE Logins SET Last_login
WHERE UserID = @UserId";
cmd.Parameters.AddWithValue("@UserId", userid);
cmd.Parameters.AddWithValue("@Last_login",time);
cmd.ExecuteNonQuery();
在 table 中检查用户和密码后,我尝试了几种不同的方法来更新列,尽管我没有任何运气来更新列 Last_login 有或没有 UserId (这是我的主键)除非我要将用户名更改为主键,否则我在不包含 Last_login sql 命令的情况下无法登录,尽管我更愿意包含它。
private void Connect()
{
SqlConnection connection = new SqlConnection();
//Imports the methods from Custom Security class
Custom_Security security = new Custom_Security();
string userid = "";
string time = "";
try
{
connection.ConnectionString = connectionPath;
connection.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Logins WHERE Username = @Username (UserId, Password) VALUES (@UserID, @Password)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@Username", txtuser.Text);
cmd.Parameters.AddWithValue("@Password", security.AES(security.Hashstring(txtpass.Text)));
cmd.Parameters.AddWithValue("@UserId", userid);
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
DialogResult dlgResult;
dlgResult = MessageBox.Show(
"Welcome: " + txtuser.Text,
"Login sucessful",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
//Closes the data reader
dr.Close();
//Clears previous SQL command
cmd.Parameters.Clear();
DateTime timeNow = DateTime.UtcNow;
time = timeNow.ToShortTimeString();
//Inserts current time into field last login to update the column
cmd.CommandText = "INSERT INTO Logins (UserId, Last_login) VALUES (@UserId, @Last_login)";
cmd.Parameters.AddWithValue("@UserId", userid);
cmd.Parameters.AddWithValue("@Last_login",time);
cmd.ExecuteNonQuery();
this.Hide();
//loads the protected menu
Protected protectedform = new Protected(txtuser.Text);
protectedform.Show();
}
else
{
DialogResult dlgResult;
dlgResult = MessageBox.Show(
"Please try again",
"Login unsucessful",
MessageBoxButtons.OK,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
}
connection.Close();
connection.Dispose();
}
catch (SqlException sql)
{
MessageBox.Show(sql.Message);
}
}
执行示例: http://imgur.com/88IziLt
如果有人能发现或知道解决此问题的方法,请告诉我:)。
非常感谢, 10gez10
您的第一个查询应该只是一个带有参数 @UserName
和 @Password
using(SqlCommand cmd = new SqlCommand(@"SELECT * FROM Logins
WHERE Username = @Username
AND Password = @Password", connection);
{
cmd.Parameters.AddWithValue("@Username", txtuser.Text);
cmd.Parameters.AddWithValue("@Password", security.AES(security.Hashstring(txtpass.Text)));
using(SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
....
}
}
现在,如果您找到了您的用户,您应该使用 userid
值的更新查询来设置 lastlogin
.....
dr.Close();
//Clears previous SQL command
cmd.Parameters.Clear();
DateTime timeNow = DateTime.UtcNow;
time = timeNow.ToShortTimeString();
//Inserts current time into field last login to update the column
cmd.CommandText = @"UPDATE Logins SET Last_login
WHERE UserID = @UserId";
cmd.Parameters.AddWithValue("@UserId", userid);
cmd.Parameters.AddWithValue("@Last_login",time);
cmd.ExecuteNonQuery();