-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sql_DataAccesesLayer-DALCS.cs
614 lines (507 loc) · 22.5 KB
/
Sql_DataAccesesLayer-DALCS.cs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DALKullanimi
{
public class DAL
{
public DAL()//BU CTOR'U OLUŞTURMAMIZIN SEBEBİ DAL class'ını başka bir yerde normal bir nesne gibi oluşturmak istediğimde sorunçıkmaması için.
{}
//DAL orndal=new DAL(); şeklinde oluşturabilmem için eğer bu ctor'u yapmasaydık sadece alttaki ctor olsaydı ben parametre girmek zorunda kalacaktım.
public DAL(string gelenBaglantiAdresi)
{
karsilayanBaglanti_Adresi = gelenBaglantiAdresi;
}//Oluşturdugum nesneme mecburi olarak bağlantı adresi parametresi gir diyorum
public string karsilayanBaglanti_Adresi { get; set; }//Bu prop sayesinde gelen bağlantıyı tutuyorum.
SqlConnection conn;
public enum BaglantiIslem//Bu enum sayesinde bağlantının açıklık kapalılık durumunu kontrol etmem soncunda işlemler yapabiliyorum.
{
BaglantiyiAc,
BaglantıyıKapat
}
public delegate void HATA(string HataMesaji);//Ana bir delegate tipi oluşturdum buna bağlı alt hata çeşitleri oluşturabilirim artık bu tipi kullanan.
public event HATA BaglantiHatasi;
public event HATA SorguHatasi;
public bool BaglantiDurum(BaglantiIslem islem)//Bu fonksiyonum(--->>>method değil method geriye değer döndürmez---<<<) sayesinde bağlantının durumunu kontrol edip ona göre açıp kapatacam
{
bool Durum = true;//buna default bir value vermekten ziyade aşağıda bir başarısızlık durumunda false yaptım ya eğer başarısızlık olmaz ise bağlantı open döndürür gibi düşün.
try
{
conn = new SqlConnection(karsilayanBaglanti_Adresi);//connection'ımla ilgili bir işlem yapabilmem için connection'ımın tanımlı olması lazım tanımlıyorum ki aşağıda onunla ilgili işlemler yapabiliyim.
switch (islem)
{
case BaglantiIslem.BaglantiyiAc:
if (conn.State==System.Data.ConnectionState.Closed)//bağlantı kapalı mı kontrol edip kapalıysa açıyorum.
{
conn.Open();
}
break;
case BaglantiIslem.BaglantıyıKapat:
if (conn.State == System.Data.ConnectionState.Open)//bağlantı açık mı kontrol edip açıksa kapatıyorum.
{
conn.Close();
}
break;
}
}
catch(Exception ex)//bağlantıyla ilgili bir hata olması durumda çalışacak örneğin bağlantı adresi yanlış ya da sql server erişime kapatıldı authentication iznin kesildi vs.
{
try { BaglantiHatasi(ex.Message.ToString()); }//event'ım çalışıyor
catch { }
Durum = false;
}
return Durum;
}
public SqlDataReader GetDataReader(string Sorgu,bool IsProcedure)
{
SqlDataReader dr = null;
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))//Bağlantım açık ise eğer ve aynı zamanda SqlDataReader tipindeki GetDateReader fonksiyonuma gerekli sorguyu atamış isem bana gerekli işlemi yapacak.
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)//Bu kısım procedure ile işlem yapılma olayını kontrol ediyor eğer procedure true ise ben sorgu kısmına procedure girmeliyim ve girdiğimde procedure kullandığıma yönelik devam edicek işlemlerim.
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
dr = cmd.ExecuteReader();//komutumdan gelen bilgi akışını üzerinde tutuyor
}
}
catch (Exception ex)//Sorgumda sıkıntı var ise bu çalışıcak.
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally //Ben her durumda bağlantımı kapatmak zorundayım , eğer 86 daki scope dan hemen sonra kapatsam olmaz çünkü diyelim ki sorguyu yanlış yazdın bi sıkıntı oldu bağlantı kapantığı için
//catch'e düşüp hatayı gösteremez,,
//catch'de kapatsam olmaz çünkü diyelim ki sorguyu falan doğru yazdım bu sefer catch'e düşmez ise bağlantım açık kalmış olur.
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
return dr;//85 te tuttuğum bilgi akışını döndürüyorum.
}//Procedure kullanmıcaksam false yazar sorgumu girerek DataReader'ımla gerçekleştireceklerimi gerçekleştiririm.
public SqlDataReader GetDataReader(string Sorgu,bool IsProcedure, params SqlParameter[] parametreler)
{
SqlDataReader dr = null;
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
//Diğer yorumlar aynı fark buradaki.
if (parametreler.Length>0)//bu koşul sağlanıyorsa parametre var demektik ben bu parametleri tek tek ekliyorum cmd'imin parametresine.
{
foreach (SqlParameter item in parametreler)
{
cmd.Parameters.Add(item);
}
}
dr = cmd.ExecuteReader();
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
return dr;
}//Parametreli olma durumu vardır.
public DataTable GetDataTable(string Sorgu, bool IsProcedure)
{
DataTable dt = new DataTable();
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
SqlDataAdapter da = new SqlDataAdapter(cmd);//DataTable'gibi bir nesneyi üzerine verdiğimiz komut sayesinde doldurabilen veri akışını sağlayan bir yapıdır.
//Verdiğim komutlar ve parametreler sayesindede bunu sağlıyor.
da.Fill(dt);
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);//Son olarak bağlantıyı kapatıyor.
}
return dt;//Ve direk DataTable' bilgisi olarak döndürüyor bana işi.
}
public DataTable GetDataTable(string Sorgu, bool IsProcedure, params SqlParameter[] parametreler)//Yukarıdaki datatable'ımın sadece parametreli modudur bu da.
{
DataTable dt = new DataTable();
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
if (parametreler.Length > 0)
{
foreach (SqlParameter item in parametreler)
{
cmd.Parameters.Add(item);
}
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
return dt;
}
public DataSet GetDataSet(string Sorgu, bool IsProcedure)
{
DataSet ds = new DataSet();
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
return ds;
}
public DataSet GetDataSet (string Sorgu, bool IsProcedure, params SqlParameter[] parametreler)
{
DataSet ds = new DataSet();
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
if (parametreler.Length > 0)
{
foreach (SqlParameter item in parametreler)
{
cmd.Parameters.Add(item);
}
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
return ds;
}
public string ReturnSQL(string TabloAdi, params string[] Kolonlar)
{
string Rtn = "SELECT ";
foreach (string item in Kolonlar)//Mesele Kolonlar params'ına sadece * koyar geçersem Rtn adlı sorgum SELECT * FROM + TABLOADİ olur.
{
Rtn += item + ",";//SORGUNUN SONUNDA HER HALUKARDA ',' kalır bu yüzden onu gideriyorum.line 301 'de
}
Rtn = Rtn.TrimEnd(',');//
Rtn += " FROM " + TabloAdi;
return Rtn;
}
public string ReturnSQL(string TabloAdi, List<TBLFilitre> filitre, params string[] Kolonlar)//Bir üstteki tablomun filtreleme yapabildiğim modu.
{
string Rtn = "SELECT ";
foreach (string item in Kolonlar)
{
Rtn += item + ",";
}
Rtn = Rtn.TrimEnd(',');
Rtn += " FROM " + TabloAdi;
if (filitre.Count>0)//filtre listemde eleman varsa yani filtre girişi yapmış isem burası çalışıyor ve filtrelerim ekleniyor.
{
Rtn += " WHERE ";
foreach (TBLFilitre item in filitre)//filte olarak gönderdiğimiz List parametre içinden tek tek TBLFilitre class'ı tipinde item dönüyoruz
{
Rtn += item.RtnSTR();//ve bunu Rtn sorgumuza ekliyoruz.
}
Rtn = Rtn.Remove(Rtn.Length - 4, 4);//Sorgumun sonunda AND veya OR her türlü bir tane fazladan kalıyor bunu silmek için böyle bir şey yapıyorum.
}
return Rtn;
}
public void INSERT(string TabloAdi, List<KolonToValue> veriler)//Herhangi bir değer geriye döndürüp onu kullanmaksızın INSERT olayımı direk içine parametre vererek çağırıyorum.
{
string SQL = "INSERT INTO " + TabloAdi + " (";
foreach (KolonToValue item in veriler)
{
SQL += item.KolonAdi + ",";
}
SQL = SQL.TrimEnd(',');
SQL += ") VALUES (";
foreach (KolonToValue item in veriler)
{
SQL += "'" + item.ValueDegeri + "',";//SQL İNJECTİON AÇIĞINI ÇİFT TIRNAK ARASINA ALMAMIZ KAPATIYOR.HER NE KADAR PARAMETER KULLANMAK DAHA SAĞLIKLI OLSADA BU DA DOĞRU BİR KULLANIM
}
SQL = SQL.TrimEnd(',');
SQL += ")";
KodCalistir(SQL, false);//ATLAMA BURAYI-->> Ve kod çalıştır methodum sayesinde insert işlemi gerçekleşiyor.(((connection command gibi değeleri devreye sokuyor KodÇalıştir methodum.)))
}
public void UPDATE(string TabloAdi, List<KolonToValue> veriler, List<TBLFilitre> filitre)//Herhangi bir değer geriye döndürüp onu kullanmaksızın UPDATE olayımı direk içine parametre vererek çağırıyorum.
{
string SQL = "UPDATE " + TabloAdi + " SET ";
foreach (KolonToValue item in veriler)
{
SQL += item.KolonAdi + "='" + item.ValueDegeri + "',";//SQL İNJECTİON AÇIĞINI ÇİFT TIRNAK ARASINA ALMAMIZ KAPATIYOR.HER NE KADAR PARAMETER KULLANMAK DAHA SAĞLIKLI OLSADA BU DA DOĞRU BİR KULLANIM
}
SQL = SQL.TrimEnd(',');
if (filitre.Count > 0)
{
SQL += " WHERE ";
foreach (TBLFilitre item in filitre)
{
SQL += item.RtnSTR();//UPDATE METHODUMA gönderdiğim filtre listesi üzerinde dönerken bu item üzerinden RtnSTR methodu sayesinde filtre şartlarını ekliyorum main sorgum olan SQL string'ine.
}
SQL = SQL.Remove(SQL.Length - 4, 4);
}
KodCalistir(SQL, false);
}
public void DELETE(string TabloAdi, List<TBLFilitre> filitre)//Herhangi bir değer geriye döndürüp onu kullanmaksızın DELETE olayımı direk içine parametre vererek çağırıyorum.
{
string SQL = "DELETE " + TabloAdi;
if (filitre.Count > 0)
{
SQL += " WHERE ";
foreach (TBLFilitre item in filitre)
{
SQL += item.RtnSTR();//UPDATE METHODUMA gönderdiğim filtre listesi üzerinde dönerken bu item üzerinden RtnSTR methodu sayesinde filtre şartlarını ekliyorum main sorgum olan SQL string'ine.
}
SQL = SQL.Remove(SQL.Length - 4, 4);
}
KodCalistir(SQL, false);
}
public void KodCalistir(string Sorgu, bool IsProcedure)//İçine gönderdiğimiz sorguyu komut olarak çalıştırıyor.Bu methodu çağırdıktan sonra içindekiler çalışmış oluyor.Altına devam scope'dan sonra edebilirsin.
//Connection,command gibi değeleri devreye sokuyor bağlantı veya sorguda hata varsa eventları çalıştırıyor kodlarımı çalıştırmış oluyor.
{
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
}
public void KodCalistir(string Sorgu, bool IsProcedure, params SqlParameter[] parametreler)//İçine gönderdiğimiz sorguyu komut olarak çalıştırıyor.Bu methodu çağırdıktan sonra içindekiler çalışmış oluyor.Altına devam scope'dan sonra edebilirsin.
//Connection,command gibi değeleri devreye sokuyor bağlantı veya sorguda hata varsa eventları çalıştırıyor kodlarımı çalıştırmış oluyor.
{
try
{
if (BaglantiDurum(BaglantiIslem.BaglantiyiAc))
{
SqlCommand cmd = new SqlCommand(Sorgu, conn);
if (IsProcedure)
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
}
if (parametreler.Length > 0)
{
foreach (SqlParameter item in parametreler)
{
cmd.Parameters.Add(item);
}
}
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
try { SorguHatasi(ex.Message.ToString()); }
catch { }
}
finally
{
BaglantiDurum(BaglantiIslem.BaglantıyıKapat);
}
}
}
public class TBLFilitre
{
public enum SartTipi
{
Eşittir,
EşitDeğildir,
Küçüktür,
Büyüktür,
KüçükEşittir,
BüyükEşittir,
Başında,
Sonunda,
İçeriğinde
}
public enum AND_OR
{
AND,
OR
}
public string _KolonAdi { get; set; }
public SartTipi _Sartdegeri { get; set; }
public string _ValueDegeri { get; set; }
public AND_OR _AndOR { get; set; }
public TBLFilitre(string KolonAdi,SartTipi SartDegeri,string ValueDegeri,AND_OR ANDOR)
{
_KolonAdi = KolonAdi;
_Sartdegeri = SartDegeri;
_ValueDegeri = ValueDegeri;
_AndOR = ANDOR;
}
public string RtnSTR()
{
string Rtn = _KolonAdi;
switch (_Sartdegeri)
{
case SartTipi.Eşittir:
Rtn += " = '" + _ValueDegeri + "'";
break;
case SartTipi.EşitDeğildir:
Rtn += " <> '" + _ValueDegeri + "'";
break;
case SartTipi.Küçüktür:
Rtn += " < '" + _ValueDegeri + "'";
break;
case SartTipi.Büyüktür:
Rtn += " > '" + _ValueDegeri + "'";
break;
case SartTipi.KüçükEşittir:
Rtn += " <= '" + _ValueDegeri + "'";
break;
case SartTipi.BüyükEşittir:
Rtn += " >= '" + _ValueDegeri + "'";
break;
case SartTipi.Başında:
Rtn += " Like '" + _ValueDegeri + "%'";
break;
case SartTipi.Sonunda:
Rtn += " Like '%" + _ValueDegeri + "'";
break;
case SartTipi.İçeriğinde:
Rtn += " Like '%" + _ValueDegeri + "%'";
break;
}
switch (_AndOR)
{
case AND_OR.AND:
Rtn += " AND ";
break;
case AND_OR.OR:
Rtn += " OR ";
break;
}
return Rtn;
}
}
public class JOINLER
{
public enum JOINTipleri
{
LEFT,
RIGHT,
INNER,
CROSS
}
//JOINLER tipinde bir List<> oluşturdugumda bu değeri giricem ve bu parametreleri kullanıcam.
public JOINTipleri _JOINTip { get; set; }
public string _AnaTablo { get; set; }
public string _AnaKolon { get; set; }
public string _EklenecekTablo { get; set; }
public string _EklenecekKolon { get; set; }
public JOINLER(JOINTipleri JOINTip, string AnaTablo, string AnaKolon, string EklenecekTablo, string EklenecekKolon)
{
_JOINTip = JOINTip;
_AnaKolon = AnaKolon;
_AnaTablo = AnaTablo;
_EklenecekTablo = EklenecekTablo;
_EklenecekKolon = EklenecekKolon;
}
public string RtnSTR()//Bu fonksiyonumu çağırdıgımda bana aldığı JOINTip'ler doğrultusunda sorgumu oluşturucak.
{
string Rtn = "";
switch (_JOINTip)
{
case JOINTipleri.LEFT:
Rtn = " LEFT JOIN " + _EklenecekTablo + " ON " + _EklenecekTablo + "." + _EklenecekKolon + "=" + _AnaTablo + "." + _AnaKolon;
break;
case JOINTipleri.RIGHT:
Rtn = " RIGHT JOIN " + _EklenecekTablo + " ON " + _EklenecekTablo + "." + _EklenecekKolon + "=" + _AnaTablo + "." + _AnaKolon;
break;
case JOINTipleri.INNER:
Rtn = " INNER JOIN " + _EklenecekTablo + " ON " + _EklenecekTablo + "." + _EklenecekKolon + "=" + _AnaTablo + "." + _AnaKolon;
break;
case JOINTipleri.CROSS:
Rtn = " CROSS JOIN " + _EklenecekTablo;
break;
}
return Rtn;
}
}
public class KolonToValue//INSERT AND UPDATE KOMUTLARIMDA ,INSERT VE UPDATE KOMUTLARIM VERI GIRISLERI ISTEDIGI ICIN BU SEKILDE KOLON ADINA GORE VERILERIMI BU CLASS'IM UZZERINDEN AKTARIYORUM BIR LIST OLUSTURUP INSERT VE UPDATE ICINDE FOREACH ILE DONEREK CEKIP VERIYORUM INSERT UPDATE METHODLARIMIN ICINDE
{
public string KolonAdi { get; set; }
public string ValueDegeri { get; set; }
}
}