Exemple #1
0
        public bool ExecNoQuery(string query, CommitRoll commitRollback = CommitRoll.commitRollback, NuovaConn nuovaConn = NuovaConn.seNecessario, StrConnection strConn = null, Int32 timeOutQuery = 0, Mess logMess = null)
        {
            if (logMess == null)
            {
                logMess = new Mess(LogType.ERR, Log.main.errUserText);
            }
            logMess.testoDaLoggare = "";

            Thread thread; Exception thrEx = null; bool esito; string testoTmp;

            if (PreExec(true, query, ref strConn, nuovaConn, ref timeOutQuery, logMess) == false)
            {
                return(false);
            }

            esito  = false;
            thread = Thr.AvviaNuovo(() => esito = ThrENQ_Exec(out thrEx));

            if (Thr.AttesaCompletamento(ref thread, timeOutQuery) == false)
            { //qua non faccio rollback poichè presumo che la connessione remota sia andata persa, se DB in locale non può andare in timeout
                logMess.testoDaLoggare = "query:<" + cmd.CommandText + "> andata in timeOut:<" + timeOutQuery + ">";
                Log.main.Add(logMess);
                return(false);
            }

            if (esito == false)
            {
                try
                {
                    if (commitRollback == CommitRoll.commitRollback || commitRollback == CommitRoll.soloRollback)
                    {
                        tra.Rollback();
                        this.DisconnettiDB();
                    }
                }
                catch (Exception ex)
                {
                    logMess.testoDaVisual  = ""; //Non visualizzo nulla poichè c'è il l'altro log sotto
                    logMess.testoDaLoggare = "eccezione durante rollback, ex.Mess:<" + ex.Message + ">, query:<" + query + ">";
                    Log.main.Add(logMess);
                }

                if (thrEx != null)
                {
                    if (FiltraEccezioniQuery(thrEx) == true)
                    {
                        logMess.testoDaVisual = "";
                        logMess.tipo          = LogType.Warn;
                    }
                    testoTmp = "ex.mess:<" + thrEx.Message + ">";
                }
                else
                {
                    testoTmp = "il thread ThrENQ_Exec è ritornato false ma senza eccezioni";
                }


                logMess.testoDaLoggare = "query:<" + cmd.CommandText + ">, " + testoTmp;
                Log.main.Add(logMess);
                return(false);
            }

            try
            {
                if (commitRollback == CommitRoll.commitRollback)
                {
                    tra.Commit();
                    this.DisconnettiDB();
                }
            }
            catch (Exception ex)
            {
                if (FiltraEccezioniCommit(ex) == true)
                {
                    logMess.testoDaVisual = "";
                    logMess.tipo          = LogType.Warn;
                }

                logMess.testoDaLoggare = "errore durante 'sqlTra.Commit()', ex.Mess:<" + ex.Message + ">, query:<" + query + ">";
                Log.main.Add(logMess);
                return(false);
            }

            return(true);
        }
Exemple #2
0
 public bool Delete(string where, ref SqlObj sql, bool wherePresente = true, NuovaConn nuovaConn = NuovaConn.seNecessario, StrConnection strConn = null, CommitRoll commitRollback = CommitRoll.commitRollback,
                    Int32 timeOutQuery = 0, Mess logMess = null)
 {
     return(Delete(where, wherePresente, ref sql, nuovaConn, strConn, commitRollback, timeOutQuery, logMess));
 }
