Exemplo n.º 1
0
        public DataSet ToDataSet(IDbCode code, CommandType type = CommandType.Text)
        {
            string name = DateTime.Now.ToString();

            command.Add(name, new SqlCommand());
            SqlCommand com = command[name];

            com.Connection  = conn;
            com.CommandText = code.ToString();
            com.CommandType = type;
            setCommand(com, (List <SqlParameter>)code.Paras);

            DataSet dt = new DataSet();

            try
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(com))
                {
                    adapter.Fill(dt);
                }
            }
            catch (Exception ex)
            {
                DoException();
                throw ex;
            }
            finally
            {
                Dispose(name);
            }

            return(dt);
        }
Exemplo n.º 2
0
        public object ToResult(IDbCode code, CommandType type = CommandType.Text)
        {
            string name = DateTime.Now.ToString();

            command.Add(name, new SqlCommand());
            SqlCommand com = command[name];

            com.Connection  = conn;
            com.CommandText = code.ToString();
            com.CommandType = type;
            setCommand(com, (List <SqlParameter>)code.Paras);


            object result = null;

            try
            {
                this.OpenConnection();
                result = com.ExecuteScalar();
            }
            catch (Exception ex)
            {
                DoException();
                throw ex;
            }
            finally
            {
                this.Dispose(name);
                this.CloseConnection();
            }

            return(result);
        }
Exemplo n.º 3
0
        public List <T> ToList <T>(IDbCode code, CommandType type = CommandType.Text)
            where T : class, new()
        {
            List <T> list = new List <T>();
            string   name = DateTime.Now.ToString();

            command.Add(name, new SqlCommand());
            SqlCommand com = command[name];

            com.Connection  = conn;
            com.CommandText = code.ToString();
            com.CommandType = type;
            setCommand(com, (List <SqlParameter>)code.Paras);


            Type t = typeof(T);
            List <PropertyInfo> pros;

            if (pro.ContainsKey(t.Name))
            {
                pros = pro[t.Name];
            }
            else
            {
                pros = t.GetProperties().ToList();
                pro.Add(t.Name, pros);
            }

            try
            {
                this.OpenConnection();
                using (SqlDataReader reader = com.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        T model = new T();
                        pros.ForEach(o =>
                        {
                            if (ReaderExists(reader, o.Name))
                            {
                                o.SetValue(model, reader[o.Name], null);
                            }
                        });
                        list.Add(model);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.Dispose(name);
                this.CloseConnection();
            }

            return(list);
        }
Exemplo n.º 4
0
        public int ExcuteResult(IDbCode code, CommandType type = CommandType.Text)
        {
            string name = DateTime.Now.ToString();

            command.Add(name, new SqlCommand());
            SqlCommand com = command[name];

            com.Connection  = conn;
            com.CommandText = code.ToString();
            com.CommandType = type;
            setCommand(com, (List <SqlParameter>)code.Paras);


            int result = 0;

            try
            {
                this.OpenConnection();
                if (tran != null)
                {
                    com.Transaction = (SqlTransaction)tran;
                }
                result = com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                DoException();
                throw ex;
            }
            finally
            {
                if (tran == null)
                {
                    Dispose(name);
                }
                this.CloseConnection();
            }
            return(result);
        }