管道基础大数据平台系统开发-【CS】-ExportMap
13693261870
2024-01-06 124102d5db2a2ace2f2d8484ceda658829657552
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
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Reflection;
using System.Web;
 
namespace JiangSu.cs
{
    public class Tools
    {
        private static PostgreHelper _pgHelper;
 
        /// <summary>
        /// DB帮助类
        /// </summary>
        public static PostgreHelper PGHelper
        {
            get
            {
                if (null == _pgHelper)
                {
                    _pgHelper = new PostgreHelper();
                }
 
                return _pgHelper;
            }
        }
 
        /// <summary>
        /// 获取参数列表
        /// </summary>
        public static DbParameter[] GetPGParams<T>(string sql, T t)
        {
            List<DbParameter> list = new List<DbParameter>();
            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);
                    NpgsqlParameter dp = new NpgsqlParameter("@" + name, value);
                    list.Add(dp);
                }
 
                start = sql.IndexOf("@", end);
            }
 
            return list.ToArray();
        }
 
        /// <summary>
        /// 获取参数列表
        /// </summary>
        public static SQLiteParameter[] GetSQLiteParams<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();
        }
    }
}