예제 #1
0
        public void Dispose()
        {
            if (dataReader != null)
            {
                dataReader.Dispose();
            }
            dataReader = null;

            if (cmd != null)
            {
                cmd.Dispose();
            }
            cmd = null;

            if (conn != null && conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
            if (conn != null)
            {
                conn.Dispose();
            }
            conn  = null;
            state = StateExec.Init;
        }
예제 #2
0
        public IEnumerable <object[]> Query()
        {
            StateExecuting();

            if (dataReader == null)
            {
                Dispose();
                state = StateExec.Init;

                yield break;
            }
            else
            {
                if (LastRow != null)
                {
                    yield return(LastRow);
                }

                var helper = this;
                while (dataReader.Read())
                {
                    object[] objVal = helper.DbRecordArray();
                    dataReader.GetValues(objVal);
                    LastRow = helper.SetValues(objVal);
                    yield return(LastRow);
                }

                Dispose();
                state = StateExec.Init;
            }
        }
예제 #3
0
        public IEnumerable <object[]> Query()
        {
            StateExecuting();
            // using (SqlDataReader dataReader = cmd.ExecuteReader())
            if (dataReader == null || !dataReader.Read())
            {
                Dispose();
                state = StateExec.Init;

                yield break;
            }
            else
            {
                var helper = this;
                do
                {
                    object[] objVal = helper.DbRecordArray();
                    dataReader.GetValues(objVal);

                    yield return(helper.SetValues(objVal));
                }while (dataReader.Read());

                Dispose();
                state = StateExec.Init;
            }
        }
예제 #4
0
        public bool Prepare(SqlProc proc, Action <SqlTableMapper, DbDataReader> parser = null)
        {
            if (state != StateExec.Init)
            {
                this.Dispose();
            }

            Context db = proc.Context;

            conn = new SqlConnection(db.ConnectionString());
            conn.Open();
            if (conn.State != ConnectionState.Open)
            {
                return(false);
            }

            conn.ChangeDatabase(db.DbName);

            cmd            = proc.CreateCommand();
            cmd.Connection = conn;

            dataReader = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);

            if (parser != null)
            {
                base.propertiesParser = parser;
            }
            (this as IDataMapHelper <object[]>).GetProperties(dataReader);


            // CommandBehavior.
            //  SingleResult = 1,   The query returns a single result set.
            //    SchemaOnly = 2,
            //     The query returns column information only. When using System.Data.CommandBehavior.SchemaOnly,
            //     the .NET Framework Data Provider for SQL Server precedes the statement being
            //     executed with SET FMTONLY ON.
            //  KeyInfo = 4,
            //     The query returns column and primary key information.
            //  SingleRow = 8,
            //     The query is expected to return a single row of the first result set. Execution
            //     of the query may affect the database state. Some .NET Framework data providers
            //     may, but are not required to, use this information to optimize the performance
            //     of the command. When you specify System.Data.CommandBehavior.SingleRow with
            //     the System.Data.OleDb.OleDbCommand.ExecuteReader() method of the System.Data.OleDb.OleDbCommand
            //     object, the .NET Framework Data Provider for OLE DB performs binding using
            //     the OLE DB IRow interface if it is available. Otherwise, it uses the IRowset
            //     interface. If your SQL statement is expected to return only a single row,
            //     specifying System.Data.CommandBehavior.SingleRow can also improve application
            //     performance. It is possible to specify SingleRow when executing queries that
            //     are expected to return multiple result sets. In that case, where both a multi-result
            //     set SQL query and single row are specified, the result returned will contain
            //     only the first row of the first result set. The other result sets of the
            //     query will not be returned.

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

            state = StateExec.Prepared;
            return(true);
        }
예제 #5
0
 public SqlObjTableReader(Action <SqlTableMapper, DbDataReader> propertiesParser)
     : base(propertiesParser)
 {
     conn       = null;
     cmd        = null;
     dataReader = null;
     state      = StateExec.Init;
 }
 public void AddStateCallback(int state, StateExec callback)
 {
     for (int i = 0; i < stateCallbacks.Length; i++)
     {
         if (stateCallbacks[i].stateNum == state)
         {
             stateCallbacks[i].callback = callback;
             break;
         }
     }
 }
예제 #7
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);
        }
예제 #8
0
 public void StateExecuting()
 {
     state = StateExec.Executing;
 }