Exemple #3
0
        public bool Delete(string where, bool wherePresente, ref SqlObj sql, NuovaConn nuovaConn, StrConnection strConn, CommitRoll commitRollback, Int32 timeOutQuery, Mess logMess)
        {
            if (logMess == null)
            {
                logMess = new Mess(LogType.ERR, Log.main.errUserText);
            }
            if (where == null)
            {
                where = "";
            }

            if (wherePresente == true && where == "")   //Serve poichè inibisce la possibilità di eseguire la query senza la parte where
            {
                Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "ricevuto 'wherePresente' a true, ma ricevuto 'where' a nothing o vuoto"));
                return(false);
            }

            if (sql == null)
            {
                sql = new SqlObj();
            }

            string query;

            if (where != "")
            {
                where = " WHERE " + where;
            }

            if (ValidaBusinessLogic(Sql.del, where, logMess: logMess) == false)
            {
                return(false);
            }

            query = Sql.del + Nm + where;

            if (sql.ExecNoQuery(query, commitRollback: commitRollback, nuovaConn: nuovaConn, strConn: strConn, timeOutQuery: timeOutQuery, logMess: logMess) == false)
            {
                return(false);
            }

            if (commitRollback == CommitRoll.commitRollback)
            {
                EventoEseguitaQuery(Sql.del);
            }

            return(true);
        }
Exemple #4
0
        public bool Update(string where, bool wherePresente, ref SqlObj sql, NuovaConn nuovaConn, StrConnection strConn, CommitRoll commitRollback, Int32 timeOutQuery, Mess logMess)
        {
            if (logMess == null)
            {
                logMess = new Mess(LogType.ERR, Log.main.errUserText);
            }
            if (where == null)
            {
                where = "";
            }

            if (wherePresente == true && where == "")   //Serve poichè inibisce la possibilità di eseguire la query senza la parte where
            {
                logMess.testoDaLoggare = "ricevuto 'wherePresente' a true, ma ricevuto 'where' a nothing o vuoto";
                Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, logMess.testoDaLoggare));
                return(false);
            }

            if (sql == null)
            {
                sql = new SqlObj();
            }

            string query, qrySet, val; Column col;

            qrySet = "";

            foreach (FieldInfo proprietàCol in this.GetType().GetFields())    //Scorro tutti i field della classe che eredita da TabellaBase alla ricerca delle colonne
            {
                if (proprietàCol.FieldType != typeof(Column))
                {
                    continue;
                }
                col = (Column)proprietàCol.GetValue(this);

                if (col.Ident == true)
                {
                    continue;                     //Se è identity non va nella Update
                }
                val = col.ValXqry;

                if ((val == "NULL" || val == "DEFAULT") && col.ValNull == false)
                {
                    continue;
                }

                qrySet += col.Nm + "=" + val + ", ";
            }

            if (ValidaBusinessLogic(Sql.upd, logMess: logMess) == false)
            {
                return(false);
            }

            qrySet = qrySet.RemoveFinal(", ");

            if (where != "")
            {
                where = " WHERE " + where;
            }

            query = Sql.upd + Nm + " SET " + qrySet + where;

            if (sql.ExecNoQuery(query, commitRollback: commitRollback, nuovaConn: nuovaConn, strConn: strConn, timeOutQuery: timeOutQuery, logMess: logMess) == false)
            {
                return(false);
            }

            if (commitRollback == CommitRoll.commitRollback)
            {
                EventoEseguitaQuery(Sql.upd);
            }

            return(true);
        }
Exemple #5
0
        public bool UpdateWithPKValue(ref SqlObj sql, NuovaConn nuovaConn = NuovaConn.seNecessario, StrConnection strConn = null, CommitRoll commitRollback = CommitRoll.commitRollback,
                                      Int32 timeOutQuery = 0, Mess logMess = null)
        {
            string where, val; Column col;
            where = "";

            foreach (FieldInfo proprietàCol in this.GetType().GetFields())
            {  //Scorro tutti i field della classe che eredita da TabellaBase alla ricerca delle colonne
                if (proprietàCol.FieldType != typeof(Column))
                {
                    continue;
                }
                col = (Column)proprietàCol.GetValue(this);

                if (col.PrimKey == false)
                {
                    continue;                        //Se non è PrimKey la scarto
                }
                val = col.ValXqry;

                if (val == "NULL" || val == "DEFAULT")
                {
                    Log.main.Add(new Mess(LogType.Warn, Log.main.warnUserText, "Non è stata valorizzata la colonna chiave:<" + col.Nm + ">"));
                    return(false);
                }

                where += col.Nm + "=" + val + " AND ";
            }

            if (where == "")
            {
                Log.main.Add(new Mess(LogType.Warn, Log.main.warnUserText, "Nella tabella non sono presenti campi chiave"));
                return(false);
            }

            where = where.RemoveFinal("AND ");

            return(Update(where, true, ref sql, nuovaConn, strConn, commitRollback, timeOutQuery, logMess));
        }
