分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-19 7a4c218909936721fe281737491d10efc7378e09
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
143
144
145
146
147
148
149
150
151
152
using FreeSql;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Comm.DBItem;
using WIDESEA_Core.DBManager;
using WIDESEA_Core.Extensions;
using static Dapper.SqlMapper;
 
namespace WIDESEA_WCS.JobsPart.Common
{
    public class StoredProcedure
    {
        /// <summary>
        /// 创建存储过程
        /// </summary>
        public static void Main<T>(T entity, string MethodName) where T : class
        {
            //string storedProcedureName = "MyNewStoredProcedure";
 
            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);
                if (MethodName == "Delete")
                    sqlCommand += Delete(entity);
                // 创建存储过程的SQL脚本
                //string sqlCommand = $@"
                //CREATE PROCEDURE {ProcedureName}
                //AS
                //BEGIN
                //SELECT * FROM {TableName}
                //END";
 
                using (SqlCommand command = new SqlCommand(sqlCommand, connection))
                {
                    command.ExecuteNonQuery();
                    Console.WriteLine($"存储过程 {ProcedureName} 已创建。");
                }
            }
        }
 
        #region 添加的存储过程
        public static string Add<T>(T entity) where T : class
        {
 
            //string sqlCommand = $"CREATE PROCEDURE {ProcedureName} ";
 
            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;
                //sqlCommand += $@"@{propertyInfo.Name} {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;
                //var name = "@" + propertyInfo.Name;
                //var value = propertyInfo.GetValue(entity, null);
            }
            //sqlCommand += $"{sql} AS\nBEGIN\nINSERT INTO {entity.GetType().Name} ({values}) VALUES ({Params}) END GO";
 
 
            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 调用存储过程
        /// <summary>
        /// 调用存储过程
        /// </summary>
        /// <param name="ProcedureName">存储过程名称</param>
        /// <param name="Param1">参数1</param>
        public static void EXEC(string ProcedureName, object Param1)
        {
            using (SqlConnection conn = new SqlConnection(DBServerProvider.GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand("DeleteOldRecords", conn);//存储过程名称
                cmd.CommandType = CommandType.StoredProcedure;
                //PropertyInfo[] propertyInfos = typeof(Param1).GetProperties();
                //cmd.Parameters.AddWithValue("@Param1", "Value1");//参数1
                cmd.Parameters.AddWithValue("@MonthsAgo", 6);//参数2
                conn.Open();
                var count = cmd.ExecuteNonQuery();
                if (count > 0)
                {
 
                }
            }
        }
        #endregion
 
        /// <summary>
        /// 调用添加存储过程
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="ProcedureName">存储过程名称</param>
        public static void EXECAdd<T>(T entity, string MethodName) where T : class
        {
            string ProcedureName = MethodName + entity.GetType().Name;
            using (SqlConnection conn = new SqlConnection(DBServerProvider.GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand(ProcedureName, conn);//存储过程名称"Add_inventory"
                cmd.CommandType = CommandType.StoredProcedure;
 
                PropertyInfo[] propertyInfos = typeof(T).GetProperties();
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    var value = propertyInfo.GetValue(entity, null);
                    cmd.Parameters.AddWithValue("@" + propertyInfo.Name, value);//参数
                }
 
                conn.Open();
                cmd.ExecuteNonQuery();
                //var count = cmd.ExecuteNonQuery();
                //if (count > 0)
                //{
                //    Console.WriteLine($"存储过程 {ProcedureName} 调用成功。");
                //}
            }
        }
    }
}