DbConnection Get() { if (QueryOutFormat != EFormat.Console) { QueryOutFile.Refresh(); if (QueryOutFile == null) { throw (new Exception(QueryOutFormat.ToString() + " Format require set 'QueryOutFile' Property")); } } switch (ServerType) { case EType.Oracle: return(new OracleConnection(ConnectionString)); case EType.SqlServer: return(new SqlConnection(ConnectionString)); case EType.OleDb: return(new OleDbConnection(ConnectionString)); case EType.Odbc: return(new OdbcConnection(ConnectionString)); case EType.MySql: return(new MySqlConnection(ConnectionString)); } return(null); }
public DbConnection Get() { if (QueryOutFormat != EFormat.Console) { QueryOutFile.Refresh(); if (QueryOutFile == null) { throw (new Exception(QueryOutFormat.ToString() + " Format require set 'QueryOutFile' Property")); } } return(DBHelper.GetDB(ServerType, ConnectionString)); }
public override bool Run() { DbConnection db = Get(); if (db == null) { return(false); } WriteInfo("Connecting ..."); using (db) { try { db.Open(); } catch (Exception e) { WriteError(e.Message); return(false); } using (DbCommand cmd = db.CreateCommand()) { cmd.CommandText = Query; if (IsQuery(Query)) { WriteInfo("Detecting Query Type"); // Query cmd.CommandType = CommandType.Text; using (DataTable dt = new DataTable() { TableName = Query }) { using (DbDataAdapter adapter = DbProviderFactories.GetFactory(db).CreateDataAdapter()) { adapter.SelectCommand = cmd; adapter.Fill(dt); } CommandTable table = new CommandTable(dt); switch (QueryOutFormat) { case EFormat.Console: { WriteTable(table); break; } case EFormat.Txt: { File.WriteAllText(QueryOutFile.FullName, table.Output()); break; } case EFormat.Xml: { dt.WriteXml(QueryOutFile.FullName, XmlWriteMode.WriteSchema, true); break; } } if (QueryOutFormat != EFormat.Console) { QueryOutFile.Refresh(); WriteInfo("OutFile Size: ", QueryOutFile.Length.ToString(), QueryOutFile.Length <= 0 ? ConsoleColor.Red : ConsoleColor.Green); } WriteInfo("Rows : ", dt.Rows.Count.ToString(), dt.Rows.Count <= 0 ? ConsoleColor.Red : ConsoleColor.Green); WriteInfo("Columns : ", dt.Columns.Count.ToString(), dt.Columns.Count <= 0 ? ConsoleColor.Red : ConsoleColor.Green); } } else { // Non query WriteInfo("Detecting Non-Query Type"); int x = cmd.ExecuteNonQuery(); WriteInfo("Affected rows", x.ToString(), x <= 0 ? ConsoleColor.Red : ConsoleColor.Green); } WriteInfo("Disconnecting"); db.Close(); } } return(true); }