Example #1
0
        private EntityResult GetEntityResult(SMT.DataResult dresult)
        {
            EntityResult result = new EntityResult();

            result.DataSet          = dresult.DataSet;
            result.DataTable        = dresult.DataTable;
            result.Sql              = dresult.Sql;
            result.IDataReader      = dresult.IDataReader;
            result.IDbDataParameter = dresult.IDbDataParameter;
            result.RecordCount      = dresult.RecordCount;
            result.Error            = dresult.Error;
            return(result);
        }
Example #2
0
        /// <summary>
        /// 是否存在记录
        /// </summary>
        /// <returns></returns>

        public EntityResult Exists()
        {
            string       sql    = "SELECT * FROM " + this.GetType().Name + " WHERE " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'";
            EntityResult result = GetEntityResult(SMT.DataProvider.ExistsRecord(sql));
            int          n      = result.RecordCount;

            if (n > 0)
            {
                eb = null;
                result.RecordCount = n;
            }
            else
            {
                result.RecordCount = 0;
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// 通过SQL语得到一个实体
        /// </summary>
        /// <typeparam name="TEntity">实体类</typeparam>
        /// <param name="t">实体类对象</param>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public DataContext GetEntity <TEntity>(TEntity t, string sql)// where TEntity : class
        {
            string error = "";

            entity = t;
            Result = GetEntityResult(SMT.DataProvider.GetDataReader(sql));
            IDataReader dt = Result.IDataReader;

            if (dt == null)
            {
                return(this);
            }
            Type type = t.GetType();

            if (dt.Read())
            {
                Result.RecordCount = 1;
                Hashtable has = new Hashtable();
                for (int i = 0; i < dt.FieldCount; i++)
                {
                    has.Add(dt.GetName(i).ToLower(), dt.GetValue(i));
                }

                try
                {
                    foreach (PropertyInfo p in type.GetProperties())
                    {
                        if (has.Contains(p.Name.ToLower()))
                        {
                            error = p.Name + "=" + has[p.Name.ToLower()];
                            p.SetValue(t, SMTProperty.SetPropertyValue(p, has[p.Name.ToLower()]), null);
                        }
                    }
                }
                catch (Exception e)
                {
                    error         = e.Message;
                    Result.Error += error + ";";
                }
            }
            dt.Close();
            entityList = null;//单个实体与实体列表不以冲突,所以清空
            return(this);
        }
Example #4
0
        private object GetChildClass(object t, string sql)
        {
            string error = "";

            Result = GetEntityResult(SMT.DataProvider.GetDataReader(sql));
            IDataReader dr = Result.IDataReader;

            if (dr == null)
            {
                return(t);
            }
            Type type = t.GetType();

            if (dr.Read())
            {
                try
                {
                    Hashtable has = new Hashtable();
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        has.Add(dr.GetName(i).ToLower(), dr.GetValue(i));
                    }
                    foreach (PropertyInfo p in type.GetProperties())
                    {
                        if (has.Contains(p.Name.ToLower()))
                        {
                            p.SetValue(t, SMTProperty.SetPropertyValue(p, has[p.Name.ToLower()]), null);
                        }
                    }
                }
                catch (Exception e)
                {
                    error         = e.Message;
                    Result.Error += error + ";";
                }
            }
            dr.Close();
            return(t);
        }
Example #5
0
        /// <summary>
        /// 分页绑定
        /// </summary>
        /// <typeparam name="TEntity">实体</typeparam>
        /// <param name="list">实体集合对像</param>
        /// <param name="index">页索引</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录数</param>
        /// <returns></returns>
        public DataContext GetEntityByPaging <TEntity>(List <TEntity> list, int index, int pageSize, out int pageCount, out int rowCount) where TEntity : new()
        {
            entityList = new List <object>();
            string error = "";

            entity = new TEntity();
            Type   type = entity.GetType();
            string sql  = "SELECT * FROM " + type.Name;

            Result = GetEntityResult(SMT.SMTPaging.BindPaging(sql, index, pageSize, out pageCount, out rowCount));
            DataTable dt = Result.DataTable;

            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    try
                    {
                        entity = new TEntity();
                        foreach (PropertyInfo p in type.GetProperties())
                        {
                            if (dt.Columns.Contains(p.Name))
                            {
                                p.SetValue(entity, SMTProperty.SetPropertyValue(p, dt.Rows[i][p.Name]), null);
                            }
                        }
                        list.Add((TEntity)entity);
                        entityList.Add((TEntity)entity);
                    }
                    catch (Exception e)
                    {
                        error         = e.Message;
                        Result.Error += error + ";";
                    }
                }
            }
            return(this);
        }
