LinkSqlDB.cs
41.7 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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Weld
{
class LinkSqlDB
{
private string strSQL;
//与数据库的连接
//private SqlConnection myConnection;
private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;
#region //判断数据项目是否存在
/// <summary>
/// 判断数据库中数据是否已经存在
/// </summary>
/// <param name="tbName">表名</param>
/// <param name="strColumn">字段名</param>
/// <param name="strKeyID">字段值</param>
/// <returns></returns>
public int Exists(string tbName,string strColumn,string strKeyID)
{
int intValue;
string strSql = "Select count(1) from " + tbName + " where " + strColumn+" ='"+strKeyID+"'";
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
intValue=int.Parse( dbCmd.ExecuteScalar().ToString());
dbTrans.Commit();
}
catch (Exception t)
{
MessageBox.Show(t.ToString());
dbTrans.Rollback();
return -1;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return intValue;
}
/// <summary>
/// 判断数据库中数据是否已经存在
/// </summary>
/// <param name="tbName">表名</param>
/// <param name="strColumn">字段名</param>
/// <param name="strKeyID">字段值</param>
/// <returns></returns>
public int Exists(string tbName, string strColumn1, string strColumn2, string strValue1,string strValue2)
{
int intValue;
string strSql = "Select count(0) from " + tbName + " where " + strColumn1 + " ='" + strValue1 + "' and '"+ strColumn2+ " ='" +strValue2 +"'";
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
intValue = int.Parse(dbCmd.ExecuteScalar().ToString());
dbTrans.Commit();
}
catch (Exception t)
{
MessageBox.Show(t.ToString());
dbTrans.Rollback();
return -1;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return intValue;
}
/// <summary>
/// 判断数据是否已经存在,主要用于修改数据保存用
/// </summary>
/// <param name="tbName">表名</param>
/// <param name="strColumn">字段名 </param>
/// <param name="strKeyID">字段值 </param>
/// <returns></returns>
public int Exists2(string tbName, string strColumn, string strKeyID)
{
int intValue;
string strSql = "Select count(1) from " + tbName + " where " + strColumn + " ='" + strKeyID + "'";
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
intValue = int.Parse(dbCmd.ExecuteScalar().ToString());
dbTrans.Commit();
if (intValue <= 1)
{ intValue = 0; }
}
catch (Exception t)
{
MessageBox.Show(t.ToString());
dbTrans.Rollback();
return -1;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return intValue;
}
#endregion
#region /////////// 操作脱机数据库(创建了该类的实例时直接用) ////////////////////////////////////////
/// <summary>
/// //根据输入的SQL语句检索数据库数据
/// </summary>
/// <param name="tempStrSQL">查询SQL字符串</param>
/// <param name="tempTableName">填充表格的名称</param>
/// <returns>DataSet对象</returns>
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
try
{
this.strSQL = tempStrSQL;
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
this.da = new SqlDataAdapter(this.strSQL, myConnection);
this.ds.Clear();
this.da.Fill(ds, tempTableName);
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}
/// <summary>
/// //数据库数据更新(传DataSet和DataTable的对象)
/// </summary>
/// <param name="changedDataSet">数据发生变化的DataSet对象</param>
/// <param name="tableName">数据表名</param>
/// <returns>DataSet对象</returns>
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
try
{
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
this.da = new SqlDataAdapter(this.strSQL, myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return changedDataSet;//返回更新了的数据库表
}
#endregion
#region//////////////// 直接操作数据库(未创建该类的实例时直接用) /////////////////////////////////////////////////////
/// <summary>
/// //检索数据库数据(传字符串,直接操作数据库)
/// </summary>
/// <param name="tempStrSQL">SQL查询语句</param>
/// <returns>Datatable对象</returns>
public DataTable SelectDataBase(string tempStrSQL)
{
DataSet tempDataSet = new DataSet();
try
{
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
this.da = new SqlDataAdapter(tempStrSQL, myConnection);
this.da.Fill(tempDataSet);
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return tempDataSet.Tables[0];
}
///// <summary>
///// //检索数据库数据(传字符串,直接操作数据库)
///// </summary>
///// <param name="tempStrSQL">SQL查询语句</param>
///// <returns>Datatable对象</returns>
//public string SelectDataColumn(string tempStrSQL)
//{
// DataSet tempDataSet = new DataSet();
// try
// {
// SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
// this.da = new SqlDataAdapter(tempStrSQL, myConnection);
// this.da.Fill(tempDataSet);
// }
// catch (Exception ex)
// {
// LogExecute.WriteDBExceptionLog(ex);
// }
// return tempDataSet.Tables[0].Rows[0][0].ToString();
//}
/// <summary>
/// //数据库数据更新(传字符串,直接操作数据库)
/// </summary>
/// <param name="tempStrSQL">SQL语句</param>
/// <returns>受影响的行数</returns>
public int UpdateDataBase(string tempStrSQL)
{
int intNumber = 0;
try
{
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, myConnection);
intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return intNumber;
}
#endregion
#region 条件查询操作 表名+Sql语句
/// <summary>
/// 条件查询操作
/// </summary>
/// <param name="strTableName">表名</param>
/// <param name="strSql">Sql语句</param>
/// <returns>条件查询结果</returns>
public DataSet SelectX(string strTableName, string strSql)
{
DataSet ds = new DataSet();
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
SqlDataAdapter dbAdapt = new SqlDataAdapter(strSql, myConnection);
dbAdapt.SelectCommand = dbCmd;
try
{
dbAdapt.Fill(ds, strTableName);
}
catch(Exception ex)
{
ds.Clear();
LogExecute.WriteDBExceptionLog(ex);
}
finally
{
myConnection.Close();
}
return ds;
}
#endregion
#region 单条记录的插入(包括相应记录的时间) 表名+列名组+值组
/// <summary>
/// 单条记录的插入操作+插入时间
/// </summary>
/// <param name="strTableName">表名</param>
/// <param name="strColumn">列名组</param>
/// <param name="strValue">值组</param>
/// <param name="flagTime"></param>
public bool InsertT(string strTableName, string[] strColumn, object[] strValue)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
string strSql = "INSERT INTO " + strTableName + " (";
for (int i = 0; i < strColumn.Length ; i++)
{
strSql += (strColumn[i] + ", ");
}
strSql += ("InputTime) VALUES ('");
for (int i = 0; i < strValue.Length-1 ; i++)
{
strSql += (strValue[i] + "', '");
}
strSql += (strValue[strValue.Length-1]) + "',";
strSql += ( "sysdatetime())");
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
#endregion
}
#endregion
#region 单条记录的插入(包括相应记录的时间) 表名+列名组+值组
/// <summary>
/// 单条记录的插入操作
/// </summary>
public bool Insert(string strInsertSql)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strInsertSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
#endregion
}
#endregion
#region 单条记录的插入 表名+列名组+值组
/// <summary>
/// 单条记录的插入操作
/// </summary>
/// <param name="strTableName">表名</param>
/// <param name="strColumn">列名组</param>
/// <param name="strValue">值组</param>
public bool Insert(string strTableName, string[] strColumn, object[] strValue)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
string strSql = "INSERT INTO " + strTableName + " (";
for (int i = 0; i < strColumn.Length - 1; i++)
{
strSql += (strColumn[i] + ", ");
}
strSql += (strColumn[strColumn.Length - 1] + ") VALUES ('");
for (int i = 0; i < strValue.Length - 1; i++)
{
strSql += (strValue[i] + "', '");
}
strSql += (strValue[strValue.Length - 1] + "')");
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
dbTrans.Rollback();
return false;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
#endregion
}
#endregion
#region 批量记录的插入操作,即可一次向多张表中插入不同的批量记录
/// <summary>
/// 批量记录的插入操作,即可一次向多张表中插入不同的批量记录
/// </summary>
/// <param name="ds">批量记录组成的DataSet,DataSet中的各个DataTable名为表名,各DataTable中的DataColumn名为列名</param>
public bool InsertSet(ref DataSet ds)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
try
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
string strSql = "INSERT INTO " + dt.TableName + " (";
for (int i = 0; i < dt.Columns.Count - 1; i++)
{
strSql += (dt.Columns[i].Caption + ", ");
}
strSql += (dt.Columns[dt.Columns.Count - 1].Caption + ") VALUES ('");
for (int i = 0; i < dt.Columns.Count - 1; i++)
{
strSql += (dr[i] + "', '");
}
strSql += (dr[dt.Columns.Count - 1] + "')");
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
dbCmd.ExecuteNonQuery();
}
}
dbTrans.Commit();
}
catch
{
dbTrans.Rollback();
return false;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
#endregion
}
#endregion
/// <summary>
/// 更新函数
/// </summary>
/// <param name="strTableName"></param>
/// <param name="strColumn"></param>
/// <param name="strValue"></param>
/// <returns></returns>
public bool Update(string strTableName, string[] strColumn, object[] strValue)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
string strSql = "UPDATE " + strTableName + " SET ";
for (int i = 0; i < strColumn.Length - 1; i++)
{
strSql += (strColumn[i] + " = '" + strValue[i] + "', ");
}
strSql += (strColumn[strColumn.Length - 1] + " = '" + strValue[strValue.Length - 1] + "' " );
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
// MessageBox.Show(t.ToString());
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return true;
#endregion
}
/// <summary>
/// 单条更新数据+更新时间记录
/// </summary>
/// <param name="strTableName"></param>
/// <param name="strColumn"></param>
/// <param name="strValue"></param>
/// <param name="strID"></param>
/// <returns></returns>
public bool UpdateT(string strTableName, string[] strColumn, object[] strValue,string strID)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
string strSql = "UPDATE " + strTableName + " SET ";
for (int i = 0; i < strColumn.Length ; i++)
{
strSql += (strColumn[i] + " = '" + strValue[i] + "', ");
}
strSql += "InputTime = sysdatetime() Where C_ID="+strID ;
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
// MessageBox.Show(t.ToString());
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return true;
#endregion
}
/// <summary>
/// 单条更新数据+更新时间记录
/// </summary>
/// <param name="strTableName"></param>
/// <param name="strColumn"></param>
/// <param name="strValue"></param>
/// <returns></returns>
public bool UpdateT(string strTableName, string[] strColumn, object[] strValue)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
string strSql = "UPDATE " + strTableName + " SET ";
for (int i = 0; i < strColumn.Length; i++)
{
strSql += (strColumn[i] + " = '" + strValue[i] + "', ");
}
strSql += "InputTime = sysdatetime() " ;
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
// MessageBox.Show(t.ToString());
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return true;
#endregion
}
/// <summary>
/// 单条更新数据
/// </summary>
/// <returns></returns>
public bool Update(string strSql)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
// MessageBox.Show(t.ToString());
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return true;
#endregion
}
#region 条件更新操作 表名+列名组+值组+条件
/// <summary>
/// 条件更新操作
/// </summary>
/// <param name="strTableName">表名</param>
/// <param name="strColumn">列名组</param>
/// <param name="strValue">值组</param>
/// <param name="strCondition">条件</param>
public bool Update(string strTableName, string[] strColumn, object[] strValue, string strCondition)
{
#region
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
myConnection.Open();
string strSql = "UPDATE " + strTableName + " SET ";
for (int i = 0; i < strColumn.Length - 1; i++)
{
strSql += (strColumn[i] + " = '" + strValue[i] + "', ");
}
strSql += (strColumn[strColumn.Length - 1] + " = '" + strValue[strValue.Length - 1] + "' " + " WHERE " + strCondition);
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.CommandTimeout = 300;
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch (Exception ex)
{
// MessageBox.Show(t.ToString());
dbTrans.Rollback();
LogExecute.WriteDBExceptionLog(ex);
return false;
}
finally
{
dbTrans.Dispose();
dbCmd.Dispose();
myConnection.Close();
}
return true;
#endregion
}
#endregion
#region 无条件删除操作,即删除表中所有记录
/// <summary>
/// 无条件删除操作,即删除表中所有记录
/// </summary>
/// <param name="strTableName">表名</param>
public bool DeleteAll(string strTableName)
{
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
string strSql = "DELETE FROM " + strTableName;
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch
{
dbTrans.Rollback();
return false;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
}
#endregion
#region 条件删除操作
/// <summary>
/// 条件删除操作
/// </summary>
/// <param name="strTableName">表名</param>
/// <param name="strCondition">条件</param>
public bool Delete(string strTableName, string strCondition)
{
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
string strSql = "DELETE FROM " + strTableName + " WHERE " + strCondition;
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch
{
dbTrans.Rollback();
return false;
// throw;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
}
#endregion
#region 条件删除操作
/// <summary>
/// 条件删除操作
/// </summary>
public bool Delete(string strSql)
{
SqlConnection myConnection = new SqlConnection(DBConnection.DBConnstring);
// string strSql = "DELETE FROM " + strTableName + " WHERE " + strCondition;
myConnection.Open();
SqlTransaction dbTrans = myConnection.BeginTransaction();
SqlCommand dbCmd = new SqlCommand(strSql, myConnection);
dbCmd.Transaction = dbTrans;
try
{
dbCmd.ExecuteNonQuery();
dbTrans.Commit();
}
catch
{
dbTrans.Rollback();
return false;
// throw;
}
finally
{
dbTrans.Dispose();
myConnection.Close();
}
return true;
}
#endregion
#region 执行SQL语句返回SqlDataReader
///<summary>
///执行SQL语句返回SqlDataReader
///</summary>
///<param name="sql">要执行的SQL语句</param>
///<returns>返回SqlDataReader,需要手动关闭连接!</returns>
///
public SqlDataReader datareader(string sql)
{
//SqlConnection myCn = Login.Cn();
SqlConnection myCn = DBConnection.DBConnectionOpen();
SqlCommand cmd = new SqlCommand(sql, myCn);
SqlDataReader dr = null;
try
{
dr = cmd.ExecuteReader();
}
catch (SqlException oe)
{
if (dr != null) dr.Close();
myCn.Dispose();
cmd.Dispose();
throw new Exception(oe.ToString());
}
finally
{
// myCn.Close();
}
return dr;
}
#endregion
/// <summary>
/// 获取表格所有数据
/// </summary>
/// <param name="tbName"></param>
/// <returns></returns>
public DataTable GetALL(string tbName)
{
try
{
string SQL_Str = " select * from "+tbName;
DataSet ds = SqlHelper.ExecuteDataset(DBConnection.DBConnstring, CommandType.Text, SQL_Str);
if (ds != null && ds.Tables.Count > 0)
{
return ds.Tables[0];
}
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return null;
}
/// <summary>
/// 获取表格所有数据
/// </summary>
/// <param name="tbName"></param>
/// <returns></returns>
public DataTable GetALL2(string strSql)
{
try
{
DataSet ds = SqlHelper.ExecuteDataset(DBConnection.DBConnstring, CommandType.Text, strSql);
if (ds != null && ds.Tables.Count > 0)
{
return ds.Tables[0];
}
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return null;
}
/// <summary>
/// 获取表格所有数据,并分页显示
/// </summary>
/// <param name="tbName"></param>
/// <returns></returns>
public DataTable GetALLDataPageDisplay(string TableName,string TIndex,string TColumn,string strWhere , int pageIndex, int pageSize,string strOrderby,out int RecordCount )
{
RecordCount = 0;
using (SqlConnection connection = new SqlConnection(DBConnection.DBConnstring))
{
SqlCommand command = new SqlCommand("procPageDisplay", connection);
command.CommandType = CommandType.StoredProcedure;//采用存储过程
//存储过程参数
command.Parameters.Add("@Table", SqlDbType.NVarChar, 1000).Value = TableName;
command.Parameters.Add("@TIndex", SqlDbType.NVarChar, 100).Value = TIndex;
command.Parameters.Add("@Column", SqlDbType.NVarChar, 2000).Value = TColumn;
command.Parameters.Add("@Sql", SqlDbType.NVarChar, 3000).Value = strWhere;
command.Parameters.Add("@PageIndex", SqlDbType.Int, 8).Value = pageIndex.ToString();
command.Parameters.Add("@PageSize", SqlDbType.Int, 8).Value = pageSize.ToString();
command.Parameters.Add("@Sort", SqlDbType.NVarChar, 200).Value = strOrderby;
//打开连接
if (connection.State != ConnectionState.Open) { connection.Open(); }
try
{
//填充数据
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
//获取总记录数
RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
//返回数据集
return ds.Tables[0];
}
catch (SqlException err)
{
MessageBox.Show(err.Message);
return null; ;
}
finally
{
connection.Close();
}
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="tbName"></param>
/// <returns></returns>
public int execProcedureEquiment(string ProcName, string stationCode, string EquipmentCode, string EquipmentName, string EquipmentType, string EquipmentCycleTime, string EquipmentMaintainTime)
{
using (SqlConnection connection = new SqlConnection(DBConnection.DBConnstring))
{
SqlCommand command = new SqlCommand(ProcName, connection);
command.CommandType = CommandType.StoredProcedure;//采用存储过程
//存储过程参数
command.Parameters.Add("@stationCode", SqlDbType.NVarChar, 50).Value = stationCode;
command.Parameters.Add("@equipmentCode", SqlDbType.NVarChar, 50).Value = EquipmentCode;
command.Parameters.Add("@equipmentName", SqlDbType.NVarChar, 50).Value = EquipmentName;
command.Parameters.Add("@equipmentType", SqlDbType.NVarChar, 50).Value = EquipmentType;
command.Parameters.Add("@equipmentCycleTime", SqlDbType.NVarChar, 50).Value = EquipmentCycleTime;
command.Parameters.Add("@equipmentMaintainTime", SqlDbType.NVarChar, 50).Value = EquipmentMaintainTime;
//打开连接
if (connection.State != ConnectionState.Open) { connection.Open(); }
try
{
//填充数据
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
//获取总记录数
//返回数据集
return da.Fill(ds); ;
}
catch (SqlException err)
{
MessageBox.Show(err.Message);
return -1 ;
}
finally
{
connection.Close();
}
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="tbName"></param>
/// <returns></returns>
public int execProcedureParameter(string ProcName, int equipmentID, string parameterCode, string parameterName)
{
using (SqlConnection connection = new SqlConnection(DBConnection.DBConnstring))
{
SqlCommand command = new SqlCommand(ProcName, connection);
command.CommandType = CommandType.StoredProcedure;//采用存储过程
//存储过程参数
command.Parameters.Add("@equipmentInfoID", SqlDbType.Int).Value = equipmentID;
command.Parameters.Add("@parameterCode", SqlDbType.NVarChar, 50).Value = parameterCode;
command.Parameters.Add("@parameterName", SqlDbType.NVarChar, 50).Value = parameterName;
//打开连接
if (connection.State != ConnectionState.Open) { connection.Open(); }
try
{
//填充数据
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
//获取总记录数
//返回数据集
return da.Fill(ds); ;
}
catch (SqlException err)
{
MessageBox.Show(err.Message);
return -1;
}
finally
{
connection.Close();
}
}
}
/// <summary>
/// 获取一行数据
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public DataRow GetOne(string tbName,string C_ID)
{
try
{
string SQL_Str = " select * from "+tbName+" where C_ID="+C_ID;
DataSet ds = SqlHelper.ExecuteDataset(DBConnection.DBConnstring, CommandType.Text, SQL_Str);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0].Rows[0];
}
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
}
return null;
}
#region 驗證輸入的字符串
/// <summary>
/// 判斷輸入的字符類型
/// </summary>
/// <param name="_value">輸入的字串</param>
/// <param name="_kind">要驗證的類型</param>
/// 1: 由26個英文字母組成的字串
/// 2: 正整數
/// 3: 非負整數(正整數 + 0)
/// 4: 非正整數(負整數 + 0)
/// 5: 負整數
/// 6: 整數
/// 7: 非負浮點數(正浮點數 + 0)
/// 8: 正浮點數
/// 9: 非正浮點數(負浮點數 + 0)
/// 10: 負浮點數
/// 11: 浮點數
/// 12: 由26個英文字母的大寫組成的字串
/// 13: 由26個英文字母的小寫組成的字串
/// 14: 由數位和26個英文字母組成的字串
/// 15: 由數位、26個英文字母或者下劃線組成的字串
/// 16: Email
/// 17: URL
/// 18: 只能輸入入中文
/// 19:
/// <returns>true是驗証通過,false是驗証錯誤</returns>
public bool ValidateUserInput(String _value, int _kind)
{
string RegularExpressions = null;
switch (_kind)
{
case 1:
//由26個英文字母組成的字串
RegularExpressions = "^[A-Za-z]+$";
break;
case 2:
//正整數
RegularExpressions = "^[0-9]*[1-9][0-9]*$";
break;
case 3:
//非負整數(正整數 + 0)
RegularExpressions = "^\\d+$";
break;
case 4:
//非正整數(負整數 + 0)
RegularExpressions = "^((-\\d+)|(0+))$";
break;
case 5:
//負整數
RegularExpressions = "^-[0-9]*[1-9][0-9]*$";
break;
case 6:
//整數
RegularExpressions = "^-?\\d+$";
break;
case 7:
//非負浮點數(正浮點數 + 0)
RegularExpressions = "^\\d+(\\.\\d+)?$";
break;
case 8:
//正浮點數
RegularExpressions = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
break;
case 9:
//非正浮點數(負浮點數 + 0)
RegularExpressions = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";
break;
case 10:
//負浮點數
RegularExpressions = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";
break;
case 11:
//浮點數
RegularExpressions = "^(-?\\d+)(\\.\\d+)?$";
break;
case 12:
//由26個英文字母的大寫組成的字串
RegularExpressions = "^[A-Z]+$";
break;
case 13:
//由26個英文字母的小寫組成的字串
RegularExpressions = "^[a-z]+$";
break;
case 14:
//由數位和26個英文字母組成的字串
RegularExpressions = "^[A-Za-z0-9]+$";
break;
case 15:
//由數位、26個英文字母或者下劃線組成的字串
RegularExpressions = "^[0-9a-zA-Z_]+$";
break;
case 16:
//email地址
RegularExpressions = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
break;
case 17:
//url
RegularExpressions = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
break;
case 18:
//只能輸入中文
RegularExpressions = "^[^\u4E00-\u9FA5]";
break;
case 19:
//只能输入0和非0打头的数字
RegularExpressions = "^(0|[1-9][0-9]*)$";
break;
case 20:
//只能输入数字
RegularExpressions = "^[0-9]*$";
break;
case 21:
//只能輸入數字加2位小數
RegularExpressions = "^[0-9]+(.[0-9]{1,2})?$";
break;
case 22:
//只能输入0和非0打头的数字加2位小數
RegularExpressions = "^(0|[1-9]+)(.[0-9]{1,2})?$";
break;
case 23:
//只能输入0和非0打头的数字加2位小數 但不匹配0.00
RegularExpressions = "^(0(.(0[1-9]|[1-9][0-9]))?|[1-9]+(.[0-9]{1,2})?)$";
break;
default:
break;
}
Match m = Regex.Match(_value, RegularExpressions);
if (m.Success)
return true;
else
return false;
}
#endregion
}
}