【发布时间】:2013-03-23 03:09:52
【问题描述】:
我对 c# 有点陌生,我在试图围绕我试图实现的这个 if-then 语句时遇到了一些麻烦。
这是我的目标:当用户在系统中输入订单时,会为每个订单创建一个唯一的 orderID。但是,一些额外的功能是用户可以选择项目的数量,然后使用相同的 orderID 为每个项目创建一个新行。每个项目都有一个唯一的密钥。这给了你这个:
ORDERID KEY ItemCode
ORD123 758 2
ORD123 584 2
ORD123 582 2
我遇到的问题是 ORDER ID 必须是唯一的除非订单中有多个商品(如上所示为每个订单创建多行)。
什么适用于我当前的代码: 单品订单 ID,与其他单品订单 ID 核对
Existing Order : ORD111
Attempt to Enter new order of : ORD111
**Sorry OrderID already exists**
单个项目的订单 ID,与多个项目的订单 ID 进行检查,
Existing Order : ORD222
ORD222
ORD222
Attempt to enter new order of : ORD222
**Sorry OrderID already exists**
但是,当我输入多个商品的订单 ID 时,
Existing Order : ORD333
ORD333
ORD333
Attempt to enter new order of ORD333
ORD333
ORD333
**Order Successfully Entered**
这是我检查订单 ID 的代码
private bool isOrderIDUnique()
{
string OrderID = txtOrderID.Text;
string ItemAmount = txtItemAmount.Text;
int items = Convert.ToInt32(ItemAmount);
string select = getSelectString("COUNT(*)", strings.settings.AccessTable, "ORDERID = '" + OrderID + "'");
int count = CountOrderID(select);
if (count == 0)
{
return true;
}
if (count >= 1 && items > 1)
{
return true;
}
else
{
return false;
}
如果有人能帮我解决这个问题,我将不胜感激!我的 IF 声明中遗漏了哪一部分?如何根据组检查组?谢谢!!
【问题讨论】:
-
您可以将您的程序简化为:return (count==0 || items>1);因此,如果 items>1,您将返回 true。这就是你想要的吗?
-
从顶部开始,您应该使用多个表,而不是为多行放置相同的 orderid...
-
我用这个方法在输入订单前检查ORDER ID。
-
Ryan,orderID 不是每一行的主键。
-
kainaw,项目数决定了行数。如果 items > 1,程序不会验证唯一的订单 ID。这正是我遇到的问题。
标签: c# if-statement