// **** контролы **** //m_textbox1 добавить //m_textbox2 переименовать //m_combo1 source //m_combo2 destination //---------------------------------------------------------------------------------------------------- //Загрузка комбы папок void LoadFoldersToCombo(ComboBox combo) { bool is_from = (combo == m_combo1); bool is_to = (combo == m_combo2); // Сохранить SELECTOR_FOLDER... int saved__SELECTOR_FOLDER = SELECTOR_FOLDER; int saved__SELECTOR_FOLDER2 = SELECTOR_FOLDER2; bool saved__SELECTOR_FOLDER__is_enabled = SELECTOR_FOLDER__is_enabled; bool saved__SELECTOR_FOLDER2__is_enabled = SELECTOR_FOLDER2__is_enabled; // Сначала очистить: combo.Items.Clear(); combo.SelectedIndex = -1; // Теперь загрузить: //1. NULL System.Data.SqlServerCe.SqlCeCommand sqlQuery = DB.CreateQuery(); sqlQuery.CommandText = "SELECT COUNT(id) FROM Games WHERE fk_Folder_id is NULL"; object o = DB.ExecuteScalar(sqlQuery); int fid = -1; string fname = "- нет папки -"; if (o != null && o != DBNull.Value) { fname += " (" + (int)o + ")"; } int index = combo.Items.Add(new comboitem_id_name(fid, fname)); // Выбрать: if (is_from && saved__SELECTOR_FOLDER__is_enabled && saved__SELECTOR_FOLDER == fid || is_to && saved__SELECTOR_FOLDER2__is_enabled && saved__SELECTOR_FOLDER2 == fid) { combo.SelectedIndex = index; } // 2. остальные sqlQuery = DB.CreateQuery(); sqlQuery.CommandText = "SELECT f.id as [fid], f.Name as [fname], COUNT(g.id) as [fcount] FROM Folders f LEFT JOIN Games g ON f.id=g.fk_Folder_id GROUP BY f.id, f.Name ORDER BY [fname]"; System.Data.SqlServerCe.SqlCeDataReader sqlReader = DB.ExecuteReader(sqlQuery); while (sqlReader.Read()) { fid = sqlReader.IsDBNull(sqlReader.GetOrdinal("fid")) ? -1 : sqlReader.GetInt32(sqlReader.GetOrdinal("fid")); fname = ""; if (fid == -1) { fname = "- нет папки -"; } if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("fname"))) { fname = sqlReader.GetString(sqlReader.GetOrdinal("fname")); } fname += " (" + (sqlReader.IsDBNull(sqlReader.GetOrdinal("fcount")) ? 0 : sqlReader.GetInt32(sqlReader.GetOrdinal("fcount"))) + ")"; index = combo.Items.Add(new comboitem_id_name(fid, fname)); // Выбрать: if (is_from && saved__SELECTOR_FOLDER__is_enabled && saved__SELECTOR_FOLDER == fid || is_to && saved__SELECTOR_FOLDER2__is_enabled && saved__SELECTOR_FOLDER2 == fid) { combo.SelectedIndex = index; } } sqlReader.Close(); }
bool CloneGame(int gameId) { System.Data.SqlServerCe.SqlCeTransaction trans = null; System.Data.SqlServerCe.SqlCeCommand sqlQuery = null; System.Data.SqlServerCe.SqlCeDataReader sqlReader = null; System.Data.SqlServerCe.SqlCeDataReader sqlReader2 = null; try { int comm_maxlen = DB.DB_GetMaxLength("Games", "Comment"); // Склонировать (используя транзакцию) sqlQuery = DB.CreateQuery(); trans = DB.sqlConnection.BeginTransaction(); sqlQuery.Transaction = trans; sqlQuery.CommandText = "INSERT INTO Games(Type, GameOptions, DealsInMatch, FirstDealer, ZoneSwims, fk_Folder_id, fk_N, fk_S, fk_E, fk_W, Place, Comment, StartDate) (SELECT Type, GameOptions, DealsInMatch, FirstDealer, ZoneSwims, fk_Folder_id, fk_N, fk_S, fk_E, fk_W, Place, (CASE WHEN Comment is NULL THEN '{клон}' ELSE (SUBSTRING(Comment, 1, (" + comm_maxlen + " - LEN(' {клон}'))) + ' {клон}') END) as Comment_clon, StartDate FROM Games WHERE id=" + gameId + ")"; if (sqlQuery.ExecuteNonQuery() == 0) { trans.Rollback(); MessageBox.Show("Игра #" + gameId + " не была склонирована!", "db error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(false); } int new_gameId = DB.GetLastInsertId(trans); sqlQuery.CommandText = "SELECT id FROM Matches WHERE fk_Game_id=" + gameId + " ORDER BY id"; sqlReader = sqlQuery.ExecuteReader(); while (sqlReader.Read()) { int cur_matchId = sqlReader.GetInt32(0); sqlQuery.CommandText = "INSERT INTO Matches(fk_Game_id, SCORE_NS, SCORE_EW) (SELECT " + new_gameId + " as fk_Game_id__new, SCORE_NS, SCORE_EW FROM Matches WHERE id=" + cur_matchId + ")"; if (sqlQuery.ExecuteNonQuery() == 0) { sqlReader.Close(); trans.Rollback(); MessageBox.Show("Игра #" + gameId + " не была склонирована!\nОшибка клонирования матча #" + cur_matchId + "!", "db error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(false); } int new_cur_matchId = DB.GetLastInsertId(trans); /****int added = 0; * sqlQuery.CommandText = "INSERT INTO Deals_Rob(fk_Match_id, Pair, Contract, Oners, Result, CardsDistribution) (SELECT " + new_cur_matchId + " as fk_Match_id__new, Pair, Contract, Oners, Result, CardsDistribution FROM Deals_Rob WHERE fk_Match_id=" + cur_matchId + ")"; * added = sqlQuery.ExecuteNonQuery(); * * sqlQuery.CommandText = "INSERT INTO Deals_Sport(fk_Match_id, Pair, Contract, Result, CardsDistribution, Figures, Fits, StrongestPair) (SELECT " + new_cur_matchId + " as fk_Match_id__new, Pair, Contract, Result, CardsDistribution, Figures, Fits, StrongestPair FROM Deals_Sport WHERE fk_Match_id=" + cur_matchId + ")"; * added = sqlQuery.ExecuteNonQuery(); * * sqlQuery.CommandText = "INSERT INTO Deals_Double(fk_Match_id, CardsDistribution, Pair1, Contract1, Result1, Pair2, Contract2, Result2, IsSecondStarted) (SELECT " + new_cur_matchId + " as fk_Match_id__new, CardsDistribution, Pair1, Contract1, Result1, Pair2, Contract2, Result2, IsSecondStarted FROM Deals_Double WHERE fk_Match_id=" + cur_matchId + ")"; * added = sqlQuery.ExecuteNonQuery();*****/ int cur_dealId = -1; sqlQuery.CommandText = "SELECT id FROM Deals_Rob WHERE fk_Match_id=" + cur_matchId + " ORDER BY id"; sqlReader2 = sqlQuery.ExecuteReader(); while (sqlReader2.Read()) { cur_dealId = (int)sqlReader2.GetSqlInt32(0); sqlQuery.CommandText = "INSERT INTO Deals_Rob(fk_Match_id, Pair, Contract, Oners, Result, CardsDistribution) (SELECT " + new_cur_matchId + " as fk_Match_id__new, Pair, Contract, Oners, Result, CardsDistribution FROM Deals_Rob WHERE id=" + cur_dealId + ")"; if (sqlQuery.ExecuteNonQuery() == 0) { sqlReader2.Close(); sqlReader.Close(); trans.Rollback(); MessageBox.Show("Игра #" + gameId + " не была склонирована!\nОшибка клонирования сдачи #" + cur_dealId + " матча #" + cur_matchId + "!", "db error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(false); } } sqlReader2.Close(); sqlQuery.CommandText = "SELECT id FROM Deals_Sport WHERE fk_Match_id=" + cur_matchId + " ORDER BY id"; sqlReader2 = sqlQuery.ExecuteReader(); while (sqlReader2.Read()) { cur_dealId = (int)sqlReader2.GetSqlInt32(0); sqlQuery.CommandText = "INSERT INTO Deals_Sport(fk_Match_id, Pair, Contract, Result, CardsDistribution, Figures, Fits, StrongestPair) (SELECT " + new_cur_matchId + " as fk_Match_id__new, Pair, Contract, Result, CardsDistribution, Figures, Fits, StrongestPair FROM Deals_Sport WHERE id=" + cur_dealId + ")"; if (sqlQuery.ExecuteNonQuery() == 0) { sqlReader2.Close(); sqlReader.Close(); trans.Rollback(); MessageBox.Show("Игра #" + gameId + " не была склонирована!\nОшибка клонирования сдачи #" + cur_dealId + " матча #" + cur_matchId + "!", "db error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(false); } } sqlReader2.Close(); sqlQuery.CommandText = "SELECT id FROM Deals_Double WHERE fk_Match_id=" + cur_matchId + " ORDER BY id"; sqlReader2 = sqlQuery.ExecuteReader(); while (sqlReader2.Read()) { cur_dealId = (int)sqlReader2.GetSqlInt32(0); sqlQuery.CommandText = "INSERT INTO Deals_Double(fk_Match_id, CardsDistribution, Pair1, Contract1, Result1, Pair2, Contract2, Result2, IsSecondStarted) (SELECT " + new_cur_matchId + " as fk_Match_id__new, CardsDistribution, Pair1, Contract1, Result1, Pair2, Contract2, Result2, IsSecondStarted FROM Deals_Double WHERE id=" + cur_dealId + ")"; if (sqlQuery.ExecuteNonQuery() == 0) { sqlReader2.Close(); sqlReader.Close(); trans.Rollback(); MessageBox.Show("Игра #" + gameId + " не была склонирована!\nОшибка клонирования сдачи #" + cur_dealId + " матча #" + cur_matchId + "!", "db error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(false); } } sqlReader2.Close(); } sqlReader.Close(); // Добавить в список sqlQuery.CommandText = "SELECT g.id, g.StartDate, g.Place, g.Comment, g.Type, g.DealsInMatch, g.GameOptions, n.Name as N, s.Name as S, e.Name as E, w.Name as W FROM Games g LEFT JOIN Players n ON g.fk_N = n.id LEFT JOIN Players s ON g.fk_S = s.id LEFT JOIN Players e ON g.fk_E = e.id LEFT JOIN Players w ON g.fk_W = w.id WHERE g.id=" + new_gameId; sqlReader = sqlQuery.ExecuteReader(); if (sqlReader.Read()) { Load1Game(null, sqlReader); sqlReader.Close(); trans.Commit(); LoadFoldersToCombo(true); // перезагрузить список папок MessageBox.Show("Игра #" + gameId + " успешно склонирована в #" + new_gameId, "Клонирование успешно", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1); return(true); } else { sqlReader.Close(); trans.Rollback(); MessageBox.Show("Игра #" + gameId + " не была склонирована в #" + new_gameId, "Клонирование неудачно", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return(false); } } catch (System.Data.SqlServerCe.SqlCeException ex) { if (trans != null) { DB.SafeTransRollback(trans); } if (sqlReader != null && sqlReader.IsClosed == false) { sqlReader.Close(); } if (sqlReader2 != null && sqlReader2.IsClosed == false) { sqlReader2.Close(); } if (ex.NativeError == 0) { return(false); } else { throw; } } }
// Загрузка 1 игры в список void Load1Game(ListViewItem lvi, System.Data.SqlServerCe.SqlCeDataReader sqlReader) { bool update = (lvi != null); //обновить существующий lvi или добавить новый? // Комментарий String str = ""; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("Comment"))) { str = sqlReader.GetString(sqlReader.GetOrdinal("Comment")).Trim(); } if (update) { lvi.Text = str; } else { lvi = m_listview_Games.Items.Add(new ListViewItem(str)); } // Дата str = ""; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("StartDate"))) { DateTime dt = sqlReader.GetDateTime(sqlReader.GetOrdinal("StartDate")); str = dt.ToString("dd.MM.yyyy HH") + "ч"; } if (update) { lvi.SubItems[1].Text = str; } else { lvi.SubItems.Add(str); } // id (тэг) if (!update) { lvi.Tag = sqlReader.GetInt32(sqlReader.GetOrdinal("id")); } // Тип GameType t = (GameType)sqlReader.GetByte(sqlReader.GetOrdinal("Type")); if (!update) { switch (t) { case GameType.Robber: lvi.SubItems.Add("Робб"); break; case GameType.Sport: lvi.SubItems.Add("Спорт"); break; case GameType.Compensat: lvi.SubItems.Add("Комп"); break; case GameType.SimpleIMP: lvi.SubItems.Add("IMP"); break; } } // Строка опций: if (!update) { ArrayOfString arrOptions = new ArrayOfString(); if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("DealsInMatch"))) { arrOptions.Add(sqlReader.GetByte(sqlReader.GetOrdinal("DealsInMatch")).ToString() + "x"); } if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("GameOptions"))) { byte GOptions = sqlReader.GetByte(sqlReader.GetOrdinal("GameOptions")); if (t == GameType.Robber) { bool BonusForOners = ((GOptions & 1) > 0); bool BonusForWholeRobber = ((GOptions & 2) > 0); arrOptions.Add(BonusForWholeRobber ? "за робб" : "за гейм"); if (BonusForOners) { arrOptions.Add("за онеры"); } } if (t == GameType.Compensat) { BridgeGameScoring.TypeOfCompensation CompType = (BridgeGameScoring.TypeOfCompensation)(GOptions & 3); //2 bits bool TenCardsIsTwoFits = ((GOptions & 4) > 0); //3rd bit bool LessCompFor2Fits23PC = ((GOptions & 8) > 0); //4th bit switch (CompType) { case BridgeGameScoring.TypeOfCompensation.Chicago: arrOptions.Add("чикаг"); break; case BridgeGameScoring.TypeOfCompensation.Europe: arrOptions.Add("европ"); if (TenCardsIsTwoFits) { arrOptions.Add("10 за 2"); } if (LessCompFor2Fits23PC) { arrOptions.Add("< 23-2"); } break; case BridgeGameScoring.TypeOfCompensation.Milton_York: arrOptions.Add("М-У"); break; case BridgeGameScoring.TypeOfCompensation.Moscow: arrOptions.Add("моск"); break; } } } str = ""; if (arrOptions.Count != 0) { for (int j = 0; j < arrOptions.Count; j++) { if (j > 0) { str += ", "; } str += arrOptions[j]; } } lvi.SubItems.Add(str); } // Место str = ""; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("Place"))) { str = sqlReader.GetString(sqlReader.GetOrdinal("Place")).Trim(); } if (update) { lvi.SubItems[4].Text = str; } else { lvi.SubItems.Add(str); } // Игроки str = ""; str += "N: "; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("N"))) { str += sqlReader.GetString(sqlReader.GetOrdinal("N")).Trim(); } str += ", S: "; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("S"))) { str += sqlReader.GetString(sqlReader.GetOrdinal("S")).Trim(); } str += ", E: "; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("E"))) { str += sqlReader.GetString(sqlReader.GetOrdinal("E")).Trim(); } str += ", W: "; if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("W"))) { str += sqlReader.GetString(sqlReader.GetOrdinal("W")).Trim(); } if (update) { lvi.SubItems[5].Text = str; } else { lvi.SubItems.Add(str); } }
//Загрузка комбы папок void LoadFoldersToCombo(bool reload) { // Сначала очистить: m_combo_Folders.Items.Clear(); // Теперь загрузить: //1. NULL System.Data.SqlServerCe.SqlCeCommand sqlQuery = DB.CreateQuery(); sqlQuery.CommandText = "SELECT COUNT(id) FROM Games WHERE fk_Folder_id is NULL"; object o = DB.ExecuteScalar(sqlQuery); int fid = -1; string fname = "- нет папки -"; if (o != null && o != DBNull.Value) { fname += " (" + (int)o + ")"; } int index = m_combo_Folders.Items.Add(new comboitem_id_name(fid, fname)); // Выбрать: if (SELECTOR_FOLDER__is_enabled && SELECTOR_FOLDER == fid) { if (reload) { dont_react_on_folder_selection = true; } m_combo_Folders.SelectedIndex = index; if (reload) { dont_react_on_folder_selection = false; } } // 2. остальные sqlQuery = DB.CreateQuery(); sqlQuery.CommandText = "SELECT f.id as [fid], f.Name as [fname], COUNT(g.id) as [fcount] FROM Folders f LEFT JOIN Games g ON f.id=g.fk_Folder_id GROUP BY f.id, f.Name ORDER BY [fname]"; System.Data.SqlServerCe.SqlCeDataReader sqlReader = DB.ExecuteReader(sqlQuery); while (sqlReader.Read()) { fid = sqlReader.IsDBNull(sqlReader.GetOrdinal("fid")) ? -1 : sqlReader.GetInt32(sqlReader.GetOrdinal("fid")); fname = ""; if (fid == -1) { fname = "- нет папки -"; } if (!sqlReader.IsDBNull(sqlReader.GetOrdinal("fname"))) { fname = sqlReader.GetString(sqlReader.GetOrdinal("fname")); } fname += " (" + (sqlReader.IsDBNull(sqlReader.GetOrdinal("fcount")) ? 0 : sqlReader.GetInt32(sqlReader.GetOrdinal("fcount"))) + ")"; index = m_combo_Folders.Items.Add(new comboitem_id_name(fid, fname)); // Выбрать: if (SELECTOR_FOLDER__is_enabled && SELECTOR_FOLDER == fid) { if (reload) { dont_react_on_folder_selection = true; } m_combo_Folders.SelectedIndex = index; if (reload) { dont_react_on_folder_selection = false; } } } sqlReader.Close(); }