Пример #1
0
        //通过多个主键查询
        public T GetItemByPK(BaseTO to)
        {
            Query  query     = new Query();
            string tableName = BaseTO.GetTableName <T>();

            query.Selects = GetEntityExtendQuery();
            string[] primaryKeys = to.GetPrimaryKey <T>();
            if (null == primaryKeys || 0 == primaryKeys.Length)
            {
                throw new ExceptionNoPrimaryKey();
            }
            foreach (string pkName in primaryKeys)
            {
                query.Wheres.AddSql(string.Format("{0} = @{0}", pkName), new string[] { "@" + pkName });
                query.Parameters.Add("@" + pkName, new Query.ParameterItem()
                {
                    Type  = SqlDbType.NVarChar,
                    Value = to.GetPropertyValue(pkName).ToString()
                });
                //query.Wheres.AddEqualPair(pkName, to.GetPropertyValue(pkName).ToString());
            }
            return(GetItem(query));
        }
Пример #2
0
        public int UpdateFromTO(BaseTO to)
        {
            Dictionary <string, object> dict;

            dict = to.GetSQLWriteValues();
            //我把recordcount放在baseTo里面,用于列表分页时返回记录总数,这里新增修改时需要过滤掉 kevin
            dict.Remove("recordcount");
            StringBuilder sb = new StringBuilder();

            sb.Append("update ").Append(_tableName).Append(" set ");
            List <SqlParameter> parameters = new List <SqlParameter>();
            int i = 0;

            string[] primaryKeys = to.GetPrimaryKey <T>();
            foreach (string name in dict.Keys)
            {
                //不是主键才添加到update语句的更新字段中
                if (!primaryKeys.Contains <string>(name))
                {
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(name).Append("=@").Append(name);
                    parameters.Add(Query.BuilderParam(name, dict[name]));
                    i++;
                }
            }

            if (null == primaryKeys || 0 == primaryKeys.Length)
            {
                throw new ExceptionNoPrimaryKey();
            }
            sb.Append(" where 1=1 ");
            foreach (string primaryKeyName in primaryKeys)
            {
                sb.Append(" and ").Append(primaryKeyName).Append("=@" + primaryKeyName);
                parameters.Add(new SqlParameter("@" + primaryKeyName, to.GetPropertyValue(primaryKeyName)));
            }

            SqlTransConn transConn = GetConnection(_connectionName);
            int          result    = 0;

            try
            {
                result = SqlHelper.ExecuteNonQuery(transConn, CommandType.Text, sb.ToString(), parameters);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + ":" + sb.ToString(), ex);
            }
            finally
            {
                if (!transConn.IsInTransaction())
                {
                    transConn.Close();
                }
            }

            return(result);
        }