Example #1
0
        /// <summary>
        /// 根据实体类创建表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void CreateTable <T>()
        {
            Type          type          = typeof(T);
            List <string> columnNames   = new List <string>();
            List <string> columnType    = new List <string>();
            bool          isTableColumn = true;

            foreach (var item in type.GetProperties())
            {
                isTableColumn = true;
                object[] attrs = item.GetCustomAttributes(true);
                foreach (var attr in attrs)
                {
                    if (attr is PropertyNameAttribute)
                    {
                        PropertyNameAttribute attr2 = attr as PropertyNameAttribute;
                        if (attr2.IsTableColumn)
                        {
                            isTableColumn = false;
                            break;
                        }
                    }
                }
                if (isTableColumn)
                {
                    columnNames.Add(item.Name);
                    columnType.Add(item.PropertyType.Name);
                }
            }
            CreateTable(ExtendTableName.GetTableName(type), columnNames.ToArray(), columnType.ToArray());
        }
Example #2
0
        /// <summary>
        /// 更新
        /// WHERE   {whereStr}
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="whereStr"></param>
        /// <returns></returns>
        public bool Update <T>(T t, string whereStr)
        {
            var type   = typeof(T);
            var values = new List <string>();
            //无法将类型为“System.Data.SqlClient.SQLiteParameter”的对象强制转换为类型“System.Data.SQLite.SQLiteParameter”。
            var  par           = new List <SQLiteParameter>();
            bool isTableColumn = true;

            foreach (var item in type.GetProperties())
            {
                isTableColumn = true;
                object[] objArr = item.GetCustomAttributes(true);
                foreach (var item2 in objArr)
                {
                    if (item2 is PropertyNameAttribute)
                    {
                        PropertyNameAttribute pro = item2 as PropertyNameAttribute;
                        if (pro.IsTableColumn)
                        {
                            isTableColumn = false;
                            break;
                        }
                    }
                }
                if (isTableColumn)
                {
                    var value = item.GetValue(t, null);
                    if (item.Name.ToLower() != "id")
                    {
                        par.Add(new SQLiteParameter(item.Name, value));
                        values.Add(string.Format($"[{item.Name}] = @{item.Name}"));
                    }
                }
            }
            var v   = string.Join(",", values.ToArray());
            var sql = string.Format($"UPDATE [{ExtendTableName.GetTableName(type)}] SET {v} WHERE   {whereStr}");

            return(ExcuteSql <bool>(sql, cmd =>
            {
                foreach (var parameter in par)
                {
                    cmd.Parameters.Add(parameter);
                }

                var result = cmd.ExecuteNonQuery();
                return result > 0;
            }));
            // return RunCmd(sql, par.ToArray());
        }
Example #3
0
        private T CreateT <T>(SQLiteDataReader reader)
        {
            var  type          = typeof(T);
            var  t             = Activator.CreateInstance(type);
            bool isTableColumn = true;

            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                object[] objArr = propertyInfo.GetCustomAttributes(true);
                isTableColumn = true;
                foreach (var item in objArr)
                {
                    if (item is PropertyNameAttribute)
                    {
                        PropertyNameAttribute proper = item as PropertyNameAttribute;
                        isTableColumn = proper.IsTableColumn;
                        break;
                    }
                }
                if (objArr.Length > 0 || !isTableColumn)
                {
                    if (!isTableColumn)
                    {
                        if (propertyInfo.CanWrite)
                        {
                            var value = reader[propertyInfo.Name];
                            if (!(value is DBNull))
                            {
                                propertyInfo.SetValue(t, value, null);
                            }
                        }
                    }
                }
                else
                {
                    if (propertyInfo.CanWrite)
                    {
                        var value = reader[propertyInfo.Name];
                        if (!(value is DBNull))
                        {
                            propertyInfo.SetValue(t, value, null);
                        }
                    }
                }
            }
            return((T)t);
        }
Example #4
0
        private string GetSql <T>(T t, bool IsGetId, ref List <SQLiteParameter> par)
        {
            //插入实体时要判断一下时间的插入.还可以通过特性来判断一下数据的长度
            var  type          = typeof(T);
            var  keys          = new List <string>();
            var  values        = new List <string>();
            bool isTableColumn = true;

            foreach (var item in type.GetProperties())
            {
                isTableColumn = true;
                object[] objArr = item.GetCustomAttributes(true);
                foreach (var obj in objArr)
                {
                    if (obj is PropertyNameAttribute)
                    {
                        PropertyNameAttribute propertyNameAttribute = obj as PropertyNameAttribute;
                        if (propertyNameAttribute.IsTableColumn || propertyNameAttribute.IsPrimaryKey == 1)
                        {
                            isTableColumn = false;
                            break;
                        }
                    }
                }
                if (isTableColumn)
                {
                    var value = item.GetValue(t, null);
                    keys.Add(item.Name);
                    values.Add("@" + item.Name);
                    par.Add(new SQLiteParameter("@" + item.Name, value));
                }
            }
            var    c      = string.Join(",", keys.ToArray());
            var    v      = string.Join(",", values.ToArray());
            string sqlStr = string.Format($"INSERT INTO [{ExtendTableName.GetTableName(type)}] ({c}) " +
                                          $"VALUES({v})");

            if (IsGetId)
            {
                sqlStr += ";SELECT last_insert_rowid();";
            }
            return(sqlStr);
        }