Exemple #1
0
        public Exception HandleException(Exception ex, SqlPreCommandSimple command)
        {
            var nex = ReplaceException(ex, command);

            nex.Data["Sql"] = command.sp_executesql();
            return(nex);
        }
Exemple #2
0
        protected internal override DataSet ExecuteDataSet(SqlPreCommandSimple preCommand, CommandType commandType)
        {
            using (SqlConnection? con = EnsureConnection())
                using (SqlCommand cmd = NewCommand(preCommand, con, commandType))
                    using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql()))
                    {
                        try
                        {
                            SqlDataAdapter da     = new SqlDataAdapter(cmd);
                            DataSet        result = new DataSet();
                            da.Fill(result);
                            return(result);
                        }
                        catch (Exception ex)
                        {
                            var nex = HandleException(ex, preCommand);
                            if (nex == ex)
                            {
                                throw;
                            }

                            throw nex;
                        }
                    }
        }
Exemple #3
0
        protected internal override object?ExecuteScalar(SqlPreCommandSimple preCommand, CommandType commandType)
        {
            using (SqlConnection? con = EnsureConnection())
                using (SqlCommand cmd = NewCommand(preCommand, con, commandType))
                    using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql()))
                    {
                        try
                        {
                            object result = cmd.ExecuteScalar();

                            if (result == null || result == DBNull.Value)
                            {
                                return(null);
                            }

                            return(result);
                        }
                        catch (Exception ex)
                        {
                            var nex = HandleException(ex, preCommand);
                            if (nex == ex)
                            {
                                throw;
                            }

                            throw nex;
                        }
                    }
        }
Exemple #4
0
        protected internal override int ExecuteNonQuery(SqlPreCommandSimple preCommand, CommandType commandType)
        {
            return(EnsureConnectionRetry(con =>
            {
                using (NpgsqlCommand cmd = NewCommand(preCommand, con, commandType))
                    using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql()))
                    {
                        try
                        {
                            int result = cmd.ExecuteNonQuery();
                            return result;
                        }
                        catch (Exception ex)
                        {
                            var nex = HandleException(ex, preCommand);
                            if (nex == ex)
                            {
                                throw;
                            }

                            throw nex;
                        }
                    }
            }));
        }
Exemple #5
0
        public void ExecuteDataReaderDependency(SqlPreCommandSimple preCommand, OnChangeEventHandler change, Action reconect, Action <FieldReader> forEach, CommandType commandType)
        {
            bool reconected = false;

retry:
            try
            {
                using (SqlConnection? con = EnsureConnection())
                    using (SqlCommand cmd = NewCommand(preCommand, con, commandType))
                        using (HeavyProfiler.Log("SQL-Dependency"))
                            using (HeavyProfiler.Log("SQL", () => preCommand.sp_executesql()))
                            {
                                try
                                {
                                    if (change != null)
                                    {
                                        SqlDependency dep = new SqlDependency(cmd);
                                        dep.OnChange += change;
                                    }

                                    using (SqlDataReader reader = cmd.ExecuteReader())
                                    {
                                        FieldReader fr  = new FieldReader(reader);
                                        int         row = -1;
                                        try
                                        {
                                            while (reader.Read())
                                            {
                                                row++;
                                                forEach(fr);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            FieldReaderException fieldEx = fr.CreateFieldReaderException(ex);
                                            fieldEx.Command = preCommand;
                                            fieldEx.Row     = row;
                                            throw fieldEx;
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    var nex = HandleException(ex, preCommand);
                                    if (nex == ex)
                                    {
                                        throw;
                                    }

                                    throw nex;
                                }
                            }
            }
            catch (InvalidOperationException ioe)
            {
                if (ioe.Message.Contains("SqlDependency.Start()") && !reconected)
                {
                    reconect();

                    reconected = true;

                    goto retry;
                }

                throw;
            }
        }