Example #1
0
 public string[] GetTables(bool WithViews = true)
 {
     if (Index != -1)
     {
         return(DbCurrent.GetTables(WithViews));
     }
     else
     {
         return(new string[] { });
     }
 }
Example #2
0
        public string UpdateRowToDatabase(DataTable tb, bool WithoutBinding)
        {
            if (WithoutBinding)
            {
                return("");
            }

            try
            {
                System.Data.Common.DbCommandBuilder cmdbldr = DbCurrent.GetCommandBuilder(Adapter);
                Adapter.Update(tb);
                return("");
            }
            catch (Exception ex)
            { return(ex.Message); }
        }
Example #3
0
        public DataTable GetQuery(string Sql, string TableName, int MaxRows, bool ShowErrors = true)
        {
            try
            {
                if (!string.IsNullOrEmpty(Sql))
                {
                    if (MaxRows != 0)
                    {
                        Sql = DbCurrent.GetLimitLines(Sql, MaxRows);
                    }

                    LastSqlQuery = Sql;

                    DbCurrent.DbConnection.Open();

                    if (Adapter != null)
                    {
                        Adapter.Dispose();
                        Adapter = null;
                    }

                    Adapter   = DbCurrent.GetDataAdapter(Sql);
                    LastTable = new DataTable(TableName);
                    Adapter.Fill(LastTable);
                }
                return(LastTable);
            }
            catch (Exception ex)
            {
                if (ShowErrors)
                {
                    Msg.Warning(ex.Message);
                }

                return(null);
            }
            finally
            {
                if (DbCurrent != null)
                {
                    if (DbCurrent.DbConnection != null && this.DbCurrent.DbConnection.State == ConnectionState.Open)
                    {
                        DbCurrent.DbConnection.Close();
                    }
                }
            }
        }
Example #4
0
 public ForeignKey[] GetDependencies()
 {
     return(DbCurrent.GetForeignKeys());
 }
Example #5
0
 public DataTable GetSchema(string CollectionName)
 {
     return(DbCurrent.GetSchema(CollectionName));
 }
Example #6
0
        public string ExecFile(string FileName)
        {
            TextFile tf = new TextFile();

            tf.Open(enmOpenMode.Reading, FileName);

            string buffer          = "";
            int    CountInstrucoes = 0;
            bool   Cancel          = false;
            long   bytes           = 0;
            long   total_bytes     = tf.GetSize();
            string line            = "";

            List <string> identity_tables = DbCurrent.GetIdentityTables();

            DbCurrent.BeginTransaction();

            while ((line = tf.ReadLine()) != null)
            {
                if (Cancel)
                {
                    break;
                }

                bytes += line.Length;

                if (line.Trim() == DbScript.COMMIT_COMMAND || line.Trim() == "GO")
                {
                    DbCurrent.CommitTransaction();
                    DbCurrent.BeginTransaction();
                    continue;
                }

                if (line.Contains("SET IDENTITY_INSERT"))
                {
                    int idx_start = line.IndexOf("SET IDENTITY_INSERT");
                    int idx_end   = line.IndexOf(" ON ");

                    string sub_string   = line.Substring(idx_start, idx_end + 4).ToUpper();
                    string table_script = sub_string.Replace("SET IDENTITY_INSERT ", "").Replace(" ON ", "").Replace("[", "").Replace("]", "");

                    if (DbCurrent.dbType == enmConnection.SqlServer)
                    {
                        string tabela_localizada = identity_tables.FirstOrDefault(q => q.ToUpper() == table_script);
                        if (string.IsNullOrEmpty(tabela_localizada))
                        {
                            line = line.Replace(sub_string, "");
                        }
                    }
                    else
                    {
                        line = line.Replace(sub_string, "");
                    }

                    /*
                     * PrimaryKey[] primaryKey = primarykeys.Where(q => q.Table.ToUpper() == table_script).ToArray();
                     *
                     * if (primaryKey.Length != 1) {
                     *  line = line.Replace(sub_string, "");
                     * }
                     */
                }

                //buffer += " " + line;

                bool PodeExecutar = line.IndexOf(";") != -1;

                if (string.IsNullOrEmpty(buffer))
                {
                    if (PodeExecutar && line.Count(q => q == '\'') % 2 == 1)
                    {
                        PodeExecutar = false;
                    }
                }
                else
                {
                    if (PodeExecutar && (buffer + line).Count(q => q == '\'') % 2 == 1)
                    {
                        PodeExecutar = false;
                    }
                }

                if (PodeExecutar)
                {
                    buffer += " " + (line.Trim() == "GO" ? "" : line);

                    string comando = "";
                    try
                    {
                        string prefix = "";
                        if (DbCurrent.dbType == enmConnection.SqlServer)
                        {
                            prefix = "set dateformat ymd";
                        }

                        comando = prefix + buffer;

                        DbCurrent.Exec(comando);

                        buffer = "";
                        CountInstrucoes++;
                        ScriptProgress("SQL executado:" + CountInstrucoes, (int)(bytes * 1000 / total_bytes), 1000, out Cancel);
                    }
                    catch (Exception ex)
                    {
                        return(GetSqlError(ex));
                    }
                }
                else
                {
                    buffer += " " + line;
                }

                if (Cancel)
                {
                    break;
                }
            }

            DbCurrent.CommitTransaction();
            tf.Close();

            return(string.Format("Nr de instruções executadas : {0} \r\n", CountInstrucoes));
        }
