예제 #1
0
        public int Update(TEntity entity)
        {
            //UPDATE Customers SET Col1=@Col1,Col2=@Col2,Col3=@Col3;
            var props = _type.GetProperties();
            List <SqlParameter> sqlParameters = new List <SqlParameter>();

            foreach (var prop in props)
            {
                sqlParameters.Add(new SqlParameter($"@{prop.Name}", SqlHelperBase.ToDbValue(prop.GetValue(entity))));
            }

            return(_sqlHelperW.ExecuteNoQuery(_updateText, sqlParameters.ToArray()));
        }
예제 #2
0
        public object InsertBackRecord(TEntity entity)
        {
            //INSERT INTO Customers {col1,col2,clo3} VALUES(@Col1,@Col2,@Col3);

            var props = _type.GetProperties().Where(m => !m.Name.Equals("Id"));
            //var strCols = GetColumns().Where(m => !m.Equals("Id"));
            List <SqlParameter> sqlParameters = new List <SqlParameter>();

            foreach (var prop in props)
            {
                sqlParameters.Add(new SqlParameter($"@{prop.Name}", SqlHelperBase.ToDbValue(prop.GetValue(entity))));
            }

            return(_sqlHelperW.ExecuteScalar(_insertBackText, sqlParameters.ToArray()));
        }
예제 #3
0
        public int Insert(TEntity entity)
        {
            //INSERT INTO Customers {col1,col2,clo3} VALUES(@Col1,@Col2,@Col3);
            IEnumerable <PropertyInfo> props = _type.GetProperties().Where(m => !m.Name.Equals("Id"));
            //IEnumerable<string> strCols = GetColumns().Where(m => !m.Equals("Id"));

            List <SqlParameter> sqlParameters = new List <SqlParameter>();

            foreach (var prop in props)
            {
                sqlParameters.Add(new SqlParameter($"@{prop.Name}", SqlHelperBase.ToDbValue(prop.GetValue(entity))));
            }

            return(_sqlHelperW.ExecuteNoQuery(_insertText, sqlParameters.ToArray()));
        }
예제 #4
0
        private static TEntity ToModel(DataRow dr)
        {
            //如果给泛型加了一个new()的约束,那么次泛型可以直接通过new方法实例化
            //TEntity entity = new TEntity();
            //如果没有给泛型加new()的约束,则需要通过反射生成实例
            TEntity entity = (TEntity)Activator.CreateInstance(_type);
            var     props  = _type.GetProperties();

            foreach (var prop in props)
            {
                if (dr.Table.Columns.Contains(prop.Name))
                {
                    prop.SetValue(entity, SqlHelperBase.FromDbValue(dr[prop.Name]));
                }
            }
            return(entity);
        }