標籤:c# 介面設計 介面 .net
登陸介面最主要的就是資料庫訪問,這裡就不多講介面設計了,
直接給代碼:(這段代碼加在登陸按鈕的事件裡)
#region 定義使用者資訊變數
string UserName = TextBox_User_Name.Text.Trim();
string UserPassword = TextBox_User_Password.Text.Trim();
#endregion
#region 檢查使用者是否輸入使用者資訊
if (UserName == "" && UserPassword == "")
{
MessageBox.Show("您還沒有輸入使用者資訊呢!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (UserName == "")
{
MessageBox.Show("您還沒有輸入使用者名稱稱呢!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (UserPassword == "")
{
MessageBox.Show("您還沒有輸入使用者密碼呢!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
#endregion
#region 檢查使用者輸入的資訊是否合法
bool IS_User_Name_OK = (UserName.Contains("\\") || UserName.Contains("\"") || UserName.Contains("\‘"));
if (IS_User_Name_OK)
{
MessageBox.Show("您輸入的使用者名稱稱裡含有特殊字元:\r1:英文的引號(\" 、‘)\r2:英文的反斜線(\\)!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
bool IS_User_Password_OK = (UserPassword.Contains("\\") || UserPassword.Contains("\"") || UserPassword.Contains("\‘"));
if (IS_User_Password_OK)
{
MessageBox.Show("您輸入的使用者密碼裡含有特殊字元:\r1:英文的引號(\" 、‘)\r2:英文的反斜線(\\)!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
#endregion
#region 資料庫訪問
string Conn_String = "";
Conn_String = "Data Source=‘DataBase.mdb‘; Provider=Microsoft.ACE.OLEDB.12.0;";
OleDbConnection Db_Conn = new OleDbConnection(Conn_String);
Db_Conn.Open();
OleDbCommand Db_Cmd = new OleDbCommand("Select * from [User] where UserName=‘" + UserName + "‘ and UserPassword=‘" + UserPassword_ + "‘",Db_Conn);
OleDbDataReader Db_Reader = Db_Cmd.ExecuteReader();
Db_Reader.Read();
if (!Db_Reader.HasRows)
{
MessageBox.Show("使用者名稱或密碼錯誤!", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else
{
//登陸成功後要執行的代碼
}
#endregion
//完
註:
代碼裡給出了檢查使用者是否輸入資訊已經輸入的規不規範的代碼。
注意事項:
1:這裡的資料庫路徑(Data Source)已經資料庫驅動程式(Provider)是以筆者電腦的,請各位自己改一下,windows7內建的是Microsoft.ACE.OLEDB.12.0;順便再說一下,低版本的Provider不能開啟高版本的資料庫。
2:查詢語句裡的資料表已經查詢欄位需要讀者自己修改。
3:這裡的代碼是針對access用的,用SQL的讀者需要把OleDB改成Sql。(區分大小寫)
4:裡面的控制項名稱可能跟讀者的不一樣,但大多數都明白的了,改一下就行了。
以上注意事項請讀者注意一下,但不要埋怨筆者,這些都是必須注意的。
要原始碼的讀者請發送請求到:[email protected]
c#做登陸介面