public void FillByGroupId(int group_id) { List <RuleDB> list_objects = new List <RuleDB>(); SqlConnection connection = new SqlConnection(connectionString); try { connection.Open(); SqlDataReader dataReader; string sql = "SELECT * FROM rules WHERE group_id = '" + group_id.ToString() + "';"; SqlCommand command = new SqlCommand(sql, connection); dataReader = command.ExecuteReader(); this.number = 0; while (dataReader.Read()) { RuleDB obj = new RuleDB(); obj.id = Convert.ToInt32(dataReader.GetValue(0)); obj.name = Convert.ToString(dataReader.GetValue(1)); obj.description = Convert.ToString(dataReader.GetValue(2)); obj.raspisanie_type_id = Convert.ToInt32(dataReader.GetValue(3)); obj.time_start = Convert.ToDateTime(dataReader.GetValue(4)); obj.time_finish = Convert.ToDateTime(dataReader.GetValue(5)); obj.number_attempt = Convert.ToInt32(dataReader.GetValue(6)); obj.admin_id = Convert.ToInt32(dataReader.GetValue(7)); if (Convert.ToInt32(dataReader.GetValue(8)) == this.group_id_show) { } else { obj.group_id = Convert.ToInt32(dataReader.GetValue(8)); this.group_id_show = obj.group_id; } list_objects.Add(obj); this.number += 1; obj = null; } connection.Close(); } catch (Exception ex) { this.number = 0; } objects = list_objects; }
public void AddToDB(bool for_test, List <int> list_id) { //for_test = true - правило для теста //for_test = false - правило для пользователя SqlConnection connection = new SqlConnection(connectionString); //try //{ // 1. Создаём правило в таблице rules string sql_rules = "INSERT INTO rules(" + "name," + " description," + " raspisanie_type_id," + " time_start," + " time_finish," + " number_attempt," + " admin_id," + " group_id" + ") VALUES('" + this.name + "', '" + this.description + "', '" + this.raspisanie_type_id + "', '" + this.time_start + "', '" + this.time_finish + "', '" + this.number_attempt + "', '" + this.admin_id + "', '" + this.group_id + "');"; SqlCommand command = new SqlCommand(sql_rules, connection); connection.Open(); command.ExecuteNonQuery(); // Получим ID только что созданного правила RuleDB rdb = new RuleDB(this.name); int id_rules = rdb.id; rdb = null; // 2. Привязываем правило к тестам в таблице rl_tests if (for_test == true) { // Получаем все id тестов из данной группы string sql_rl_tests = ""; for (int i = 0; i < list_id.Count(); i++) { sql_rl_tests = "INSERT INTO rl_tests(rules_id, test_id) VALUES('" + id_rules.ToString() + "', '" + list_id[i].ToString() + "');"; command = new SqlCommand(sql_rl_tests, connection); command.ExecuteNonQuery(); } } // 2. Привязываем правило к пользователям в таблице rl_users if (for_test == false) { // Получаем все id пользователей из данной группы string sql_rl_users = ""; for (int i = 0; i < list_id.Count(); i++) { sql_rl_users = "INSERT INTO rl_users(rules_id, user_id) VALUES('" + id_rules + "', '" + list_id[i].ToString() + "');"; command = new SqlCommand(sql_rl_users, connection); command.ExecuteNonQuery(); } } connection.Close(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} }