Example #7
0
        public string ExecQuery(string Sql, bool AutoExec)
        {
            //Connection cnnExec = new Connection();
            //cnnExec.Connect(DbCurrent.dbType, DbCurrent.Info);

            if (!AutoExec)
            {
                try
                {
                    if (!string.IsNullOrEmpty(Sql))
                    {
                        DbCurrent.Exec(Sql);
                        return("Executado com sucesso!\r\n");
                    }
                    else
                    {
                        return("Nr de instruções : 0 \r\n");
                    }
                }
                catch (Exception ex)
                { return(GetSqlError(ex)); }
            }
            else
            {
                SearchCommand sc = new SearchCommand(Sql, new char[] { ';' });

                string s_log = "";
                int    Count = 0;

                while (sc.HasCode)
                {
                    if (ScriptProgress != null)
                    {
                        bool Cancel = false;
                        if ((Count % 25) == 0)
                        {
                            ScriptProgress("SQL executado:" + Count, sc.Index, sc.Code.Length, out Cancel);
                        }

                        if (Cancel)
                        {
                            break;
                        }
                    }

                    string instrucao = sc.NextCommand().Trim();
                    if (!string.IsNullOrEmpty(instrucao))
                    {
                        Count++;
                        try
                        {
                            DbCurrent.Exec(instrucao);
                            s_log += "Executado com sucesso!\r\n";
                        }
                        catch (Exception ex)
                        {
                            s_log += GetSqlError(ex);
                            break;
                        }
                    }
                }

                return(string.Format("Nr de instruções : {0} \r\n", Count) + s_log);
            }
        }
Example #8
0
        public void UpdateColumns()
        {
            try
            {
                /*if (DbCurrent.GetSchema("DataTypes") == null)
                 * {
                 * DbColumns = new DbColumn[] { };
                 * return;
                 * }*/

                Conversion cnv = new Conversion();
                DataTable  dt  = DbCurrent.GetSchema("Columns");

                string ColumnDataType         = "";
                string ColumnDecimalDigits    = "";
                string ColumnSize             = "";
                bool   NumericPrecisionExists = false;

                if (ColumnExists(dt, "TYPE_NAME"))
                {
                    ColumnDataType = "TYPE_NAME";
                }
                else if (ColumnExists(dt, "DATA_TYPE"))
                {
                    ColumnDataType = "DATA_TYPE";
                }
                else if (ColumnExists(dt, "COLUMN_DATA_TYPE"))
                {
                    ColumnDataType = "COLUMN_DATA_TYPE";
                }

                if (ColumnExists(dt, "NUMERIC_SCALE"))
                {
                    ColumnDecimalDigits = "NUMERIC_SCALE";
                }
                else if (ColumnExists(dt, "DECIMAL_DIGITS"))
                {
                    ColumnDecimalDigits = "DECIMAL_DIGITS";
                }

                if (ColumnExists(dt, "CHARACTER_MAXIMUM_LENGTH"))
                {
                    ColumnSize = "CHARACTER_MAXIMUM_LENGTH";
                }
                else if (ColumnExists(dt, "COLUMN_SIZE"))
                {
                    ColumnSize = "COLUMN_SIZE";
                }

                NumericPrecisionExists = ColumnExists(dt, "NUMERIC_PRECISION");

                //DbColumn[] cols = new DbColumn[dt.Rows.Count];
                List <DbColumn> cols = new List <DbColumn>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    /*cols[i] = new DbColumn();
                     * cols[i].TABLE_NAME = dt.Rows[i]["TABLE_NAME"].ToString();
                     * cols[i].COLUMN_NAME = dt.Rows[i]["COLUMN_NAME"].ToString();
                     * cols[i].DATA_TYPE = dt.Rows[i][ColumnDataType].ToString();
                     * cols[i].DECIMAL_DIGITS = cnv.ToInt(dt.Rows[i][ColumnDecimalDigits]);
                     *
                     * if (NumericPrecisionExists)
                     * { cols[i].COLUMN_SIZE = cnv.ToInt(dt.Rows[i]["NUMERIC_PRECISION"]) == 0 ? cnv.ToInt(dt.Rows[i][ColumnSize]) : cnv.ToInt(dt.Rows[i]["NUMERIC_PRECISION"]); }
                     * else
                     * { cols[i].COLUMN_SIZE = cnv.ToInt(dt.Rows[i][ColumnSize]); }
                     */

                    DbColumn column = new DbColumn();
                    column.TABLE_NAME     = dt.Rows[i]["TABLE_NAME"].ToString();
                    column.COLUMN_NAME    = dt.Rows[i]["COLUMN_NAME"].ToString();
                    column.DATA_TYPE      = dt.Rows[i][ColumnDataType].ToString();
                    column.DECIMAL_DIGITS = cnv.ToInt(dt.Rows[i][ColumnDecimalDigits]);

                    if (NumericPrecisionExists)
                    {
                        column.COLUMN_SIZE = cnv.ToInt(dt.Rows[i]["NUMERIC_PRECISION"]) == 0 ? cnv.ToInt(dt.Rows[i][ColumnSize]) : cnv.ToInt(dt.Rows[i]["NUMERIC_PRECISION"]);
                    }
                    else
                    {
                        column.COLUMN_SIZE = cnv.ToInt(dt.Rows[i][ColumnSize]);
                    }

                    cols.Add(column);
                }

                DbColumns = cols.ToArray();
            }
            catch (Exception ex)
            { System.Windows.Forms.MessageBox.Show("Erro ao pesquisar as colunas.\n" + ex.Message); }
        }