2017-02-26 130 views
0

我是新的C#,所以也許我沒有正確地記錄它。C#與sql查詢結果變量

在我的C#表格中,我把order nr.放入表格中。 然後在sql中檢查。

需要執行這個查詢:

  1. 獲取OrderIDOrder nr.

  2. 需要檢查,如果Order ID是​​=爲了打開

  3. 如果訂單被打開,則需要以C#運行#Messagebox.Show("Order opened, try again later")

  4. 如果沒有打開,負載數據從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 [email protected]) 
      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#代碼:

SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Agenda;Persist Security Info=True;User ID=sa;Password=xxxxxxxx"); 
      conn.Open(); 

      SqlCommand command = new SqlCommand(/*sql query*/, conn); 
      command.Parameters.AddWithValue("@sqlordernr", odernr); 

      using (SqlDataReader reader = command.ExecuteReader()) 
      { 
       if (reader.Read()) 
       { 
        // if order is opened then Messagebox.Show 

        // if is not opened 
        // String OrderCustommerName = reader.GetString(3).TrimEnd(); 
        // String OrderCustommerCity= reader.GetString(4).TrimEnd(); 
        // lbOrderData.Text = OrderCustommerName + " " + OrderCustommerCity; 
       } 
      } 
      conn.Close(); 
+0

答案是好的,但如果你願意,你可以嘗試SQL [輸出參數](http://www.aspsnippets.com /Articles/How-to-return-Output-parameter-from-Stored-Procedure-in-ASPNet-in-C-and-VBNet.aspx)。 –

回答

1

您需要返回somethi存儲過程中的if語句。你可以簡單地返回一個值

if EXISTS (SELECT 1 FROM [Agenda].[dbo].[System_Opened_Orders] 
    WHERE [email protected]) 
     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; 
     } 
    } 
} 
2

你不需要選擇任何東西,如果「訂單打開「

IF EXISTS (SELECT 1 FROM [Agenda].[dbo].[System_Opened_Orders] WHERE [email protected]) 
BEGIN 
    -- Order is not openend, get custommer data 
    select ID, OrderNumber, CustommerName, CustommerCity 
    FROM [Agenda].[dbo].[orders] where OrderNumber = @sqlordernr 
END 

然後在代碼端;如果沒有一行的DataReader,while.Read()將返回

using (SqlDataReader reader = command.ExecuteReader()) 
{ 
    while(reader.Read()) 
    { 
     // String OrderCustommerName = reader.GetString(3).TrimEnd(); 
     // String OrderCustommerCity= reader.GetString(4).TrimEnd(); 
     // lbOrderData.Text = OrderCustommerName + " " + OrderCustommerCity; 
    } 
} 
+0

謝謝,非常簡單,但在我的情況下最好使用'select 1' – nordscan