using System; using System.Data; using System.IO; using System.Windows.Forms; using Forks.Utils.Data; using Forks.Utils.IO; namespace B3ButcherWeightClient { public partial class Login : Form { public Login() { InitializeComponent(); using (TextReader reader = FS.OpenReader(ConfigUtil.ConfigFilePath,true)) { NutFile nutFile = NutFile.Parse(reader); txtBUser.Text = nutFile.AsString(ConfigItem.LastUser, "system"); } } private void Form1_Load(object sender, EventArgs e) { } private void btnLogin_Click(object sender, EventArgs e) { ConfigUtil.Init(); string loginUser = txtBUser.Text.Trim(); string passWord = txtBPassword.Text.Trim(); long userID = 0; string role = ""; if (!CheckUser(loginUser)) { MessageBox.Show("用户名不存在!"); txtBUser.Text = ""; txtBPassword.Text = ""; txtBUser.Focus(); } else { if (CheckPassword(loginUser, passWord, out userID, out role)) { Form main = new Main(this); Hide(); using (var reader = FS.OpenReader(ConfigUtil.ConfigFilePath)) { var nutFile = NutFile.Parse(reader); nutFile.SetValue(ConfigItem.LastUser, loginUser); using (var writer = FS.OpenWriter(ConfigUtil.ConfigFilePath)) { nutFile.Write(writer); } } main.Show(); } else { MessageBox.Show("密码错误!"); txtBPassword.Text = ""; txtBPassword.Focus(); } } } private void btnSetConStr_Click(object sender, EventArgs e) { Form setForm = new Setting(); setForm.ShowDialog(); } private static bool CheckUser(string userName) { var conStr = ConfigUtil.ConnectionStr; if (string.IsNullOrEmpty(conStr)) { MessageBox.Show("未设置本地数据库信息"); return false; } var isExist = false; if (!string.IsNullOrEmpty(userName)) { string sql = "select PassWord from LoginUser where UserName='" + userName.Trim() + "'"; try { using (ISqlUtil sqlUtil = new SqlUtil(conStr)) { DataSet dataSet = sqlUtil.ExecuteSql(sql); if (dataSet.Tables[0].Rows.Count > 0) { isExist = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } } return isExist; } private static bool CheckPassword(string userName, string password, out long userID, out string role) { userID = 0; role = ""; string conStr = ConfigUtil.ConnectionStr; bool isRight = false; string sql = "select top 1 ID,RoleSchema from LoginUser where UserName='" + userName.Trim() + "' and PassWord=cast('" + password.Trim() + "' as binary)"; try { using (ISqlUtil sqlUtil = new SqlUtil(conStr)) { DataSet dataSet = sqlUtil.ExecuteSql(sql); if (dataSet.Tables[0].Rows.Count > 0) { isRight = true; userID = (long)dataSet.Tables[0].Rows[0][0]; role = (string)dataSet.Tables[0].Rows[0][1]; } } } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } return isRight; } } }