private bool SaveFile(string tableName) { var tbl = StaticReference.GetTableByFullName(tableName); if (tbl == null) { return(true); } tbl.Save(); // save tbl.Altered = false; if (tcMain.SelectedTabPage.Text.Contains("(*)")) { tcMain.SelectedTabPage.Text = tcMain.SelectedTabPage.Text.Replace("(*)", ""); } var page = GetTabPageByFilename(tableName); var view = GetViewFromTabPage(page); if (_editedCells.ContainsKey(view)) { _editedCells.Remove(view); view.Invalidate(); } ClearNewRowHighlight(view); StaticReference.ShowInformation(this, LanguageManager.Get("Message_SaveSuccess")); return(true); }
private void btnSave_Click(object sender, EventArgs e) { using (var sfd = new SaveFileDialog()) { sfd.Title = @"Select path and enter filename"; sfd.Filter = @"Knight OnLine data tables |*.tbl"; sfd.OverwritePrompt = true; if (DialogResult.OK == sfd.ShowDialog()) { var tbl = StaticReference.GetTableByFullName(_tableName); if (tbl == null) { StaticReference.ShowError(this, LanguageManager.Get("Message_FileNotOpen")); Close(); return; } foreach (var v in StaticReference.EncryptionMethods.Where(v => string.Compare(v.Name(), cbEncryptionList.Text, StringComparison.Ordinal) == 0)) { tbl.SetEncryption(v); } tbl.SaveAs(sfd.FileName); StaticReference.ShowInformation(this, LanguageManager.Get("Message_Done")); Close(); // tePath.Text = sfd.FileName; } } }
private void bgwFindNext_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { while (true && !bgwFindNext.CancellationPending) { mre.WaitOne(); mre.Reset(); IEnumerable <indexColumnPair> resultSet = findNextMatch(false); IEnumerator <indexColumnPair> resultEnum = resultSet.GetEnumerator(); if (!resultEnum.MoveNext() || resultEnum.Current == null) { this.Invoke(new Action(() => { StaticReference.ShowInformation(this, Auxillary.LanguageManager.Get("Message_NoMoreMatches")); })); } else { do { // MessageBox.Show(_gridView.GetRowCellValue(resultEnum.Current.index, resultEnum.Current.m_column).ToString()); _parent.Invoke(new Action(() => { _gridView.FocusedRowHandle = resultEnum.Current.index; _gridView.FocusedColumn = resultEnum.Current.m_column; _gridView.SetRowCellValue(resultEnum.Current.index, resultEnum.Current.m_column, teReplaceValue.Text); })); mre.WaitOne(); mre.Reset(); }while (resultEnum.MoveNext() && !bgwFindNext.CancellationPending); this.Invoke(new Action(() => { StaticReference.ShowInformation(this, Auxillary.LanguageManager.Get("Message_NoMoreMatches")); })); } } }
private void deleteRowToolStripMenuItem_Click(object sender, EventArgs e) { if (tcMain.SelectedTabPageIndex == -1 || tcMain.SelectedTabPage == null) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_NoFileOpen")); return; } string fileName = tcMain.SelectedTabPage.Tag as string; var tbl = StaticReference.GetTableByFullName(fileName); GridView view = GetViewFromSelectedTabPage(); if (view == null || tbl == null) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_SelectPageFirst")); return; } if (view.SelectedRowsCount == 0) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_NoRowsSelected")); return; } if (StaticReference.ShowQuestion(this, string.Format(LanguageManager.Get("Message_DeleteConfirm"), view.SelectedRowsCount)) == DialogResult.Yes) { List <DataRow> selectedRows = new List <DataRow>(); for (int i = 0; i < view.SelectedRowsCount; i++) { selectedRows.Add(tbl.Table.Rows[view.GetSelectedRows()[i]]); } if (_editedCells.ContainsKey(view)) { foreach (var q in view.GetSelectedRows()) { _editedCells[view].RemoveAll(i => i.Key == q); } view.Invalidate(); } foreach (var t in selectedRows) { tbl.Table.Rows.Remove(t); RemoveNewRowHighlight(view, t); } view.ClearSelection(); StaticReference.ShowInformation(this, string.Format(LanguageManager.Get("Message_DeleteSuccess"), selectedRows.Count)); } }
private void btnRepAll_Click(object sender, EventArgs e) { int replacementCount = 0; foreach (var v in findNextMatch(true)) { _parent.Invoke(new Action(() => { _gridView.FocusedRowHandle = v.index; _gridView.FocusedColumn = v.m_column; string rowCellValue = Convert.ToString(_gridView.GetRowCellValue(v.index, v.m_column)); rowCellValue = rowCellValue.Replace(teFindValue.Text, teReplaceValue.Text); _gridView.SetRowCellValue(v.index, v.m_column, rowCellValue); })); replacementCount++; } this.Invoke(new Action(() => { StaticReference.ShowInformation(this, string.Format(Auxillary.LanguageManager.Get("Message_NMatchesReplaced"), replacementCount)); })); }
private void btnStart_Click(object sender, EventArgs e) { if (_loadedTables.Count == 0) { StaticReference.ShowWarning(this, "There are no loaded tables to convert."); return; } if (string.IsNullOrEmpty(_destinationFolder)) { StaticReference.ShowWarning(this, "Destination folder is not set."); return; } if (!Directory.Exists(_destinationFolder)) { try { Directory.CreateDirectory(_destinationFolder); } catch (Exception ex) { StaticReference.ShowWarning(this, $"An exception occured while creating destination directory.\nException : {ex.Message}"); return; } } var encMethod = StaticReference.EncryptionMethods.FirstOrDefault(v => string.Compare(v.Name(), cbEncryption.Text, StringComparison.Ordinal) == 0); if (encMethod == null) { StaticReference.ShowWarning(this, $"Encryption load error."); return; } foreach (var tbl in _loadedTables) { meLog.Text += $"Saving {_destinationFolder}\\{tbl.Value.Name}" + Environment.NewLine; tbl.Value.SetEncryption(encMethod); tbl.Value.SaveAs($"{_destinationFolder}\\{tbl.Value.Name}"); } StaticReference.ShowInformation(this, "Done."); }
private void copyRowToolStripMenuItem_Click(object sender, EventArgs e) { if (tcMain.SelectedTabPageIndex == -1 || tcMain.SelectedTabPage == null) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_NoFileOpen")); return; } string fileName = tcMain.SelectedTabPage.Tag as string; var tbl = StaticReference.GetTableByFullName(fileName); GridView view = GetViewFromSelectedTabPage(); if (view == null || tbl == null) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_SelectPageFirst")); return; } // MessageBox.Show("ctrlc"); if (view.SelectedRowsCount == 0) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_NoRowsSelected")); return; } List <DataRow> drList = new List <DataRow>(); for (int i = 0; i < view.SelectedRowsCount; i++) { drList.Add(tbl.Table.Rows[view.GetSelectedRows()[i]]); //view.GetSelectedRows().cl } /* Copy the content of selected rows to the internal clipboard. */ InternalClipboard.Copy(ref drList); view.ClearSelection(); StaticReference.ShowInformation(this, string.Format(LanguageManager.Get("Message_NRowsCopied"), InternalClipboard.GetSize())); }
private void simpleButton1_Click(object sender, System.EventArgs e) { if (clbColumns.CheckedItems.Count == 0) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_NoCheckedItems")); return; } using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Filter = LanguageManager.Get("frmExportAsSQL_Filter"); if (sfd.ShowDialog() == DialogResult.OK) { /* Let's begin */ StringBuilder insertQuery = new StringBuilder(); StringBuilder createQuery = new StringBuilder(); int batch = 0; if (ceDropTable.Checked) { createQuery.Append($"DROP TABLE {teTableName.Text};\n"); } createQuery.Append($"CREATE TABLE {teTableName.Text}("); if (ceTruncateTable.Checked) { insertQuery.Append($"TRUNCATE TABLE {teTableName.Text};\n"); } insertQuery.Append($"INSERT INTO {teTableName.Text} VALUES \n"); foreach (CheckedListBoxItem clbi in clbColumns.CheckedItems) { string[] zzz = Convert.ToString(clbi.Value).Split(','); int ind = Convert.ToInt32(zzz[0]); TextEdit te = _columnNames[ind]; createQuery.Append(te.Text); switch (_tbl.Table.Columns[ind].DataType.FullName) { case "System.UInt64": case "System.Int64": createQuery.Append($" bigint,\n"); break; case "System.Double": case "System.Single": createQuery.Append($" real,\n"); break; case "System.UInt32": case "System.Int32": createQuery.Append($" int,\n"); break; case "System.UInt16": case "System.Int16": createQuery.Append($" smallint,\n"); break; case "System.SByte": case "System.Byte": createQuery.Append($" tinyint,\n"); break; case "System.String": createQuery.Append($" varchar(MAX),\n"); break; default: createQuery.Append($" unknown(MAX),\n"); break; } /* EOF cell type switch */ } int insertedRows = 0; foreach (DataRow v in _tbl.Table.Rows) { insertQuery.Append("("); foreach (CheckedListBoxItem clbi in clbColumns.CheckedItems) { string[] zzz = Convert.ToString(clbi.Value).Split(','); int ind = Convert.ToInt32(zzz[0]); DataColumn clmn = _tbl.Table.Columns[ind]; if (clmn.DataType.FullName == "System.String") { insertQuery.Append(("'" + v[clmn].ToString().Replace('\'', '’').Replace("\\", "\\\\") + "',")); } else if (clmn.DataType.FullName == "System.Single" || clmn.DataType.FullName == "System.Double") { /* Convert float notation from ',' to '.' */ insertQuery.Append(Convert.ToString(Convert.ToDouble(v[clmn].ToString())).Replace(",", ".") + ","); } else { insertQuery.Append(v[clmn] + ","); } } insertQuery.Remove(insertQuery.Length - 1, 1); insertQuery.Append("),\n"); if (++insertedRows == 999) { /* We need to break it to several parts. */ insertQuery.Remove(insertQuery.Length - 2, 2); insertQuery.Append(";"); File.WriteAllText(sfd.FileName.Replace(".sql", "") + "_data" + (batch++) + ".sql", insertQuery.ToString()); insertQuery.Clear(); /* prepare for next batch */ insertQuery.Append($"INSERT INTO {teTableName.Text} VALUES \n"); insertedRows = 0; } } insertQuery.Remove(insertQuery.Length - 2, 2); insertQuery.Append(";"); createQuery.Remove(createQuery.Length - 2, 2); createQuery.Append(");\n\n"); if (ceCreateTable.Checked) { File.WriteAllText(sfd.FileName.Replace(".sql", "") + "_ddl" + ".sql", createQuery.ToString()); } File.AppendAllText(sfd.FileName.Replace(".sql", "") + "_data" + (batch++) + ".sql", insertQuery.ToString()); } } StaticReference.ShowInformation(this, LanguageManager.Get("Message_ExportComplete")); }
private void pasteRowToolStripMenuItem_Click(object sender, EventArgs e) { if (tcMain.SelectedTabPageIndex == -1 || tcMain.SelectedTabPage == null) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_NoFileOpen")); return; } string fileName = tcMain.SelectedTabPage.Tag as string; KOTableFile tbl = StaticReference.GetTableByFullName(fileName); GridView view = GetViewFromSelectedTabPage(); if (null == tbl || null == view) { StaticReference.ShowWarning(this, LanguageManager.Get("Message_PasteError")); return; } if (InternalClipboard.DataExists()) { /* * Let's check if copied datarow matches the column length of current table * */ int successfulRows = 0; foreach (var copiedRow in InternalClipboard.GetNext()) { /* * 1. Column count of both tables should be same * 2. Data types of columns for both tables should match */ bool column_ok = true; DataRow newRow = tbl.Table.NewRow(); if (tbl.Table.Columns.Count == copiedRow.Table.Columns.Count) { foreach (DataColumn cColumn in copiedRow.Table.Columns) { if (tbl.Table.Columns[cColumn.Ordinal].DataType != cColumn.DataType) { column_ok = false; break; } else { newRow[cColumn.Ordinal] = copiedRow[cColumn]; } } } else { column_ok = false; } if (column_ok) { successfulRows++; /* Create a new row in table */ /* Insert the row.*/ tbl.Table.Rows.Add(newRow); AddNewRowHighlight(view, newRow); } } /* Scroll to bottom */ view.FocusedRowHandle = view.RowCount - 1; view.TopRowIndex = view.RowCount - 1; if (successfulRows == InternalClipboard.GetSize()) { StaticReference.ShowInformation(this, string.Format(LanguageManager.Get("Message_NRowsAddSuccess"), successfulRows)); } else { StaticReference.ShowWarning(this, string.Format(LanguageManager.Get("Message_CopyError"), successfulRows, InternalClipboard.GetSize())); } } else { StaticReference.ShowWarning(this, LanguageManager.Get("Message_PasteNoRows")); } }