Exemple #6
0
        //public bool Insert(Optional selecDeiValori As String = "", Optional ByRef sql As Ogg = Nothing, Optional nuovaConn As NuovaConn = NuovaConn.seNecessario, Optional strConn As StrConnessione = Nothing,
        //                       Optional commitRollback As CommitRoll = CommitRoll.commitRollback, Optional timeOutQuery As Int32 = 0, Optional logMess As Mess = Nothing) As Boolean

        //    If IsNothing(logMess) Then logMess = New Mess(Tipi.err, log.errUserText)
        //    If IsNothing(sql) Then sql = New Ogg

        //    Dim query As String

        //    If IsNothing(selecDeiValori) = True OrElse selecDeiValori = "" Then

        //        Dim col As Colonna, qryTestata, qryValori As String, valori As New List(Of String), primaCol As Boolean, valXqry As List(Of String)
        //        primaCol = True

        //        For Each proprietàCol As Reflection.FieldInfo In Me.GetType.GetFields  'Scorro tutti i field della classe che eredita da TabellaBase alla ricerca delle colonne
        //            If proprietàCol.FieldType<> GetType(Colonna) Then Continue For
        //            col = proprietàCol.GetValue(Me)
        //            If col.ident = True Then Continue For  'Se è identity non va nella insert

        //            qryTestata += col.nm & ", "

        //            If primaCol = True Then
        //                For Each tmpVal In col.valXqryList
        //                    valori.Add(tmpVal)
        //                Next

        //                primaCol = False

        //            Else '2° col in poi

        //                valXqry = col.valXqryList 'valXqryList al suo interno cicla i valori per renderli compatibili con sql, quindi la richiamo solamente una volta per le prestazioni
        //                If valori.Count<> valXqry.Count Then
        //                    log.Acc(New Mess(Tipi.err, log.errUserText, "per la tabella:<" & nmTab & ">, la colonna:<" & col.nm & "> ha un numero valori di:<" & valXqry.Count & "> mentre la prima colonna ne prevedeva:<" & valori.Count & "> la insert sarà annullata"))
        //                    Return False
        //                End If

        //                For i = 0 To valori.Count - 1
        //                    valori(i) += ", " & valXqry(i)
        //                Next

        //            End If

        //        Next

        //        If ValidaBusinessLogic(qryIns, logMess:=logMess) = False Then Return False

        //        qryTestata = RimuoviFinale(qryTestata, ", ")

        //        For Each gruppoValore In valori
        //            qryValori += "(" & gruppoValore & "), "
        //        Next

        //        qryValori = RimuoviFinale(qryValori, ", ")

        //        query = qryIns & nmTab & " (" & qryTestata & ") VALUES " & qryValori

        //    Else
        //        If ValidaBusinessLogic(qryUpd, selecDeiValori, logMess:=logMess) = False Then Return False

        //        query = qryIns & nmTab & " " & selecDeiValori

        //    End If


        //    If sql.ExecNoQuery(query, commitRollback:=commitRollback, nuovaConn:=nuovaConn, strConn:=strConn, timeOutQuery:=timeOutQuery, logMess:=logMess) = False Then Return False

        //    If commitRollback = CommitRoll.commitRollback Then EventoEseguitaQuery(qryIns)

        //    Return True
        //End Function

        //Public Function Update(where As String, Optional wherePresente As Boolean = True, Optional ByRef sql As Ogg = Nothing, Optional nuovaConn As NuovaConn = NuovaConn.seNecessario, Optional strConn As StrConnessione = Nothing,
        //                       Optional commitRollback As CommitRoll = CommitRoll.commitRollback, Optional visualLog As Boolean = True, Optional timeOutQuery As Int32 = 0, Optional ByRef descErr As String = "") As Boolean

        public bool UpdateWithPKValue(NuovaConn nuovaConn = NuovaConn.seNecessario, StrConnection strConn = null, CommitRoll commitRollback = CommitRoll.commitRollback,
                                      Int32 timeOutQuery  = 0, Mess logMess = null)
        {
            SqlObj sql = null;

            return(UpdateWithPKValue(ref sql, nuovaConn, strConn, commitRollback, timeOutQuery, logMess));
        }
