public List <T> Query <T>(SqlCondition condition)
        {
            EntityBase entity = EntityManager.GetEntity(condition.ConnectionName, condition.TableName);

            if (entity == null)
            {
                throw new Exception("Invalid connection or table.");
            }
            List <string> attrList = new List <string>();

            foreach (var attr in entity.Attributes.Keys)
            {
                if ((condition.FilterWay == FilterWay.FilterOut && !condition.FilterColumns.Contains(attr)) ||
                    (condition.FilterWay == FilterWay.Keep && condition.FilterColumns.Count == 0) ||
                    (condition.FilterWay == FilterWay.Keep && condition.FilterColumns.Contains(attr)))
                {
                    attrList.Add(attr);
                }
            }
            string sqlTemplate = "SELECT {0} FROM {1}";
            string sqlText     = string.Format(sqlTemplate, string.Join(",", attrList), condition.TableName);

            if (condition.Expression != null)
            {
                sqlText += condition.Expression.GenerateSqlText();
            }
            if (condition.OrderColumns != null && condition.OrderColumns.Count > 0)
            {
                sqlText = sqlText + string.Format(" ORDER BY {0} ", string.Join(",", condition.OrderColumns)) + condition.OrderBy.ToString();
            }
            string connStr = SqlServerConnectionProvider.Instance.GetConnectionString(condition.ConnectionName);

            return(SqlServerBaseOrder.ExecuteReader <T>(connStr, sqlText));
        }
        public int Insert <T>(SqlCondition condition, List <T> datas)
        {
            DataTable  dt     = new DataTable();
            EntityBase entity = EntityManager.GetEntity(condition.ConnectionName, condition.TableName);

            if (entity == null)
            {
                throw new Exception("Invalid connection or table.");
            }
            Hashtable mappings = new Hashtable();

            foreach (var attr in entity.Attributes.Keys)
            {
                dt.Columns.Add(attr, typeof(T).GetProperty(attr).PropertyType);
                mappings.Add(attr, attr);
            }
            foreach (var data in datas)
            {
                DataRow dr = dt.NewRow();
                foreach (var attr in entity.Attributes.Keys)
                {
                    dr[attr] = data.GetType().GetProperty(attr).GetValue(data);
                }
                dt.Rows.Add(dr);
            }
            string connStr = SqlServerConnectionProvider.Instance.GetConnectionString(condition.ConnectionName);

            return(SqlServerBaseOrder.ExecuteBulkCopy(connStr, condition.TableName, dt, mappings));
        }
        public int Insert <T>(SqlCondition condition, T data)
        {
            EntityBase entity = EntityManager.GetEntity(condition.ConnectionName, condition.TableName);

            if (entity == null)
            {
                throw new Exception("Invalid connection or table.");
            }
            List <string> attrList  = new List <string>();
            List <string> paramList = new List <string>();

            foreach (var attr in entity.Attributes.Keys)
            {
                attrList.Add(attr);
                paramList.Add(string.Format("@{0}", attr));
            }
            string sqlTemplate        = "INSERT INTO {0}({1})VALUES({2})";
            string sqlText            = string.Format(sqlTemplate, condition.TableName, string.Join(",", attrList), string.Join(",", paramList));
            List <SqlParameter> paras = new List <SqlParameter> {
            };

            foreach (var attr in entity.Attributes.Keys)
            {
                paras.Add(this.Convert <T>(entity.Attributes[attr], data));
            }
            string connStr = SqlServerConnectionProvider.Instance.GetConnectionString(condition.ConnectionName);

            return(SqlServerBaseOrder.ExecuteNonQuery(connStr, sqlText, paras.ToArray()));
        }
        public int Delete(SqlCondition condition)
        {
            string sqlTemplate = "DELETE FROM {0}";
            string sqlText     = string.Format(sqlTemplate, condition.TableName);

            if (condition.Expression != null)
            {
                sqlText += condition.Expression.GenerateSqlText();
            }
            string connStr = SqlServerConnectionProvider.Instance.GetConnectionString(condition.ConnectionName);

            return(SqlServerBaseOrder.ExecuteNonQuery(connStr, sqlText));
        }
        public int Update <T>(SqlCondition condition, T data)
        {
            EntityBase entity = EntityManager.GetEntity(condition.ConnectionName, condition.TableName);

            if (entity == null)
            {
                throw new Exception("Invalid connection or table.");
            }
            List <string> attrList       = new List <string>();
            List <string> expressionList = new List <string>();

            foreach (var attr in entity.Attributes.Keys)
            {
                if ((condition.FilterWay == FilterWay.FilterOut && !condition.FilterColumns.Contains(attr)) ||
                    (condition.FilterWay == FilterWay.Keep && condition.FilterColumns.Count == 0) ||
                    (condition.FilterWay == FilterWay.Keep && condition.FilterColumns.Contains(attr)))
                {
                    attrList.Add(attr);
                    expressionList.Add(string.Format("{0} = @{0}", attr));
                }
            }
            string sqlTemplate = "UPDATE {0} SET {1}";
            string sqlText     = string.Format(sqlTemplate, condition.TableName, string.Join(",", expressionList));

            if (condition.Expression != null)
            {
                sqlText += condition.Expression.GenerateSqlText();
            }
            List <SqlParameter> paras = new List <SqlParameter> {
            };

            foreach (var attr in attrList)
            {
                paras.Add(this.Convert <T>(entity.Attributes[attr], data));
            }
            string connStr = SqlServerConnectionProvider.Instance.GetConnectionString(condition.ConnectionName);

            return(SqlServerBaseOrder.ExecuteNonQuery(connStr, sqlText, paras.ToArray()));
        }
Exemplo n.º 6
0
 public DbContext(SqlCondition condition)
 {
     _condition = condition;
 }