Esempio n. 1
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));
        }
Esempio n. 2
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);
            }
        }