Exemplo n.º 1
0
        public static SqlDataReader ExecuteReaderRetry(this SqlCommand command, ILastError errObj, Func <SqlCommand> retry)
        {
            if (errObj != null && errObj.LastError != null)
            {
                errObj.LastError = null;
            }

            SqlDataReader dataReader = null;
            SqlConnection conn       = command.Connection;

            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                dataReader = command.ExecuteReader() as SqlDataReader;
            }
            catch (Exception ex) { if (errObj != null)
                                   {
                                       errObj.LastError = ex;
                                   }
            }

            if (dataReader != null)
            {
                return(dataReader);
            }

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
                if (retry != null)
                {
                    command = retry();
                }
                try
                {
                    dataReader = command.ExecuteReader() as SqlDataReader;
                    if (errObj != null)
                    {
                        errObj.LastError = null;
                    }
                }
                catch (Exception ex) { if (errObj != null)
                                       {
                                           errObj.LastError = ex;
                                       }
                }
            }
            return(dataReader);
        }
Exemplo n.º 2
0
        public static XDocument ExecuteXmlReaderRetry(this SqlCommand cmd, ILastError errObj)
        {
            XDocument doc    = null;
            XmlReader reader = null;

            try
            {
                reader = cmd.ExecuteXmlReader();
            }
            catch (Exception ex) { errObj.LastError = ex; }
            if (reader == null)
            {
                if (errObj is ISqlContext)
                {
                    cmd.Connection = PreparedSqlConnection(errObj as ISqlContext);
                }
                else
                if (errObj is ISqlProc)
                {
                    cmd.Connection = PreparedSqlConnection(errObj as ISqlProc);
                }

                reader = cmd.ExecuteXmlReader();
            }

            if (reader != null)
            {
                errObj.LastError = null;
                XDocument docNext = null;

                // list nodes : reader.NameTable;
                while (reader.Read())
                {
                    if (doc == null)
                    {
                        doc = XDocument.Load(reader.ReadSubtree());
                    }
                    else
                    {
                        docNext = XDocument.Load(reader.ReadSubtree());
                        doc.Document.Root.Add(docNext.Root);
                        docNext = null;
                    }
                }
                if (reader is IDisposable)
                {
                    (reader as IDisposable).Dispose();
                }

                cmd.Dispose();
            }
            return(doc);
        }
Exemplo n.º 3
0
        public static Int32?UpdateSpidIfError(this SqlConnection SqlConnection, ILastError onError)
        {
            try
            {
                var cmd = new SqlCommand {
                    CommandText = "SELECT @@SPID", Connection = SqlConnection
                };
                var res = cmd.ExecuteScalar();
                if (res != null && (res is int || res is Int16))
                {
                    return(Convert.ToInt32(res));
                }
            }
            catch (Exception ex) { if (onError != null)
                                   {
                                       onError.LastError = ex;
                                   }
            }

            return(null);
        }
Exemplo n.º 4
0
 public static void CloseConn(this IDbConnection connection, bool withPool = true, ILastError onError = null)
 {
     if (connection == null)
     {
         return;
     }
     try
     {
         connection.Dispose();
         if (withPool && connection is SqlConnection)
         {
             SqlConnection.ClearPool(connection as SqlConnection);
         }
     }
     catch (Exception ex)
     {
         // DbObject PrePush internal exception
         if (onError != null)
         {
             onError.LastError = ex;
         }
     }
 }