我是C#的新手,所以也许我没有正确地做错。
在我的C#
表单中,我将order nr.
放入表单中。
然后在sql
。
需要执行此查询:
从OrderID
Order nr.
需要检查Order ID
是否在[System_Opened_Orders]
=订单已打开
如果订单已打开,则需要在C#中运行Messagebox.Show("Order opened, try again later")
order
SQL查询:
Declare @OrderID uniqueidentifier
SET @OrderID = (SELECT m.ID FROM [Agenda].[dbo].[orders] m
WHERE m.OrderNumber= @sqlordernr)
if EXISTS (SELECT 1 FROM [Agenda].[dbo].[System_Opened_Orders]
WHERE Record_ID=@OrderID )
BEGIN
// Tell its opened, need try again later
END
ELSE
BEGIN
// Order is not openend, can get custommer data
select ID, OrderNumber, CustommerName, CustommerCity
FROM [Agenda].[dbo].[orders] where OrderNumber = @sqlordernr
END
C#代码:
if EXISTS (SELECT 1 FROM [Agenda].[dbo].[System_Opened_Orders]
WHERE Record_ID=@OrderID )
BEGIN
// Tell its opened, need try again later
END
ELSE
BEGIN
// Order is not openend, can get custommer data
select ID, OrderNumber, CustommerName, CustommerCity
FROM [Agenda].[dbo].[orders] where OrderNumber = @sqlordernr
END
答案 0 :(得分:3)
如果“订单已打开”
,则无需选择任何内容IF EXISTS (SELECT 1 FROM [Agenda].[dbo].[System_Opened_Orders] WHERE Record_ID=@OrderID )
BEGIN
-- Order is not openend, get custommer data
select ID, OrderNumber, CustommerName, CustommerCity
FROM [Agenda].[dbo].[orders] where OrderNumber = @sqlordernr
END
然后在代码方面;如果datareader上没有行,则.Read()将返回 false
using (SqlDataReader reader = command.ExecuteReader())
{
while(reader.Read())
{
// String OrderCustommerName = reader.GetString(3).TrimEnd();
// String OrderCustommerCity= reader.GetString(4).TrimEnd();
// lbOrderData.Text = OrderCustommerName + " " + OrderCustommerCity;
}
}
答案 1 :(得分:1)
您需要从存储过程中的if语句返回一些内容。您只需返回一个值
即可if EXISTS (SELECT 1 FROM [Agenda].[dbo].[System_Opened_Orders]
WHERE Record_ID=@OrderID )
BEGIN
select 1
END
ELSE
BEGIN
-- Order is not openend, get custommer data
select ID, OrderNumber, CustommerName, CustommerCity
FROM [Agenda].[dbo].[orders] where OrderNumber = @sqlordernr
END
现在你可以看看两个结果之间的区别,看看DataReader的属性FieldCount
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.FieldCount == 1)
MessageBox.Show("Order open");
else
{
while(reader.Read())
{
// String OrderCustommerName = reader.GetString(3).TrimEnd();
// String OrderCustommerCity= reader.GetString(4).TrimEnd();
// lbOrderData.Text = OrderCustommerName + " " + OrderCustommerCity;
}
}
}