带有sql查询结果的C#到变量

时间:2017-02-26 21:55:55

标签: c# sql-server

我是C#的新手,所以也许我没有正确地做错。

在我的C#表单中,我将order nr.放入表单中。 然后在sql

中进行检查

需要执行此查询:

  1. OrderID

  2. 获取Order nr.
  3. 需要检查Order ID是否在[System_Opened_Orders] =订单已​​打开

  4. 如果订单已打开,则需要在C#中运行Messagebox.Show("Order opened, try again later")

  5. 如果未打开,请从order
  6. 加载数据

    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
    

2 个答案:

答案 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;
        }
    }
}