Example #6
0
        /// <summary>
        /// 更改(通过事务,必须在BeginTransaction()和 CommitTransaction()二个方法中间)
        /// </summary>

        public EntityResult UpdateByTransaction(IDbCommand idbconn)
        {
            #region 参数化
            eb = this;
            string sql  = "";
            Type   type = this.GetType();
            string name = "";
            if (has == null)
            {
                has = DataBaseType.GetTableColumn(this.GetType().Name);
            }
            SMT.DataProvider.CreateParameters(has.Count);
            int i = 0;
            foreach (PropertyInfo p in this.GetType().GetProperties())
            {
                //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null));
                //this.GetType().GetProperties()[3].Name
                if (has.Contains(p.Name.ToUpper()))
                {
                    if (PrimaryKeyName.ToUpper() != p.Name.ToUpper())
                    {
                        SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null));
                        sql  += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',";
                        name += p.Name.ToUpper() + "=:" + p.Name.ToUpper() + ",";
                        i++;
                    }
                }
            }
            sql = "UPDATE " + this.GetType().Name + " SET " + sql.TrimEnd(',') + " WHERE  " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'";
            SMT.DataProvider.AddParameters(i, PrimaryKeyName.ToUpper(), PrimaryKeyValue);
            string exeSql = "UPDATE " + this.GetType().Name + " SET " + name.TrimEnd(',') + " WHERE  " + PrimaryKeyName.ToUpper() + "=:" + PrimaryKeyName.ToUpper() + "";

            EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQLByTransaction(idbconn, exeSql));
            result.Sql = sql;
            return(result);

            #endregion
        }
Example #7
0
        //static  IDbConnection idbconn;
        ///// <summary>
        ///// 开始事务
        ///// </summary>
        //public   void BeginTransaction()
        //{
        //   idbconn= SMT.DataProvider.BeginTransaction();
        //}
        ///// <summary>
        ///// 提交事务
        ///// </summary>
        //public  bool CommitTransaction()
        //{
        //   return SMT.DataProvider.CommitTransaction();
        //}
        #region 事务增加\更新\删除
        /// <summary>
        /// 增加(通过事务,必须在BeginTransaction()和 CommitTransaction()二个方法中间)
        /// </summary>

        public EntityResult AddByTransaction(IDbCommand idbconn)
        {
            eb = this;
            Type   type   = this.GetType();
            string name   = "(";
            string value  = "(";
            string name2  = "(";
            string value2 = "(";

            if (has == null)
            {
                has = DataBaseType.GetTableColumn(this.GetType().Name);
            }
            SMT.DataProvider.CreateParameters(has.Count);
            int i = 0;

            foreach (PropertyInfo p in this.GetType().GetProperties())
            {
                if (has.Contains(p.Name.ToUpper()))
                {
                    SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null));
                    name   += p.Name.ToUpper() + ",";
                    value  += ":" + p.Name.ToUpper() + ",";
                    name2  += p.Name.ToUpper() + ",";
                    value2 += "'" + p.GetValue(eb, null) + "',";

                    i++;
                }
            }
            string       Sql    = "INSERT INTO  " + this.GetType().Name + " " + name2.TrimEnd(',') + ")VALUES" + value2.TrimEnd(',') + ")";
            string       exeSql = "INSERT INTO  " + this.GetType().Name + " " + name.TrimEnd(',') + ")VALUES" + value.TrimEnd(',') + ")";
            EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQLByTransaction(idbconn, exeSql));

            result.Sql = Sql;
            return(result);
        }
Example #8
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public DataContext()
 {
     Result = new EntityResult();
 }
Example #9
0
        private EntityResult GetEntityResult(SMT.DataResult dresult)
        {
            EntityResult result = new EntityResult();
            result.DataSet = dresult.DataSet;
            result.DataTable = dresult.DataTable;
            result.Sql = dresult.Sql;
            result.IDataReader = dresult.IDataReader;
            result.IDbDataParameter = dresult.IDbDataParameter;
            result.RecordCount = dresult.RecordCount;
            result.Error = dresult.Error;
            return result;

        }
