1
13693261870
2022-11-19 dc201899900ef47d4abbbcf242b7df67ec048659
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
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<SQLiteParameter> list = GetParams<House>(h, _insertHouse);
                return SQLiteHelper.ExecuteNonQuery(_insertHouse, list.ToArray());
            }
            catch
            {
                return 0;
            }
        }
 
        private static List<SQLiteParameter> GetParams<T>(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<SQLiteParameter> list = new List<SQLiteParameter>();
            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<SQLiteParameter> list = GetParams<Household>(hh, _insertHousehold);
                return SQLiteHelper.ExecuteNonQuery(_insertHousehold, list.ToArray());
            }
            catch
            {
                return 0;
            }
        }
    }
}