分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-06-05 1a7f388092c07cba07a4948a82509b23222f7865
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.DBManager;
using WIDESEA_Core.Extensions;
using static OfficeOpenXml.ExcelErrorValue;
 
namespace WIDESEA_Comm.StoredProcedure
{
    public class CreateStoredProcedure
    {
        /// <summary>
        /// 创建存储过程
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="MethodName"></param>
        public static void Main<T>(T entity, string MethodName) where T : class
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(DBServerProvider.GetConnectionString()))
                {
                    string ProcedureName = MethodName + entity.GetType().Name;
 
 
                    string sqlCommand = $"CREATE PROCEDURE {ProcedureName} ";
                    connection.Open();
 
                    if (MethodName == "Add")
                        sqlCommand += Add(entity);
                    else if (MethodName == "Delete")
                        sqlCommand += Delete(entity);
                    else if (MethodName == "Update")
                        sqlCommand += Update(entity);
                    else if (MethodName == "Query")
                        sqlCommand += Query(entity);
 
                    using (SqlCommand command = new SqlCommand(sqlCommand, connection))
                    {
                        #region MyRegion
                        command.ExecuteNonQuery();
                        //Console.WriteLine($"存储过程 {ProcedureName} 已创建。");
                        #endregion
                        //if (count < 1)
                        //    throw new Exception($"存储过程 {ProcedureName} 创建失败!");
                    }
                }
            }
            catch (Exception ex)
            {
 
            }
        }
 
        #region 添加的存储过程
        public static string Add<T>(T entity) where T : class
        {
            string sql = string.Empty;
            string values = string.Empty;
            string Params = string.Empty;
 
            PropertyInfo[] propertyInfos = typeof(T).GetProperties();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                var ValueType = propertyInfo.GetColumnType(true).Value;
                sql += sql == string.Empty ? $"@{propertyInfo.Name} {ValueType}" : $",@{propertyInfo.Name} {ValueType}";
                values += values == string.Empty ? propertyInfo.Name : "," + propertyInfo.Name;
                Params += Params == string.Empty ? "@" + propertyInfo.Name : ",@" + propertyInfo.Name;
            }
            return $"{sql} AS\nBEGIN\nINSERT INTO {entity.GetType().Name} ({values}) VALUES ({Params}) END";
        }
        #endregion
 
        #region 删除的存储过程
        public static string Delete<T>(T entity) where T : class
        {
            PropertyInfo[] propertyInfos = typeof(T).GetProperties();
            var key = propertyInfos.GetKeyName(true);
            var name = propertyInfos.GetKeyProperty().Name;
            var value = propertyInfos.GetKeyProperty().GetValue(entity, null);
            string sql = $"@{name} {key} AS\nBEGIN\nDELETE FROM {entity.GetType().Name} WHERE {name}='{value}' END GO";
            return sql;
        }
        #endregion
 
        #region 更新的存储过程
        public static string Update<T>(T entity) where T : class
        {
            string sql = string.Empty;
            string values = string.Empty;
            string Params = string.Empty;
            try
            {
                PropertyInfo[] propertyInfos = typeof(T).GetProperties();
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    var ValueType = propertyInfo.GetColumnType(true).Value;
                    sql += sql == string.Empty ? $"@{propertyInfo.Name} {ValueType}" : $",@{propertyInfo.Name} {ValueType}";
                    values += values == string.Empty ? propertyInfo.Name : "," + propertyInfo.Name;
                    Params += Params == string.Empty ? "@" + propertyInfo.Name : ",@" + propertyInfo.Name;
                }
            }
            catch (Exception ex)
            {
 
                throw;
            }
            return sql;
        }
        #endregion
 
        #region 查询的存储过程
        public static string Query<T>(T entity) where T : class
        {
            string sql = string.Empty;
            string values = string.Empty;
            string Params = string.Empty;
 
            PropertyInfo[] propertyInfos = typeof(T).GetProperties();
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                var ValueType = propertyInfo.GetColumnType(true).Value;
                if (ValueType != "uniqueidentifier" && ValueType != "datetime")
                {
                    sql += sql == string.Empty ? $"@{propertyInfo.Name} {ValueType}" : $",@{propertyInfo.Name} {ValueType}";
                    values += values == string.Empty ? ($"{propertyInfo.Name}= {propertyInfo.Name}") : ($" AND {propertyInfo.Name}= {propertyInfo.Name}");
                    //values += values == string.Empty ? propertyInfo.Name : "," + propertyInfo.Name;
                    //Params += Params == string.Empty ? "@" + propertyInfo.Name : ",@" + propertyInfo.Name;
                }
            }
            return $"{sql} AS\nBEGIN\nSELECT * FROM {entity.GetType().Name} WHERE {values}  END";
        }
        #endregion
 
 
    }
}