写了一段可读性超差的代码,望高手不吝赐教!~
写了一段可读性超差的代码,望高手不吝赐教!~
需求如下:
1、从A表中取ID,对比B表Code,若B表中缺少对应A表ID的Code,则将A表ID作为B表Code添加到B表;
2、将B表插入完全后,再用A表ID与B表Code对比,取出A表中(A)ID==(B)Code的项,连同整行插入到C表中;
我写的代码如下:
1conn.Open();
2 bool x = true;
3 if (A.Tables[0].Rows.Count > 0)
4 {
5 for (int i = 0; i < A.Tables[0].Rows.Count; i++)
6 {
7 DataRow dr = A.Tables[0].Rows[i];
8
9 //此处循环想达到目的:若(A)ID==(B)Code,直接跳过下面if语句,故设x = false;
10 //若(A)ID !=(B)Code,继续循环,循环结束仍然不等,跳出小循环继续向下执行,故设x = true;
11
12 for (int j = 0; j < B.Tables[0].Rows.Count; j++)
13 {
14 if (A.Tables[0].Rows[i]["ID"].ToString() == B.Tables[0].Rows[j]["Code"].ToString())
15 {
16 x = false;
17 break;
18 }
19
20 else
21 x = true;
22 continue;
23 }
24
25 //若小循环结束仍不相等,则将A表ID作为B表Code添加到B表
26 if (x == true )
27 {
28 Code = A.Tables[0].Rows[i]["ID"].ToString();
29 InsertBCommand.CommandText = string.Format(insertSql);
30 InsertBCommand.ExecuteNonQuery();
31 }
32
33 //与上一个for循环判断完全相同,此处表示若(A)ID==(B)Code,将其A中整项插入C
34
35 for (int k = 0; k < B.Tables[0].Rows.Count; k++)
36 {
37 int ID = 0;
38 if (A.Tables[0].Rows[i]["ID"].ToString() == B.Tables[0].Rows[k]["Code"].ToString())
39 {
40 IndicatorID = int.Parse(B.Tables[0].Rows[k]["ID"].ToString());
41 SelectCCommand.CommandText = string.Format(selectSqlMonthBase);
42 InsertCCommand.CommandText = string.Format(insertSql);
43 InsertCCommand.ExecuteNonQuery();
44 break;
45 }
46
47 else continue;
48 }
49 }
50 }
51
52 conn.Close();
2 bool x = true;
3 if (A.Tables[0].Rows.Count > 0)
4 {
5 for (int i = 0; i < A.Tables[0].Rows.Count; i++)
6 {
7 DataRow dr = A.Tables[0].Rows[i];
8
9 //此处循环想达到目的:若(A)ID==(B)Code,直接跳过下面if语句,故设x = false;
10 //若(A)ID !=(B)Code,继续循环,循环结束仍然不等,跳出小循环继续向下执行,故设x = true;
11
12 for (int j = 0; j < B.Tables[0].Rows.Count; j++)
13 {
14 if (A.Tables[0].Rows[i]["ID"].ToString() == B.Tables[0].Rows[j]["Code"].ToString())
15 {
16 x = false;
17 break;
18 }
19
20 else
21 x = true;
22 continue;
23 }
24
25 //若小循环结束仍不相等,则将A表ID作为B表Code添加到B表
26 if (x == true )
27 {
28 Code = A.Tables[0].Rows[i]["ID"].ToString();
29 InsertBCommand.CommandText = string.Format(insertSql);
30 InsertBCommand.ExecuteNonQuery();
31 }
32
33 //与上一个for循环判断完全相同,此处表示若(A)ID==(B)Code,将其A中整项插入C
34
35 for (int k = 0; k < B.Tables[0].Rows.Count; k++)
36 {
37 int ID = 0;
38 if (A.Tables[0].Rows[i]["ID"].ToString() == B.Tables[0].Rows[k]["Code"].ToString())
39 {
40 IndicatorID = int.Parse(B.Tables[0].Rows[k]["ID"].ToString());
41 SelectCCommand.CommandText = string.Format(selectSqlMonthBase);
42 InsertCCommand.CommandText = string.Format(insertSql);
43 InsertCCommand.ExecuteNonQuery();
44 break;
45 }
46
47 else continue;
48 }
49 }
50 }
51
52 conn.Close();