我正在写的系统要多个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;
}
{
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;
}
解决方案 »
- mssql2005+C#.net毕业设计,自助餐饮系统的预订问题
- 请教大家如何用C#连接到页面读取数据
- 求救,关于自动更新的问题
- winform 怎么实现datagridview下拉框联动功能,也就是我改变其中一个下拉框的值时另一个也跟着改变,谢谢!
- winform 打印 问题 急 100分 求助
- 求救,如何用c#写代码保存下来
- DataGrid更新时取不到页面上TextBox的值,如何解决?
- in_addr inAddr;inAddr.S_un.S_addr = ntohl(this->m_ipv4)
- 最新消息:微软向各国政府和各大公司公开操作系统源代码!
- 文件夹权限设置无效
- C# 中 需要将一个窗口初始化时放在屏幕的最右边
- 关于webBrowser
有什么办法,只一次创建,重复使用呢?
药品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%' 这句循环,有多少个已经选的就加多少个。
都通过查询有结果,那就不会有药理冲突
2. 用存储过程,优于自己写Code
3. Linq很不错!