| | |
| | | { |
| | | public class ModelDAL |
| | | { |
| | | /// <summary> |
| | | /// 获取参数列表 |
| | | /// </summary> |
| | | public static SQLiteParameter[] GetParams<T>(string sql, T t) |
| | | { |
| | | List<SQLiteParameter> list = new List<SQLiteParameter>(); |
| | | Type tType = typeof(T); |
| | | BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; |
| | | |
| | | int start = sql.IndexOf("@"); |
| | | while (start != -1) |
| | | { |
| | | int end = sql.IndexOf(",", start); |
| | | if (end == -1) end = sql.IndexOf(")", start); |
| | | if (end == -1) end = sql.IndexOf(" ", start); |
| | | if (end == -1) end = sql.Length; |
| | | |
| | | string name = sql.Substring(start + 1, end - start - 1); |
| | | PropertyInfo pi = tType.GetProperty(name, flags); |
| | | if (pi != null) |
| | | { |
| | | object value = pi.GetValue(t, null); |
| | | SQLiteParameter dp = new SQLiteParameter("@" + name, value); |
| | | list.Add(dp); |
| | | } |
| | | |
| | | start = sql.IndexOf("@", end); |
| | | } |
| | | |
| | | return list.ToArray(); |
| | | } |
| | | |
| | | public static List<Model> SelectByPage(string name, int pageSize = 10, int pageIndex = 1) |
| | | { |
| | | bool flag = string.IsNullOrWhiteSpace(name); |
| | |
| | | public static int Insert(Model model) |
| | | { |
| | | string sql = "insert into model (name, json) values (@name, @json);select last_insert_rowid();"; |
| | | SQLiteParameter[] sqlParams = GetParams<Model>(sql, model); |
| | | SQLiteParameter[] sqlParams = Tools.GetSQLiteParams<Model>(sql, model); |
| | | |
| | | //return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); |
| | | |
| | |
| | | public static int UpdateById(Model model) |
| | | { |
| | | string sql = "update model set name = @name, json = @json where id = @id"; |
| | | SQLiteParameter[] sqlParams = GetParams<Model>(sql, model); |
| | | SQLiteParameter[] sqlParams = Tools.GetSQLiteParams<Model>(sql, model); |
| | | |
| | | return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); |
| | | } |