我對使用 C# 進行編碼以及使用 Windows 表單應用程式和 SQLite 資料庫是相當陌生的,我想知道是否有辦法讓用戶保持登錄狀態并在不同的表單上顯示所述用戶的 ID 以表明他們已登錄。專案本身是一個訂購系統,我希望客戶登錄以執行任何購買,以便可以將其記錄為管理報告的一部分。如果有任何幫助,代碼如下。
int charlengthusername= usernametextbox.Text.Length;
int charlengthpassword = passwordtextbox.Text.Length;
if (charlengthusername <= 4 || charlengthusername >= 6 || charlengthpassword >=13||String.IsNullOrEmpty(usernametextbox.Text)||String.IsNullOrEmpty(passwordtextbox.Text))
{
MessageBox.Show("Details Entered Don't Meet The Expected Character Length Requirements. Try Again.");
usernametextbox.Clear();
passwordtextbox.Clear();
}
//make another if for user and pass matching
//change else below to be inside of username and password match if statement so that it will only send to custmainmenu when username and password has been matched
else
{
sqlite_conn = new SQLiteConnection("Data Source=CustomerDatabase.db; Version = 3; New = True; Compress = True;");
sqlite_conn.Open();
SQLiteDataReader sqlite_datareader;
string user = usernametextbox.Text;
string pass = passwordtextbox.Text;
sqlite_cmd = sqlite_conn.CreateCommand();
sqlite_cmd.Connection = sqlite_conn;
sqlite_cmd.CommandText = "SELECT * FROM TblCustomer where CustRandID='" usernametextbox.Text "' AND CustPassword='" passwordtextbox.Text "'";
sqlite_datareader = sqlite_cmd.ExecuteReader();
if (sqlite_datareader.Read())
{
MessageBox.Show("Login Successful! ");
this.Hide();
var openmainmenu = new CustomerMainMenu();
openmainmenu.Closed = (t, args) => this.Close();
openmainmenu.Show();
}
else
{
MessageBox.Show("Incorrct Details! Try Again! ");
this.Hide();
var spenmainmenu = new CustomerLogin();
spenmainmenu.Closed = (s, args) => this.Close();
spenmainmenu.Show();
}
sqlite_conn.Close();
}
uj5u.com熱心網友回復:
您可以在類中使用靜態成員并從任何形式訪問它們。
class UserInfo {
public static String UserName ="";
public static bool IsLoggedIn = false;
}
您需要在登錄成功塊內設定值,如下所示:
UserInfo.IsLoggedIn = true;
UserInfo.UserName = "Uday Patel";
uj5u.com熱心網友回復:
最簡單的方法是將經過驗證的用戶名/密碼存盤在靜態類的公共靜態屬性中。
public static class AppInfo {
public static string Username = "";
}
// ...
MessageBox.Show("Login Successful! ");
AppInfo.Username = user;
然后,您可以從中訪問它Username(以及您的應用程式可能需要的任何其他全域資料)AppInfo(當然,您可以將其命名為其他名稱)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/446886.html
