private static void Main(string[] args) { if (args.Length != 5) { if ((args.Length != 5) && (args.Length != 0)) { showUsage(); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new formMain()); } else { OdbcDataReader reader2; string dsn = args[0]; string username = args[1]; string password = args[2]; string path = args[3]; string str5 = args[4]; OdbcConnectionManager manager = new OdbcConnectionManager(dsn, username, password); StreamReader reader = new StreamReader(path); OdbcCommand command = new OdbcCommand(reader.ReadLine(), manager.cnn); DataTable table = new DataTable(); try { reader2 = command.ExecuteReader(); } catch (Exception exception) { Cursor.Current = Cursors.Default; MessageBox.Show("Error executing command\n\n" + exception.Message); return; } try { table.Load(reader2); } catch (OutOfMemoryException) { MessageBox.Show("Error: Out of memory", "NetTools: ODBCBrowse", MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } StreamWriter writer = new StreamWriter(str5); string str7 = ""; foreach (DataColumn column in table.Columns) { writer.Write(str7); if (column.ColumnName.Contains(",") || column.ColumnName.Contains("\"") || column.ColumnName.Contains("\n")) { writer.Write("\""); writer.Write(column.ColumnName.Replace("\"", "\"\"").Replace("\r", " ").Replace("\n", " ")); writer.Write("\""); } else { writer.Write(column.ColumnName); } str7 = ","; } writer.WriteLine(); ExportProgress progress = new ExportProgress(); progress.updateProgressMin(1); progress.updateProgressMax(table.Rows.Count); progress.Show(); int num = 1; foreach (DataRow row in table.Rows) { str7 = ""; foreach (object obj2 in row.ItemArray) { writer.Write(str7); if (obj2.ToString().Contains(",") || obj2.ToString().Contains("\"") || obj2.ToString().Contains("\n")) { writer.Write("\""); writer.Write(obj2.ToString().Trim().Replace("\"", "\"\"").Replace("\r", " ").Replace("\n", " ")); writer.Write("\""); } else { writer.Write(obj2.ToString().Trim()); } str7 = ","; } writer.WriteLine(); progress.updateProgressBar(num); num++; } writer.Close(); } }
private void exportToCSVToolStripMenuItem_Click(object sender, EventArgs e) { if (this.dataTable.Rows.Count < 1) { MessageBox.Show("Error: No data to export", "NetTools: ODBCBrowse"); } else { SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = "csv"; dialog.Filter = "CSV file (*.csv)|*.csv"; dialog.AddExtension = true; dialog.RestoreDirectory = true; dialog.Title = "Export Data to CSV"; dialog.InitialDirectory = "C:/"; if (dialog.ShowDialog() != DialogResult.OK) { dialog.Dispose(); dialog = null; } else { StreamWriter writer = new StreamWriter(dialog.FileName); string str = ""; foreach (DataColumn column in this.dataTable.Columns) { writer.Write(str); if (column.ColumnName.Contains(",") || column.ColumnName.Contains("\"") || column.ColumnName.Contains("\n")) { writer.Write("\""); writer.Write(column.ColumnName.Replace("\"", "\"\"").Replace("\r", " ").Replace("\n", " ")); writer.Write("\""); } else { writer.Write(column.ColumnName); } str = ","; } writer.WriteLine(); ExportProgress progress = new ExportProgress(); progress.updateProgressMin(1); progress.updateProgressMax(this.dataTable.Rows.Count); progress.Show(); int num = 1; foreach (DataRow row in this.dataTable.Rows) { str = ""; foreach (object obj2 in row.ItemArray) { writer.Write(str); if (obj2.ToString().Contains(",") || obj2.ToString().Contains("\"") || obj2.ToString().Contains("\n")) { writer.Write("\""); writer.Write(obj2.ToString().Trim().Replace("\"", "\"\"").Replace("\r", " ").Replace("\n", " ")); writer.Write("\""); } else { writer.Write(obj2.ToString().Trim()); } str = ","; } writer.WriteLine(); progress.updateProgressBar(num); num++; } writer.Close(); progress.Close(); progress.Dispose(); progress = null; this.lblStatus.Text = "Export to '" + dialog.FileName + "' complete"; } } }