Beispiel #1
0
        // in T
        public static bool ExecFill <T>(this ISqlProc proc, IList <T> list,
                                        IDataMapHelper <T> mapper,
                                        Action <double> progress = null) where T : class // , TMap : IDataMapHelper
        {
            Ai.Guard.Check(proc.Connection != null, "proc.Connection null error in ExecFill");
            Ai.Guard.Check(list != null, "list null error in ExecFill");
            if (proc.Connection.State != ConnectionState.Open)
            {
                proc.Connection.Open();
            }

            using (var command = proc.CreateCommand())
            {
                if (progress != null)
                {
                    progress(0.0);
                }

                using (SqlDataReader dataReader = command.ExecuteReader())
                {
                    var results = list as IList <T>;
                    if (!dataReader.Read())
                    {
                        if (progress != null)
                        {
                            progress(1.0);
                        }
                        return(false);
                    }

                    // Task<T> GetFieldValueAsync<T>(int i, CancellationToken cancellationToken);
                    var helper = mapper; //  new DbDataMapHelper<T>();
                    helper.GetProperties(dataReader);

                    do
                    {
                        object[] objVal = helper.DbRecordArray();
                        // int ret =
                        dataReader.GetValues(objVal);
                        T val = helper.SetValues(objVal);
                        results.Add(val);
                    }while (dataReader.Read());

                    if (progress != null)
                    {
                        progress(1.0);
                    }

                    return(true);
                }
            }
        }
Beispiel #2
0
        public bool Prepare(ISqlProc proc, Action <SqlTableMapper, DbDataReader> parser = null, int?commandTimeout = null)
        {
            if (state != StateExec.Init)
            {
                this.Dispose();
            }
            if (parser != null)
            {
                base.propertiesParser = parser;
            }

            LastRow = null;
            var conn = proc.OpenConnection();

            if (conn == null)
            {
                return(false);
            }
            cmd            = proc.CreateCommand();
            cmd.Connection = conn;
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }

            if (commandTimeout.HasValue)
            {
                cmd.CommandTimeout = commandTimeout.Value;
            }

            cmd.Prepare();
            dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult);

            state = StateExec.Prepared;

            if (dataReader.Read())
            {
                // First record
                (this as IDataMapHelper <object[]>).GetProperties(dataReader);

                var      helper = this;
                object[] objVal = helper.DbRecordArray();
                // first record array
                dataReader.GetValues(objVal);
                LastRow = helper.SetValues(objVal);
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public static SqlDataReader ExecuteWithReconnect(this ISqlProc proc)
        {
            var lastErr = proc.LastError;
            var cmd     = proc.CreateCommand() as SqlCommand;

            var reader = ExecuteReaderRetry(cmd, proc,
                                            retry: () => GetSqlCommandReconnect(proc));

            lastErr = proc.LastError;
            if (lastErr is SqlException)
            {
                throw lastErr;
            }
            else if (reader == null)
            {
                proc.LastError = new Exception(String.Format("procedure error {0}", proc));
                throw proc.LastError;
            }

            return(reader);
        }
Beispiel #4
0
 public static SqlCommand GetSqlCommandReconnect(this ISqlProc proc)
 {
     proc.Connection     = null;
     proc.CloseOnDispose = true;
     if (proc is SqlProc)
     {
         var sqlProc = proc as SqlProc;
         if (sqlProc.Context != null)
         {
             sqlProc.Context.AssureOpen(withCommand: true);
             sqlProc.Connection = sqlProc.Context.SqlConnection;
         }
         else
         {
             sqlProc.OpenConnection();
         }
     }
     else
     {
         proc.Connection = proc.OpenConnection();
     }
     return(proc.CreateCommand() as SqlCommand);
 }
Beispiel #5
0
        public static IEnumerable <object[]> ExecEnumerable(this ISqlProc proc,
                                                            IDataMapHelper <object[]> mapper,
                                                            Action <double> progress = null, Action <SqlField[]> onReadFields = null)
        {
            Ai.Guard.Check(proc.Connection != null, "proc.Connection null error in ExecFill");

            using (SqlConnection connection = new SqlConnection(proc.ConnectionString()))
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    if (connection.State != ConnectionState.Open)
                    {
                        yield break;
                    }
                }
                if (connection.Database != proc.DbName)
                {
                    connection.ChangeDatabase(proc.DbName);
                }

                using (var command = proc.CreateCommand())
                {
                    command.Connection = connection;

                    if (progress != null)
                    {
                        progress(0.0);
                    }

                    using (SqlDataReader dataReader = command.ExecuteReader())
                    {
                        if (!dataReader.Read())
                        {
                            if (progress != null)
                            {
                                progress(1.0);
                            }

                            yield break;
                        }

                        // Task<T> GetFieldValueAsync<T>(int i, CancellationToken cancellationToken);
                        var helper = mapper; //  new DbDataMapHelper<T>();
                        helper.GetProperties(dataReader);

                        if (onReadFields != null)
                        {
                            onReadFields(helper.GetFields(dataReader));
                        }

                        do
                        {
                            object[] objVal = helper.DbRecordArray();
                            dataReader.GetValues(objVal);

                            object[] val = helper.SetValues(objVal);
                            yield return(val);
                        }while (dataReader.Read());

                        if (progress != null)
                        {
                            progress(1.0);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        // Unsafe
        public static SqlDataReader ExecMultiReader(this ISqlProc proc,
                                                    Action <SqlCommand> setup  = null,
                                                    Action <Exception> onError = null,
                                                    Action <double> progress   = null)
        {
            var command = proc.CreateCommand();
            //if (command.Connection == null || command.Connection.State != ConnectionState.Open)

            var connStr = proc.ConnectionString();

            Guard.Check(!string.IsNullOrWhiteSpace(connStr), "Exec MultiReader connection error");

            SqlConnection connection = new SqlConnection(connStr);

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
                if (connection.State != ConnectionState.Open)
                {
                    return(null);
                }
            }
            if (connection.Database != proc.DbName)
            {
                connection.ChangeDatabase(proc.DbName);
            }
            command.Connection = connection;

            if (setup != null)
            {
                setup(command);
            }

            if (progress != null)
            {
                progress(0.0);
            }

            SqlDataReader dataReader = null;

            if (onError != null)
            {
                try
                {
                    dataReader = command.ExecuteReader(behavior: CommandBehavior.CloseConnection);
                }
                catch (Exception ex) { onError(ex); }
            }
            else
            {
                dataReader = command.ExecuteReader(behavior: CommandBehavior.CloseConnection);
            }

            if (dataReader != null && dataReader.IsClosed) // !dataReader.Read())
            {
                if (progress != null)
                {
                    progress(1.0);
                }

                return(null);
            }

            return(dataReader);
        }