Exemple #7
0
        public bool Insert(string selecDeiValori, ref SqlObj sql, NuovaConn nuovaConn, StrConnection strConn, CommitRoll commitRollback, Int32 timeOutQuery, Mess logMess)
        {
            if (logMess == null)
            {
                logMess = new Mess(LogType.ERR, Log.main.errUserText);
            }
            if (sql == null)
            {
                sql = new SqlObj();
            }

            string query;

            if (selecDeiValori == null || selecDeiValori == "")
            {
                Column col; string qryTestata, qryValori; List <string> valori = new List <String>(); bool primaCol; List <string> valXqry;
                primaCol   = true;
                qryTestata = qryValori = "";

                foreach (FieldInfo proprietàCol in this.GetType().GetFields())
                {  //Scorro tutti i field della classe che eredita da TabellaBase alla ricerca delle colonne
                    if (proprietàCol.FieldType != typeof(Column))
                    {
                        continue;
                    }

                    col = (Column)proprietàCol.GetValue(this);
                    if (col.Ident == true)
                    {
                        continue;                     //Se è identity non va nella insert
                    }
                    qryTestata += col.Nm + ", ";

                    if (primaCol == true)
                    {
                        foreach (string tmpVal in col.ValXqryList)
                        {
                            valori.Add(tmpVal);
                        }

                        primaCol = false;
                    }
                    else
                    {                              //2° col in poi
                        valXqry = col.ValXqryList; //valXqryList al suo interno cicla i valori per renderli compatibili con sql, quindi la richiamo solamente una volta per le prestazioni
                        if (valori.Count != valXqry.Count)
                        {
                            Log.main.Add(new Mess(LogType.ERR, Log.main.errUserText, "per la tabella:<" + Nm + ">, la colonna:<" + col.Nm + "> ha un numero valori di:<" + valXqry.Count + "> mentre la prima colonna ne prevedeva:<" + valori.Count + "> la insert sarà annullata"));
                            return(false);
                        }
                        for (int i = 0; i < valori.Count; i++)
                        {
                            valori[i] += ", " + valXqry[i];
                        }
                    }
                }

                if (ValidaBusinessLogic(Sql.ins, logMess: logMess) == false)
                {
                    return(false);
                }

                qryTestata = qryTestata.RemoveFinal(", ");

                foreach (string gruppoValore in valori)
                {
                    qryValori += "(" + gruppoValore + "), ";
                }

                qryValori = qryValori.RemoveFinal(", ");

                query = Sql.ins + Nm + " (" + qryTestata + ") VALUES " + qryValori;
            }
            else
            {
                if (ValidaBusinessLogic(Sql.upd, selecDeiValori, logMess: logMess) == false)
                {
                    return(false);
                }
                query = Sql.ins + Nm + " " + selecDeiValori;
            }

            if (sql.ExecNoQuery(query, commitRollback: commitRollback, nuovaConn: nuovaConn, strConn: strConn, timeOutQuery: timeOutQuery, logMess: logMess) == false)
            {
                return(false);
            }

            if (commitRollback == CommitRoll.commitRollback)
            {
                EventoEseguitaQuery(Sql.ins);
            }

            return(true);
        }
Exemple #8
0
 public bool Insert(ref SqlObj sql, string selecDeiValori = "", NuovaConn nuovaConn = NuovaConn.seNecessario, StrConnection strConn = null, CommitRoll commitRollback = CommitRoll.commitRollback, Int32 timeOutQuery = 0,
                    Mess logMess = null)
 {
     return(Insert(selecDeiValori, ref sql, nuovaConn, strConn, commitRollback, timeOutQuery, logMess));
 }