-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLUtility.cs
More file actions
203 lines (181 loc) · 7.55 KB
/
SQLUtility.cs
File metadata and controls
203 lines (181 loc) · 7.55 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using Dapper;
namespace CPUFramework
{
public static class SQLUtility
{
public enum ExecSQLTypeEnum { NoResultSet, SingleRecord, MultipleRecord}
public static List<T> ExecuteGetListDapper<T>(string sprocname, DynamicParameters dynamparam) where T : new()
{
(T tobj, List<T> tlst) = DoExecuteSQLDapper<T>(sprocname, dynamparam, ExecSQLTypeEnum.MultipleRecord);
return tlst;
}
public static T ExecuteGetSingleDapper<T>(string sprocname, DynamicParameters dynamparam) where T : new()
{
(T tobj, List<T> tlst) = DoExecuteSQLDapper<T>(sprocname, dynamparam, ExecSQLTypeEnum.SingleRecord);
return tobj;
}
public static void ExecuteSQLDapper(string sprocname, DynamicParameters dynamparam)
{
DoExecuteSQLDapper<object>(sprocname, dynamparam);
}
private static (T, List<T> )DoExecuteSQLDapper<T>(string sprocname, DynamicParameters dynamparam, ExecSQLTypeEnum execsqltype = ExecSQLTypeEnum.NoResultSet) where T :new()
{
T tobj = new();
List<T> tlst= new();
dynamparam.Add("Message", "", direction: ParameterDirection.InputOutput);
dynamparam.Add("return_value", "", direction: ParameterDirection.ReturnValue);
using (SqlConnection conn = new SqlConnection(DataUtility.ConnectionString))
{
try
{
switch (execsqltype)
{
case ExecSQLTypeEnum.SingleRecord:
tobj = conn.QueryFirstOrDefault<T>(sprocname, dynamparam, commandType: CommandType.StoredProcedure);
if (tobj == null)
{
tobj = new T();
}
break;
case ExecSQLTypeEnum.MultipleRecord:
tlst = conn.Query<T>(sprocname, dynamparam, commandType: CommandType.StoredProcedure).ToList();
break;
default:
conn.Execute(sprocname, dynamparam, commandType: CommandType.StoredProcedure);
break;
}
}
catch (SqlException ex) when (IsConstraintError(ex.Message))
{
throw new CPUException(ex.Message);
}
}
int ret = dynamparam.Get<int>("return_value");
string msg = dynamparam.Get<string>("Message");
if (ret == 1)
{
throw new CPUException(msg);
}
return(tobj, tlst);
}
//internal static object DoGet<T>(string SprocName, DynamicParameters dynamparam, ExecSQLTypeEnum execsqltype)
//{
// object obj = null;
// using (SqlConnection conn = new SqlConnection(DataUtility.ConnectionString))
// {
// switch (execsqltype)
// {
// case ExecSQLTypeEnum.SingleRecord:
// obj = conn.QueryFirstOrDefault<T>(SprocName, dynamparam, commandType: CommandType.StoredProcedure);
// break;
// case ExecSQLTypeEnum.MultipleRecord:
// obj = conn.Query<T>(SprocName, dynamparam, commandType: CommandType.StoredProcedure).ToList();
// break;
// }
// return obj;
// }
//}
private static DataTable ExecuteSQl(SqlCommand cmd, string connstringvalue)
{
Debug.Print(GetSQL(cmd));
using (SqlConnection conn = new SqlConnection(connstringvalue))
{
cmd.Connection = conn;
conn.Open();
if (cmd.CommandType == CommandType.StoredProcedure && cmd.Parameters.Count == 0)
{
SqlCommandBuilder.DeriveParameters(cmd);
}
DataTable dt = new DataTable();
try
{
SqlDataReader dr = cmd.ExecuteReader();
if (cmd.Parameters.Contains("@return_value") && cmd.Parameters["@return_value"].Value != null)
{
int returnval = (int)cmd.Parameters["@return_value"].Value;
if (returnval == 1)
{
string? msg = cmd.Parameters["@Message"].Value.ToString();
throw new CPUException(msg, cmd.CommandText);
}
}
dt.Load(dr);
}
catch (SqlException ex) when (IsConstraintError(ex.Message))
{
throw new CPUException(ex.Message, cmd.CommandText);
}
return dt;
}
}
private static bool IsConstraintError(string message)
{
bool b = false;
if (message.ToLower().Contains("f_") || message.ToLower().Contains("ck_"))
{
b = true;
}
return b;
//unique violations and check constarints
}
public static SqlCommand GetSQLCommand(string connstringvalue, string sprocname)
{
using (SqlConnection conn = new SqlConnection(connstringvalue))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sprocname, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(cmd);
return cmd;
}
}
//getdATA tabke by executing the command that is passed in this procedure
public static DataTable GetDataTable(SqlCommand cmd, string connstringvalue)
{
return ExecuteSQl(cmd, connstringvalue);
}
public static DataTable GetDataTable(string connstringvalue, string sqlstatement)
{
SqlCommand cmd = new SqlCommand(sqlstatement);
cmd.CommandType = CommandType.Text;
return ExecuteSQl(cmd, connstringvalue);
}
public static DataTable GetDataTableFromSproc(string connstringvalue, string procname)
{
SqlCommand cmd = new SqlCommand(procname) { CommandType = CommandType.StoredProcedure };
return ExecuteSQl(cmd, connstringvalue);
}
private static string GetSQL(SqlCommand cmd)
{
string val = "";
if (cmd.Connection != null)
{
val += "**********\n--" + cmd.Connection.DataSource + "\nuse " + cmd.Connection.Database + "\ngo\n";
}
if (cmd.CommandType == CommandType.StoredProcedure)
{
val += "exec " + cmd.CommandText;
foreach (SqlParameter p in cmd.Parameters)
{
if (p.ParameterName != "@RETURN_VALUE")
{
string paramval = "null";
if (p.Value != null)
{
paramval = p.Value.ToString();
}
val += "\n" + p.ParameterName + " = " + paramval;
}
}
}
else
{
val += cmd.CommandText;
}
return val;
}
}
}