Exemple #1
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);
        }
Exemple #2
0
        public static ISqlProc PrepareWithReconnect(this ISqlProc proc, Context db)
        {
            var conn = proc.Connection;

            if (conn == null)
            {
                proc.Connection = proc.OpenConnection();
            }
            conn = proc.Connection;

            try
            {
                var dbName = conn.Database;
                var cmd    = new SqlCommand {
                    CommandText = "SELECT DB_NAME()"
                };                                                              // neutral command
                var result = cmd.ExecuteScalar() as string;
            }
            catch (Exception ex) { proc.LastError = ex; }
            return(proc);
        }
Exemple #3
0
        public static SqlConnection PreparedSqlConnection(this ISqlProc proc)
        {
            if (proc == null)
            {
                return(null);
            }
            if (proc.Connection.IsReady())
            {
                return(proc.Connection as SqlConnection);
            }

            proc.Connection     = proc.OpenConnection();
            proc.CloseOnDispose = true;

            if (proc is ISqlProcContext && string.IsNullOrWhiteSpace(proc.Connection.ConnectionString))
            {
                var procDb = proc as ISqlProcContext;
                proc.Connection = procDb.Context.SqlConnection;
            }

            return(proc.Connection as SqlConnection);
        }
Exemple #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);
 }