1- using System ;
21using System . Collections . Generic ;
32using System . Data ;
43using System . Data . SqlClient ;
@@ -23,6 +22,7 @@ static MsSql()
2322 "BctSpConnectionStringOrConfigurationPath"
2423 } ;
2524 }
25+
2626 public MsSql ( string connectionString )
2727 {
2828 _connectionString = connectionString ;
@@ -33,45 +33,35 @@ public IDictionary<string, object> QueryFirst(IDictionary<string, object> parame
3333 IDictionary < string , object > result = new ExpandoObject ( ) ;
3434 using var connection = new SqlConnection ( _connectionString ) ;
3535 connection . Open ( ) ;
36- try
37- {
38- using var command = connection . CreateCommand ( ) ;
3936
40- command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
41- command . CommandType = CommandType . StoredProcedure ;
37+ using var command = connection . CreateCommand ( ) ;
4238
43- foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
44- {
45- command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
46- }
39+ command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
40+ command . CommandType = CommandType . StoredProcedure ;
41+
42+ foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
43+ {
44+ command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
45+ }
4746
48- using var reader = command . ExecuteReader ( ) ;
47+ using var reader = command . ExecuteReader ( ) ;
4948
50- while ( reader . Read ( ) )
49+ while ( reader . Read ( ) )
50+ {
51+ for ( var i = 0 ; i < reader . FieldCount ; i ++ )
5152 {
52- for ( var i = 0 ; i < reader . FieldCount ; i ++ )
53+ if ( result . ContainsKey ( reader . GetName ( i ) ) )
5354 {
54- if ( result . ContainsKey ( reader . GetName ( i ) ) )
55- {
56- continue ;
57- }
58-
59- result . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
55+ continue ;
6056 }
6157
62- if ( result . Any ( ) )
63- {
64- break ;
65- }
58+ result . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
59+ }
60+
61+ if ( result . Any ( ) )
62+ {
63+ break ;
6664 }
67- }
68- catch ( Exception )
69- {
70- // ignored
71- }
72- finally
73- {
74- connection . Close ( ) ;
7565 }
7666
7767 return result ;
@@ -82,38 +72,27 @@ public IEnumerable<ExpandoObject> Query(IDictionary<string, object> parameters)
8272 var result = new List < ExpandoObject > ( ) ;
8373 using var connection = new SqlConnection ( _connectionString ) ;
8474 connection . Open ( ) ;
85- try
86- {
87- using var command = connection . CreateCommand ( ) ;
75+ using var command = connection . CreateCommand ( ) ;
8876
89- command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
90- command . CommandType = CommandType . StoredProcedure ;
77+ command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
78+ command . CommandType = CommandType . StoredProcedure ;
9179
92- foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
93- {
94- command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
95- }
80+ foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
81+ {
82+ command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
83+ }
9684
97- using var reader = command . ExecuteReader ( ) ;
85+ using var reader = command . ExecuteReader ( ) ;
9886
99- while ( reader . Read ( ) )
87+ while ( reader . Read ( ) )
88+ {
89+ IDictionary < string , object > response = new ExpandoObject ( ) ;
90+ for ( var i = 0 ; i < reader . FieldCount ; i ++ )
10091 {
101- IDictionary < string , object > response = new ExpandoObject ( ) ;
102- for ( var i = 0 ; i < reader . FieldCount ; i ++ )
103- {
104- response . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
105- }
106-
107- result . Add ( ( ExpandoObject ) response ) ;
92+ response . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
10893 }
109- }
110- catch ( Exception )
111- {
112- //ignored
113- }
114- finally
115- {
116- connection . Close ( ) ;
94+
95+ result . Add ( ( ExpandoObject ) response ) ;
11796 }
11897
11998 return result ;
@@ -123,73 +102,51 @@ public void NonQuery(IDictionary<string, object> parameters)
123102 {
124103 using var connection = new SqlConnection ( _connectionString ) ;
125104 connection . Open ( ) ;
126- try
127- {
128- using var command = connection . CreateCommand ( ) ;
105+ using var command = connection . CreateCommand ( ) ;
129106
130- command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
131- command . CommandType = CommandType . StoredProcedure ;
107+ command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
108+ command . CommandType = CommandType . StoredProcedure ;
132109
133- foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
134- {
135- command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
136- }
137-
138- command . ExecuteNonQuery ( ) ;
139- }
140- catch ( Exception )
141- {
142- //ignored
143- }
144- finally
110+ foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
145111 {
146- connection . Close ( ) ;
112+ command . Parameters . Add ( new SqlParameter ( $ "@ { property . Key } " , property . Value ) ) ;
147113 }
114+
115+ command . ExecuteNonQuery ( ) ;
148116 }
149117
150118 public async Task < IDictionary < string , object > > QueryFirstAsync ( IDictionary < string , object > parameters )
151119 {
152120 IDictionary < string , object > result = new ExpandoObject ( ) ;
153121 await using var connection = new SqlConnection ( _connectionString ) ;
154122 await connection . OpenAsync ( ) ;
155- try
156- {
157- await using var command = connection . CreateCommand ( ) ;
123+ await using var command = connection . CreateCommand ( ) ;
158124
159- command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
160- command . CommandType = CommandType . StoredProcedure ;
125+ command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
126+ command . CommandType = CommandType . StoredProcedure ;
161127
162- foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
163- {
164- command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
165- }
128+ foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
129+ {
130+ command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
131+ }
166132
167- await using var reader = await command . ExecuteReaderAsync ( ) ;
168- while ( await reader . ReadAsync ( ) )
133+ await using var reader = await command . ExecuteReaderAsync ( ) ;
134+ while ( await reader . ReadAsync ( ) )
135+ {
136+ for ( var i = 0 ; i < reader . FieldCount ; i ++ )
169137 {
170- for ( var i = 0 ; i < reader . FieldCount ; i ++ )
138+ if ( result . ContainsKey ( reader . GetName ( i ) ) )
171139 {
172- if ( result . ContainsKey ( reader . GetName ( i ) ) )
173- {
174- continue ;
175- }
176-
177- result . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
140+ continue ;
178141 }
179142
180- if ( result . Any ( ) )
181- {
182- break ;
183- }
143+ result . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
144+ }
145+
146+ if ( result . Any ( ) )
147+ {
148+ break ;
184149 }
185- }
186- catch ( Exception )
187- {
188- //ignored
189- }
190- finally
191- {
192- await connection . CloseAsync ( ) ;
193150 }
194151
195152 return result ;
@@ -200,38 +157,27 @@ public async Task<IEnumerable<ExpandoObject>> QueryAsync(IDictionary<string, obj
200157 var result = new List < ExpandoObject > ( ) ;
201158 await using var connection = new SqlConnection ( _connectionString ) ;
202159 await connection . OpenAsync ( ) ;
203- try
204- {
205- await using var command = connection . CreateCommand ( ) ;
160+ await using var command = connection . CreateCommand ( ) ;
206161
207- command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
208- command . CommandType = CommandType . StoredProcedure ;
162+ command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
163+ command . CommandType = CommandType . StoredProcedure ;
209164
210- foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
211- {
212- command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
213- }
165+ foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
166+ {
167+ command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
168+ }
214169
215- await using var reader = await command . ExecuteReaderAsync ( ) ;
170+ await using var reader = await command . ExecuteReaderAsync ( ) ;
216171
217- while ( await reader . ReadAsync ( ) )
172+ while ( await reader . ReadAsync ( ) )
173+ {
174+ IDictionary < string , object > response = new ExpandoObject ( ) ;
175+ for ( var i = 0 ; i < reader . FieldCount ; i ++ )
218176 {
219- IDictionary < string , object > response = new ExpandoObject ( ) ;
220- for ( var i = 0 ; i < reader . FieldCount ; i ++ )
221- {
222- response . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
223- }
224-
225- result . Add ( ( ExpandoObject ) response ) ;
177+ response . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
226178 }
227- }
228- catch ( Exception )
229- {
230- //ignored
231- }
232- finally
233- {
234- await connection . CloseAsync ( ) ;
179+
180+ result . Add ( ( ExpandoObject ) response ) ;
235181 }
236182
237183 return result ;
@@ -241,28 +187,17 @@ public async Task NonQueryAsync(IDictionary<string, object> parameters)
241187 {
242188 await using var connection = new SqlConnection ( _connectionString ) ;
243189 await connection . OpenAsync ( ) ;
244- try
245- {
246- await using var command = connection . CreateCommand ( ) ;
247-
248- command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
249- command . CommandType = CommandType . StoredProcedure ;
190+ await using var command = connection . CreateCommand ( ) ;
250191
251- foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
252- {
253- command . Parameters . Add ( new SqlParameter ( $ "@{ property . Key } ", property . Value ) ) ;
254- }
192+ command . CommandText = parameters [ SpPropertyName ] . ToString ( ) ;
193+ command . CommandType = CommandType . StoredProcedure ;
255194
256- await command . ExecuteNonQueryAsync ( ) ;
257- }
258- catch ( Exception )
259- {
260- //ignored
261- }
262- finally
195+ foreach ( var property in parameters . Where ( x => ! ExcludedProperties . Contains ( x . Key ) ) )
263196 {
264- await connection . CloseAsync ( ) ;
197+ command . Parameters . Add ( new SqlParameter ( $ "@ { property . Key } " , property . Value ) ) ;
265198 }
199+
200+ await command . ExecuteNonQueryAsync ( ) ;
266201 }
267202 }
268203}
0 commit comments