Example #10
0
        /// <summary>
        /// 增加
        /// </summary>
        /// <returns></returns>

        public EntityResult Add()
        {
            eb = this;

            Type type = this.GetType();
            //string name = "(";
            //string value = "(";
            //foreach (PropertyInfo p in this.GetType().GetProperties())
            //{
            //    //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null));
            //    //this.GetType().GetProperties()[3].Name
            //    if (has.Contains(p.Name.ToUpper()))
            //    {
            //        name += p.Name.ToUpper() + ",";
            //        value +="'"+ p.GetValue(eb, null) +"',";
            //    }
            //}
            string name   = "(";
            string value  = "(";
            string name2  = "(";
            string value2 = "(";

            if (has == null || has.Count == 0)
            {
                has = DataBaseType.GetTableColumn(this.GetType().Name);
            }
            SMT.DataProvider.CreateParameters(has.Count);
            int i = 0;

            foreach (PropertyInfo p in this.GetType().GetProperties())
            {
                if (has.Contains(p.Name.ToUpper()))
                {
                    SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null));
                    name   += p.Name.ToUpper() + ",";
                    value  += ":" + p.Name.ToUpper() + ",";
                    name2  += p.Name.ToUpper() + ",";
                    value2 += "'" + p.GetValue(eb, null) + "',";
                    i++;
                }
            }
            if (has.Count != i)
            {//数据表的字段与实体定义的字段属表有不一样的,重新分配
                SMT.DataProvider.ClearParameters();

                name   = "(";
                value  = "(";
                name2  = "(";
                value2 = "(";
                SMT.DataProvider.CreateParameters(i);
                i = 0;
                foreach (PropertyInfo p in this.GetType().GetProperties())
                {
                    if (has.Contains(p.Name.ToUpper()))
                    {
                        SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null));
                        name   += p.Name.ToUpper() + ",";
                        value  += ":" + p.Name.ToUpper() + ",";
                        name2  += p.Name.ToUpper() + ",";
                        value2 += "'" + p.GetValue(eb, null) + "',";
                        i++;
                    }
                }
            }
            string       Sql    = "INSERT INTO  " + this.GetType().Name + " " + name2.TrimEnd(',') + ")VALUES" + value2.TrimEnd(',') + ")";
            string       exeSql = "INSERT INTO  " + this.GetType().Name + " " + name.TrimEnd(',') + ")VALUES" + value.TrimEnd(',') + ")";
            EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQL(exeSql));

            result.Sql = Sql;
            int n = result.RecordCount;

            if (n > 0)
            {
                result.RecordCount = n;
            }
            else
            {
                result.RecordCount = 0;
            }
            return(result);
        }
Example #11
0
        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>

        public EntityResult Update()
        {
            #region sql语句
            //Result result = new Result();
            //eb = this;
            //string sql="";
            //Type type=this.GetType();
            //foreach (PropertyInfo p in this.GetType().GetProperties())
            //     {
            //         //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null));
            //         //this.GetType().GetProperties()[3].Name
            //         if (has.Contains(p.Name.ToUpper()))
            //         {
            //             if (PrimaryKeyName.ToUpper() != p.Name.ToUpper())
            //             {
            //                 sql += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',";
            //             }
            //         }
            //     }
            //string exeSql="UPDATE "+this.GetType().Name+" SET "+sql.TrimEnd(',')+" WHERE  "+ PrimaryKeyName.ToUpper() + "='" + PrimaryKey + "'";
            //result.Sql = exeSql;
            //int n = SMT.DataProvider.ExecuteSQL(exeSql);
            //if (n > 0)
            //{
            //    result.RecordCount = n;
            //}
            //else
            //{
            //    result.RecordCount = 0;
            //}
            //return result;
            #endregion
            #region 参数化

            eb = this;
            string sql  = "";
            Type   type = this.GetType();
            string name = "";
            if (has == null)
            {
                has = DataBaseType.GetTableColumn(this.GetType().Name);
            }
            SMT.DataProvider.CreateParameters(has.Count);
            int i = 0;
            foreach (PropertyInfo p in this.GetType().GetProperties())
            {
                //Console.WriteLine(p.Name + "=" + p.GetValue(eb, null));
                //this.GetType().GetProperties()[3].Name
                if (has.Contains(p.Name.ToUpper()))
                {
                    if (PrimaryKeyName.ToUpper() != p.Name.ToUpper())
                    {
                        SMT.DataProvider.AddParameters(i, p.Name.ToUpper(), p.GetValue(eb, null));
                        sql  += p.Name.ToUpper() + "='" + p.GetValue(eb, null) + "',";
                        name += p.Name.ToUpper() + "=:" + p.Name.ToUpper() + ",";
                        i++;
                    }
                }
            }
            sql = "UPDATE " + this.GetType().Name + " SET " + sql.TrimEnd(',') + " WHERE  " + PrimaryKeyName.ToUpper() + "='" + PrimaryKeyValue + "'";
            SMT.DataProvider.AddParameters(i, PrimaryKeyName.ToUpper(), PrimaryKeyValue);
            string exeSql = "UPDATE " + this.GetType().Name + " SET " + name.TrimEnd(',') + " WHERE  " + PrimaryKeyName.ToUpper() + "=:" + PrimaryKeyName.ToUpper() + "";

            EntityResult result = GetEntityResult(SMT.DataProvider.ExecuteSQL(exeSql));
            result.Sql = sql;
            int n = result.RecordCount;
            if (n > 0)
            {
                result.RecordCount = n;
            }
            else
            {
                result.RecordCount = 0;
            }
            return(result);

            #endregion
        }