Beispiel #1
0
        public static bool Exists(this ISqlObject obj)
        {
            string cmd = $"select * from {obj.Table} {obj.GetWhereExpression()}";

            var table = obj.SqlProvider.Query(cmd);

            return(table.Rows != null && table.Rows.Count != 0);
        }
Beispiel #2
0
        /// <summary>
        /// 尝试查询,如果成功,就将<see cref="SqlElementAttribute"/>标记的属性更新为数据库中的值。
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool TryQuery(this ISqlObject obj)
        {
            string cmd = $"select * from {obj.Table} {obj.GetWhereExpression()}";

            var table = obj.SqlProvider.Query(cmd);

            if (table.Rows.Count == 0)
            {
                return(false);
            }
            else
            {
                obj.SetValue(table.Rows[0]);
                return(true);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 根据<see cref="SqlSearchKeyAttribute"/>将数据库相应行中<see cref="SqlBindingAttribute"/>绑定的属性更新。
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="binding"></param>
        public static void Update(this ISqlObject obj, string binding)
        {
            //获取成员列表。
            NameValueList vs = new NameValueList();

            foreach (var property in obj.GetType().GetProperties())
            {
                if (property.CanWrite && property.CanRead && property.TryGetSqlElementName(out string name) && property.SqlBindingExists(binding))
                {
                    //加密。
                    vs.Add(name, GetSqlValue(property.GetValue(obj), property.IsSqlEncrypt()));
                }
            }
            //生成命令语句。
            string cmd = $"update {obj.Table} set " +
                         string.Join(',', vs.Map((m) => $"{m.Name}={m.Value}")) + " " +
                         obj.GetWhereExpression();

            obj.SqlProvider.Execute(cmd);
        }