/// <summary> /// Добавляет указанного пользователя в базу данных /// </summary> /// <param name="people"></param> /// <returns></returns> public void AddUser(User user) { using (SQLiteCommand cmd = connection.CreateCommand()) { Dictionary<String, String> data = new Dictionary<string, string>(); data.Add("name", user.name); data.Add("lastName", user.lastName); data.Add("fatherName", user.fatherName); data.Add("phone", user.phone); data.Add("birthday", user.birthDayInt64.ToString()); data.Add("sex", user.sex); data.Add("password", user.password); data.Add("createDate", user.createDateInt64.ToString()); bool ok = Insert(cmd, TABLE_USER, data); if (!ok) throw new Exception("Не удалось добавить пользователя!"); } }
private void buttonCreateUser_Click(object sender, RoutedEventArgs e) { try { if (pass1.Password.Equals(pass2.Password)) { string pass = ""; using (MD5 md5Hash = MD5.Create()) { pass = GetMd5Hash(md5Hash, pass1.Password); } string[] names = tbName.Text.Split(' '); string name = names[0]; string lastName = names.Length > 1 ? names[1] : ""; string fatherName = names.Length > 2 ? names[2] : ""; string sex = radioMen.IsChecked ?? true ? "М" : "Ж"; string[] bDayArr = tbBDay.Text.Split('.'); DateTime bDay = new DateTime(int.Parse(bDayArr[2]), int.Parse(bDayArr[1]), int.Parse(bDayArr[0])); string phone = tbPhone.Text; long userRoleId = sqlite.GetIdByValue(SQLite.TABLE_USER_ROLE, comboBoxUserRole.Text); long sportTypeId = sqlite.GetIdByValue(SQLite.TABLE_SPORT_TYPE, comboBoxSportType.Text); long sportCategoryId = sqlite.GetIdByValue(SQLite.TABLE_SPORT_CATEGORY, comboBoxSportCategory.Text); string address = tbAddress.Text; string place = tbPlace.Text; string login = ""; User u = new User(0, name, lastName, fatherName, login, pass, phone, sex, DateTime.Now.ToShortDateString(), bDay.ToShortDateString()); sqlite.AddUser(u); long userId = sqlite.GetIdByUserInfo(u); switch (userRoleId) { case 1: { } break; case 2: { sqlite.AddAthlete(new Athlete(0, (byte)sportTypeId, (byte)sportCategoryId, address, place, userId, DateTime.Now.ToShortDateString())); } break; case 3: { } break; } } else MessageBox.Show("Пароли не совпадают!", "Повторите ввод", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); } catch (Exception ex) { ErrorsHandler.ShowError(ex); } }
/// <summary> /// Загружает информацию о текущем пользователе в контролы /// </summary> private void LoadUser() { user = sqlite.GetUser(userId); athlete = sqlite.GetAthleteByUserId(user.id); tbName.Text = string.Format("{0} {1} {2}", user.name, user.lastName, user.fatherName); tbBDay.Text = user.bDay; tbAddress.Text = athlete.address; tbPhone.Text = user.phone; tbPlace.Text = athlete.place; cbSportCategory.Text = sqlite.GetValueById(SQLite.TABLE_SPORT_CATEGORY, athlete.sportCategoryId); cbSportType.Text = sqlite.GetValueById(SQLite.TABLE_SPORT_TYPE, athlete.sportTypeId); cbSex.Text = user.sex; tbLogin.Text = user.login; tbPassword.Password = user.password; }
/// <summary> /// Возвращает id пользователя по всей его информации /// </summary> /// <param name="user"></param> /// <returns></returns> public long GetIdByUserInfo(User user) { using (SQLiteCommand command = connection.CreateCommand()) { command.CommandText = string.Format("SELECT id FROM {0} WHERE name = '{1}' AND lastName = '{2}' AND fatherName = '{3}' AND phone = '{4}' AND birthday = {5} AND sex = '{6}' AND password = '******' AND createDate = {8};", TABLE_USER, user.name, user.lastName, user.fatherName, user.phone, user.birthDayInt64, user.sex, user.password, user.createDateInt64); DbDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { return reader.GetInt64(0); } } } return -1; }