Exemple #1
0
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">需要更新到数据库的实体类</param>
        /// <param name="sColList">需要更新的字段</param>
        public virtual bool Update(T model, string sColList)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update  " + TableName + " set ");
            PropertyInfo[] pis = ModelProperties;
            //  typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
            List <DbParam> listParam = new List <DbParam>();

            if (string.IsNullOrEmpty(sColList))
            {
                for (int i = 0; i < pis.Length; i++)
                {
                    if (pis[i].Name != PrimaryKey)
                    {
                        strSql.AppendFormat("[{0}]=@{0},", pis[i].Name);
                    }
                    listParam.Add(new DbParam
                    {
                        ParamDbType = DbTypeConvert.GetDbType(pis[i].PropertyType),
                        ParamName   = pis[i].Name,
                        ParamValue  = pis[i].GetValue(model, null)
                    });
                }
            }
            else
            {
                string[] strArr = sColList.Split(',');
                foreach (PropertyInfo pi in pis)
                {
                    for (int i = 0; i < strArr.Length; i++)
                    {
                        if (pi.Name == PrimaryKey || pi.Name != strArr[i])
                        {
                            continue;
                        }
                        strSql.AppendFormat("[{0}]=@{0},", pi.Name);
                    }

                    listParam.Add(new DbParam
                    {
                        ParamDbType = DbTypeConvert.GetDbType(pi.PropertyType),
                        ParamName   = pi.Name,
                        ParamValue  = pi.GetValue(model, null)
                    });
                }
            }
            strSql = strSql.Replace(",", " ", strSql.Length - 1, 1);
            strSql.Append(" where " + PrimaryKey + "=@" + PrimaryKey);
            Database db = DatabaseFactory.CreateDatabase(connName);

            using (DbCommand cmd = db.GetSqlStringCommand(strSql.ToString()))
            {
                foreach (DbParam dbpm in listParam)
                {
                    db.AddInParameter(cmd, dbpm.ParamName, dbpm.ParamDbType, dbpm.ParamValue);
                }
                return(db.ExecuteNonQuery(cmd) > 0 ? true : false);
            }
        }
Exemple #2
0
        /// <summary>
        /// 添加记录,返回当前插入的这条记录的ID
        /// </summary>
        /// <param name="model">实体层某实体的实例</param>
        public virtual int Add(T model)
        {
            StringBuilder strSql       = new StringBuilder();
            StringBuilder strParameter = new StringBuilder();

            strSql.Append(string.Format("insert into {0}(", TableName));
            PropertyInfo[] pis = ModelProperties;
            //typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
            List <DbParam> listParam = new List <DbParam>();

            for (int i = 0; i < pis.Length; i++)
            {
                if (t.IsAutoId)
                {
                    if (t.PrimaryKey == pis[i].Name)
                    {
                        continue;
                    }
                }
                strSql.Append("[" + pis[i].Name + "],");      //构造SQL语句前半部份
                strParameter.Append("@" + pis[i].Name + ","); //构造参数SQL语句
                listParam.Add(new DbParam
                {
                    ParamName   = pis[i].Name,
                    ParamDbType = DbTypeConvert.GetDbType(pis[i].PropertyType),
                    ParamValue  = pis[i].GetValue(model, null)
                });
            }
            strSql       = strSql.Replace(",", ")", strSql.Length - 1, 1);
            strParameter = strParameter.Replace(",", ")", strParameter.Length - 1, 1);
            strSql.Append(" values (");
            strSql.Append(strParameter + ";");
            if (t.IsAutoId)
            {
                strSql.Append("select SCOPE_IDENTITY()");
            }
            Database db = DatabaseFactory.CreateDatabase(connName);

            using (DbCommand cmd = db.GetSqlStringCommand(strSql.ToString()))
            {
                foreach (DbParam dbpm in listParam)
                {
                    db.AddInParameter(cmd, dbpm.ParamName, dbpm.ParamDbType, dbpm.ParamValue);
                }
                object obj = db.ExecuteScalar(cmd);
                return(DbTypeConvert.ToInt32(obj));
            }
        }
Exemple #3
0
        protected virtual List <DbParam> GetAddUpdatePms(T model, bool IsAdd)
        {
            PropertyInfo[] pis       = ModelProperties;
            List <DbParam> listParam = new List <DbParam>();

            for (int i = 0; i < pis.Length; i++)
            {
                if (IsAdd)
                {
                    if (t.IsAutoId)
                    {
                        if (t.PrimaryKey == pis[i].Name)
                        {
                            continue;
                        }
                    }
                }
                if (model == null)
                {
                    listParam.Add(new DbParam
                    {
                        ParamName   = pis[i].Name,
                        ParamDbType = DbTypeConvert.GetDbType(pis[i].PropertyType),
                        ParamValue  = null
                    });
                }
                else
                {
                    listParam.Add(new DbParam
                    {
                        ParamName   = pis[i].Name,
                        ParamDbType = DbTypeConvert.GetDbType(pis[i].PropertyType),
                        ParamValue  = pis[i].GetValue(model, null)
                    });
                }
            }
            return(listParam);
        }