コード例 #1
0
        public int ExecuteNonQuery(StatementDAO pStatement)
        {
            IDbCommand comm = Connection.CreateCommand();

            switch (pStatement.TypeCommand)
            {
            case TypeCommand.Text:
                comm.CommandType = CommandType.Text;
                break;

            case TypeCommand.TableDirect:
                comm.CommandType = CommandType.TableDirect;
                break;

            default:
                comm.CommandType = CommandType.StoredProcedure;
                break;
            }
            comm.CommandText = string.Format(pStatement.SQL);

            for (int n = 0; n < pStatement.NamesParameter.Count; n++)
            {
                IDbDataParameter param = comm.CreateParameter();
                param.ParameterName = "@" + pStatement.NamesParameter[n];
                param.DbType        = TypeToDbType(pStatement.TypesParameter[n]);
                if (pStatement.TypesParameter[n] == DateTime.Now.GetType()) // Se o tipo de dado for Datetime
                {
                    if (pStatement.ValuesParameter[n] == null)              //Se a data for null informa null
                    {
                        param.Value = DBNull.Value;
                    }
                    else if ((DateTime)pStatement.ValuesParameter[n] == DateTime.MinValue) //se a data for MinValue
                    {
                        param.Value = DBNull.Value;
                    }
                    else
                    {
                        param.Value = pStatement.ValuesParameter[n];
                    }
                }
                else //se for outro tipo de dado diferente de datetime
                {
                    param.Value = pStatement.ValuesParameter[n] == null ? DBNull.Value : pStatement.ValuesParameter[n];
                }
                comm.Parameters.Add(param);
            }
            try
            {
                comm.Transaction = GetTransactionControler();
                comm.CommandText = string.Format(pStatement.SQL);
                checkIfConnectionIsOpen();
                return(comm.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                comm.Dispose();
                CloseConnection();
            }
        }
コード例 #2
0
        public IList <T> ExecuteReturnListT(StatementDAO pStatement, out int pNumRegTotal)
        {
            pNumRegTotal = 0;
            IDbCommand comm = Connection.CreateCommand();

            switch (pStatement.TypeCommand)
            {
            case TypeCommand.Text:
                comm.CommandType = CommandType.Text;
                break;

            case TypeCommand.TableDirect:
                comm.CommandType = CommandType.TableDirect;
                break;

            default:
                comm.CommandType = CommandType.StoredProcedure;
                break;
            }
            comm.CommandText = string.Format(pStatement.SQL);

            for (int n = 0; n < pStatement.NamesParameter.Count; n++)
            {
                IDbDataParameter param = comm.CreateParameter();
                param.ParameterName = "@" + pStatement.NamesParameter[n];
                param.DbType        = TypeToDbType(pStatement.TypesParameter[n]);
                if (pStatement.TypesParameter[n] == DateTime.Now.GetType()) // Se o tipo de dado for Datetime
                {
                    if (pStatement.ValuesParameter[n] == null)              //Se a data for null informa null
                    {
                        param.Value = DBNull.Value;
                    }
                    else if ((DateTime)pStatement.ValuesParameter[n] == DateTime.MinValue) //se a data for MinValue
                    {
                        param.Value = DBNull.Value;
                    }
                    else
                    {
                        param.Value = pStatement.ValuesParameter[n];
                    }
                }
                else //se for outro tipo de dado diferente de datetime
                {
                    param.Value = pStatement.ValuesParameter[n] == null ? DBNull.Value : pStatement.ValuesParameter[n];
                }
                comm.Parameters.Add(param);
            }

            comm.Transaction = GetTransactionControler();

            //comm.CommandText = string.Format(pStatement.SQL);
            checkIfConnectionIsOpen();
            IDataReader dr = comm.ExecuteReader();

            List <T> lst = new List <T>();

            try
            {
                while (dr.Read())
                {
                    T obj = ToObject(dr, out pNumRegTotal);
                    lst.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //return new List<T>();
            }
            finally
            {
                comm.Dispose();
                dr.Close();
                dr.Dispose();
                CloseConnection();
            }
            return(lst);
        }