private void btnLogin_Click(object sender, EventArgs e) { string account = txtAccount.Text.Trim(); string password = txtPassword.Text.Trim(); if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(password)) { MessageBox.Show("用户名或密码不能为空", "操作提示"); } else { string md5Str = ""; System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] data = Encoding.UTF8.GetBytes(password); byte[] md5data = md5.ComputeHash(data); md5.Clear(); for (int i = 0; i < md5data.Length; i++) { md5Str += md5data[i].ToString("x2"); } string query = string.Format(" WHERE account = '{0}' AND password = '******' ", account, md5Str); DataSetLibrarySystem.userDataTable dtUser = new DataSetLibrarySystem.userDataTable(); if (this._dbHelper.DataSetUserFill(dtUser, query) > 0) { DataSetLibrarySystem.userRow userRow = (DataSetLibrarySystem.userRow)dtUser[0]; this.DialogResult = DialogResult.OK; this._dbHelper.CurrentUser.uid = userRow.uid; this._dbHelper.CurrentUser.name = userRow.name; this._dbHelper.CurrentUser.account = userRow.account; this._dbHelper.CurrentUser.isUserLogin = true; this._dbHelper.CurrentUser.isAdminRole = (dtUser[0]["permission"].ToString() == "管理员"); this.Close(); } else { MessageBox.Show("用户名或密码错误", "操作提示"); } } }
/// <summary> /// 填充数据表[User]。 /// </summary> /// <param name="dataTable">填充数据表</param> /// <param name="where">查询条件</param> /// <param name="top">前多少行</param> /// <param name="clearBeforeFill">是否填充前清空</param> /// <returns>查询记录数目</returns> public int DataSetUserFill(DataSetLibrarySystem.userDataTable dataTable, string where = "", int top = 1000, bool clearBeforeFill = true) { int returnValue = 0; bool success = false; try { dataAdapterUserSelect.SelectCommand.CommandText = string.Format("SELECT TOP {0} uid, name, account, password, permission, " + "createdate, lastlogin, userremark, user_status FROM dbo.[user] {1} ", top, where); if (clearBeforeFill) { dataTable.Clear(); } returnValue = dataAdapterUserSelect.Fill(dataTable); success = true; } catch (System.Exception ex) { Console.WriteLine(ex.Message); } return(success ? returnValue : -1); }