Beispiel #1
0
        public bool Add(DbTransaction transaction = null)
        {
            string             tablename  = getTableName();
            List <DbParameter> paras      = new List <DbParameter>();
            StringBuilder      strSql     = new StringBuilder();
            StringBuilder      str1       = new StringBuilder(); //数据字段
            StringBuilder      str2       = new StringBuilder(); //数据参数
            PropertyInfo       identityPi = null;

            foreach (PropertyInfo pi in this.GetType().GetProperties())
            {
                var value = pi.GetValue(this, null);
                if (value == null)
                {
                    continue;
                }
                if (value.GetType() == typeof(DateTime) && (DateTime)value == DateTime.MinValue)
                {
                    continue;
                }
                var attrs = pi.GetCustomAttributes(typeof(AttrTableFieldInfo), false);
                if (attrs.Length > 0)
                {
                    AttrTableFieldInfo item = attrs[0] as AttrTableFieldInfo;
                    if (!item.IsIdentity)
                    {
                        paras.Add(dbh.MakeInParam(pi.Name, item.Type, item.Size, value ?? DBNull.Value));
                        str1.Append(pi.Name + ",");
                        str2.Append("@" + pi.Name + ",");
                    }
                    else
                    {
                        identityPi = pi;
                    }
                }
            }
            if (str1.Length > 0)
            {
                strSql.Append("insert into  " + tablename + "(");
                strSql.Append(str1.ToString().Trim(','));
                strSql.Append(") values (");
                strSql.Append(str2.ToString().Trim(','));
                strSql.Append(") ");
                if (identityPi == null)
                {
                    if (transaction != null)
                    {
                        return(dbh.ExecuteNonQuery(transaction, CommandType.Text, strSql.ToString(), paras.ToArray()) > 0);
                    }
                    else
                    {
                        return(dbh.ExecuteNonQuery(CommandType.Text, strSql.ToString(), paras.ToArray()) > 0);
                    }
                }
                else
                {
                    int id;
                    int optCnt = 0;
                    if (transaction != null)
                    {
                        optCnt = dbh.ExecuteNonQuery(out id, transaction, CommandType.Text, strSql.ToString(), paras.ToArray());
                    }
                    else
                    {
                        optCnt = dbh.ExecuteNonQuery(out id, CommandType.Text, strSql.ToString(), paras.ToArray());
                    }
                    if (optCnt > 0)
                    {
                        identityPi.SetValue(this, id, null);
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// 根据Form的数据填充当前模型
        /// </summary>
        public void BindForm()
        {
            foreach (PropertyInfo pi in this.GetType().GetProperties())
            {
                if (!MakRequest.Exists(pi.Name))
                {
                    continue;
                }
                var attrs = pi.GetCustomAttributes(typeof(AttrTableFieldInfo), false);
                if (attrs.Length > 0)
                {
                    AttrTableFieldInfo item = attrs[0] as AttrTableFieldInfo;
                    if (!item.IsPrimaryKey)
                    {
                        Type type = pi.PropertyType;
                        if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            //如果是带问号的类型
                            type = pi.PropertyType.GetGenericArguments()[0];
                        }
                        if (type == typeof(int))
                        {
                            pi.SetValue(this, MakRequest.GetInt(pi.Name, 0), null);
                        }
                        else if (type == typeof(long))
                        {
                            pi.SetValue(this, MakRequest.GetInt64(pi.Name, 0), null);
                        }
                        else if (type == typeof(string))
                        {
                            pi.SetValue(this, MakRequest.GetString(pi.Name), null);
                        }

                        else if (type == typeof(DateTime))
                        {
                            pi.SetValue(this, Utils.StrToDateTime(MakRequest.GetString(pi.Name), DateTime.MinValue).Value, null);
                        }
                        else if (type == typeof(bool))
                        {
                            string boolval = MakRequest.GetString(pi.Name).ToLower();
                            if (boolval == "1" || boolval == "yes" || boolval == "on")
                            {
                                pi.SetValue(this, true, null);
                            }
                            else
                            {
                                pi.SetValue(this, false, null);
                            }
                        }
                        else if (type == typeof(decimal))
                        {
                            pi.SetValue(this, (decimal)MakRequest.GetFloat(pi.Name, 0), null);
                        }
                        else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
                        {
                            pi.SetValue(this, MakRequest.GetFloat(pi.Name, 0), null);
                        }
                        else
                        {
                            throw new NotImplementedException("未完整定义的类型!" + pi.GetType().FullName);
                        }
                    }
                    var value = pi.GetValue(this, null);
                }
            }
        }
Beispiel #3
0
        public bool Update(DbTransaction transaction, string where = "")
        {
            string             tablename = getTableName();
            List <DbParameter> paras     = new List <DbParameter>();
            StringBuilder      strSql    = new StringBuilder();
            StringBuilder      str1      = new StringBuilder(); //数据字段
            StringBuilder      str2      = new StringBuilder(); //条件字段

            foreach (PropertyInfo pi in this.GetType().GetProperties())
            {
                var attrs = pi.GetCustomAttributes(typeof(AttrTableFieldInfo), false);
                if (attrs.Length > 0)
                {
                    AttrTableFieldInfo item = attrs[0] as AttrTableFieldInfo;
                    var value = pi.GetValue(this, null);
                    if (value != null && value.GetType() == typeof(DateTime) && (DateTime)value == DateTime.MinValue)
                    {
                        continue;
                    }
                    if (item.IsPrimaryKey)
                    {
                        str2.Append(pi.Name + "=@" + pi.Name + ",");
                        paras.Add(dbh.MakeInParam(pi.Name, item.Type, item.Size, value ?? DBNull.Value));
                    }
                    if (sourceDt != null && sourceDt.Rows.Count > 0)
                    {
                        object dbval = sourceDt.Rows[0][pi.Name];
                        if (dbval == DBNull.Value || value == null)
                        {
                            continue;
                        }
                        else if (value.ToString() == dbval.ToString())
                        {
                            continue;
                        }
                        else if (dbval.GetType() == typeof(float) || dbval.GetType() == typeof(double) || dbval.GetType() == typeof(decimal))
                        {
                            float f1 = Utils.StrToFloat(value, 0);
                            float f2 = Utils.StrToFloat(dbval, 0);
                            if (f1 == f2)
                            {
                                continue;
                            }
                        }
                    }
                    if (!item.IsIdentity)
                    {
                        str1.Append(pi.Name + "=@" + pi.Name + ",");
                        paras.Add(dbh.MakeInParam(pi.Name, item.Type, item.Size, value ?? DBNull.Value));
                    }
                }
            }
            if (str1.Length == 0)
            {
                return(true);
            }
            strSql.Append("Update " + tablename + " set ");
            strSql.Append(str1.ToString().Trim(','));
            strSql.Append(" where ");
            if (str2.Length > 0)
            {
                strSql.Append(str2.ToString().Trim(','));
            }
            else
            {
                strSql.Append(" id=@id ");
            }

            if (!string.IsNullOrEmpty(where))
            {
                strSql.Append(" and ").Append(where);
            }
            if (transaction != null)
            {
                return(dbh.ExecuteNonQuery(transaction, CommandType.Text, strSql.ToString(), paras.ToArray()) > 0);
            }
            else
            {
                return(dbh.ExecuteNonQuery(CommandType.Text, strSql.ToString(), paras.ToArray()) > 0);
            }
        }