我正在写的系统要多个for语句中包括从数据库中取数判断的操作,我个人觉得这样写法不太合适,但不晓得如何优化,请哪位大侠指正,谢谢!public string Relation_Check_Prohibit()
        {
            int totalcode;
            string strcode, strtype;
            string sql,result;           
                    
            totalcode = alcode.Count;
            result = "not";
            sh.Open();
            for (int i = 0; i < totalcode - 1; i++)
                {
                    strcode = alcode[i].ToString();
                    strtype = altype[i].ToString();
                
                    for (int j = i + 1; j < totalcode; j++)
                        {                            if (strcode == alcode[j].ToString()) //相同药品对比,则继续下一个药品
                            {
                                continue; 
                            }
                            /*
                             * no_no=1 二药品类别配伍;
                             * no_no=2 code_a为药品代码 code_b为药品类别;
                             * no_no=3 code_a为药品代码 code_b为药品代码;
                             */
                            sql = "select *  from um_relation where  ";
                            sql += "(no_no ='1'  and code_a ='" + strtype + "' and code_b='"  + altype[j].ToString() +  " ')  ";
                            sql +=  " or ( no_no = '2' and code_a='" + strcode + "' and code_b='" + altype[j].ToString() + "') ";
                            sql +=  " or ( no_no = '2' and code_b='" + alcode[j].ToString() + "' and code_b='" + strtype + "') ";
                            sql +=  "";
                          try
                            {
                                SqlDataReader reader = sh.ExecuteReaderQuery(sql);
                                
                                    if (reader.Read())
                                    {   result = strcode + "|" + alcode[j].ToString() + "两味药配伍禁忌!";
                                        return result;
                                    }
                                    else
                                    {
                                        reader.Dispose();
                                        sql = "select *  from um_relation where  ";
                                        sql += "(no_no ='3'  and code_a ='" + strcode + "' and code_b='"  + alcode[j].ToString() +  " ')  ";
                                        SqlDataReader readcode = sh.ExecuteReaderQuery(sql);
                                        if (readcode.Read())
                                        {                                          
                                           result = strcode + "|" + alcode[j].ToString() + "两味药配伍禁忌!";
                                           return result;
                                        }
                                        readcode.Dispose();
                                      }
                                 
                              }
                              catch (Exception ex)
                              {
                                  throw ex;
                              }                         
                           
                        }
                }                sh.Close();
                return result;
          }
       

解决方案 »

  1.   

    这段代码中不断的在create SqlDataReader
    有什么办法,只一次创建,重复使用呢?
      

  2.   

     建议表结构:
    药品ID      药品名称        禁忌ID
    001         AAA           002#003#
    002         BBB           001#
    003         CCC           001#
    004         DDD           #比如已经有了002 添加001的话
    select * from 表 where ID='002' and 禁忌ID not like '%001%'//and 禁忌ID not like '%001%' 这句循环,有多少个已经选的就加多少个。
    都通过查询有结果,那就不会有药理冲突
      

  3.   

    1. 循环不是瓶颈所在,数据库才是IO的瓶颈。
    2. 用存储过程,优于自己写Code
    3. Linq很不错!