using Community.Model.Build; using System; using System.Collections.Generic; using System.Data.SQLite; using System.Linq; using System.Reflection; namespace Community.DAL { public class HouseDAL { private const string _queryFormat = "SELECT * FROM ZRZ WHERE XMMC LIKE '%{0}%' ORDER BY XMMC LIMIT {1} OFFSET {2}"; private const string _maxIdSql = "select ifnull(max(Id),0)+1 MaxId from {0}"; private const string _insertHouse = "insert into House (Id,Building,HouseState,FullName,BuildNum,Unit,HouseNum,Remark) values (@Id,@Building,@HouseState,@FullName,@BuildNum,@Unit,@HouseNum,@Remark)"; private const string _insertHousehold = "insert into Household (House,HoldType,Name,Gender,Nation,Political,IdNum,Addr,Phone,Remark) values (@House,@HoldType,@Name,@Gender,@Nation,@Political,@IdNum,@Addr,@Phone,@Remark)"; private const string _queryBuildingId = "select Id from Building where Name=@Name";//'罗庄南里2号楼' public static int GetHouseMaxId() { string sql = string.Format(_maxIdSql, "House"); object obj = SQLiteHelper.ExecuteScalar(sql, null); return obj == null ? 0 : Convert.ToInt32(obj); } public static int GetBuildingId(string name) { try { SQLiteParameter sp = new SQLiteParameter("@Name"); sp.Value = name; object obj = SQLiteHelper.ExecuteScalar(_queryBuildingId, sp); return obj == null ? -1 : Convert.ToInt32(obj); } catch { return 0; } } public static int InsertHouse(House h) { try { List list = GetParams(h, _insertHouse); return SQLiteHelper.ExecuteNonQuery(_insertHouse, list.ToArray()); } catch { return 0; } } private static List GetParams(T t, string insertSql) { Type type = typeof(T); BindingFlags flags = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance; string str = insertSql.Substring(insertSql.LastIndexOf("(") + 1, insertSql.Length - insertSql.LastIndexOf("(") - 2); string[] strs = str.Split(new char[] { ',' }); List list = new List(); for (int i = 0, c = strs.Length; i < c; i++) { PropertyInfo pi = type.GetProperty(strs[i].Replace("@", ""), flags); if (pi != null) { SQLiteParameter sp = new SQLiteParameter(strs[i]); sp.Value = pi.GetValue(t, null); list.Add(sp); } } return list; } public static int InsertHousehold(Household hh) { try { List list = GetParams(hh, _insertHousehold); return SQLiteHelper.ExecuteNonQuery(_insertHousehold, list.ToArray()); } catch { return 0; } } } }