2009-10-01 65 views
0

在下面的存儲過程中,如果所有條件都滿足,我將返回一行,否則我將返回一條消息,如條件是否滿足。 存儲的過程是可以正常使用...如何在c#中通過存儲過程獲取返回值和行

ALTER PROCEDURE dbo.BookingCheck 
    (
    @int_duration_of_stay int , 
    @int_number_of_guests int, 
    @date_of_application date, 
    @date_of_checkin  date, 
    @date_of_checkout  date, 
    @str_room_type   varchar(50), 
    @ret_value    varchar(100) = '' output 
    ) 
AS 
    DECLARE @MaxPer int 
    DECLARE @BasicCharge int 
    DECLARE @SurCharge int 
    DECLARE @TotalAmount int 
    DECLARE @NoOfDays int 
    DECLARE @Free VARCHAR(10) 

    IF @int_duration_of_stay > 6 
    BEGIN 
     SET @NoOfDays = @int_duration_of_stay 
     SET @Free = 'Yes' 
    END 
    ELSE 
    BEGIN 
     SET @NoOfDays = @int_duration_of_stay - 1 
     SET @Free = 'No' 
    END 


    SELECT @MaxPer = int_max_pax, @BasicCharge = flt_basic_charge, @SurCharge = flt_surcharge_per_pax 
    FROM RoomTypes WHERE UPPER(str_room_type) = UPPER(@str_room_type) 

IF DATEDIFF(DAY, GETDATE(), @date_of_checkin) < 40 
BEGIN 
    IF @int_number_of_guests <= @MaxPer 
    BEGIN 
     SET @TotalAmount = (@NoOfDays * @int_number_of_guests * @SurCharge) + @BasicCharge 
     SET @ret_value = 'Success' 

     SELECT @str_room_type as 'Room Type', @MaxPer as 'Max Persons Allowed', @int_number_of_guests as 'No. of persons requested', 
     @int_duration_of_stay as 'No. of days stay', @BasicCharge as 'Basic Charge', @SurCharge as 'Sur Charge', @Free as 'Complimentary', 
     @TotalAmount as 'Total Amount' 
    END 
    ELSE 
    BEGIN 
     SET @ret_value = 'Max persons allowed is ' + CONVERT(VARCHAR(20), @MaxPer) 
    END 
END 
ELSE 
BEGIN 
    SET @ret_value = 'The check in date should be less than 40 days from current date.' 
END 
RETURN 

的問題是不知道如何得到返回的消息或使用C#從SP返回的行。

如果條件在SP中滿足,則下面的代碼返回行。如果沒有,我沒有收到回覆消息。如何獲得?

public DataSet BookingCheck(int duration_of_stay, int number_of_guests, 
    string date_of_application, string date_of_checkin, string date_of_checkout, 
    string room_type) 
{ 
    DataSet dsGetBookingCheck = new DataSet(); 
    SqlConnection conn = new SqlConnection(Con); 
    SqlCommand command = new SqlCommand("BookingCheck", conn); 
    command.CommandType = CommandType.StoredProcedure; 
    SqlDataAdapter da = new SqlDataAdapter(); 
    SqlParameter param = new SqlParameter(); 
    param = command.Parameters.Add("@int_duration_of_stay", SqlDbType.Int); 
    param.Value = duration_of_stay; 
    param = command.Parameters.Add("@int_number_of_guests", SqlDbType.Int); 
    param.Value = number_of_guests; 
    param = command.Parameters.Add("@date_of_application", SqlDbType.Date); 
    param.Value = date_of_application; 
    param = command.Parameters.Add("@date_of_checkin", SqlDbType.Date); 
    param.Value = date_of_checkin; 
    param = command.Parameters.Add("@date_of_checkout", SqlDbType.Date); 
    param.Value = date_of_checkout; 
    param = command.Parameters.Add("@str_room_type", SqlDbType.VarChar, 50); 
    param.Value = room_type; 
    conn.Open(); 
    command.ExecuteNonQuery(); 
    da.SelectCommand = command; 
    da.Fill(dsGetBookingCheck); 
    conn.Close(); 
    return dsGetBookingCheck; 
} 

回答

1

您需要添加一個輸出參數:

public DataSet BookingCheck(int duration_of_stay, int number_of_guests, 
    string date_of_application, string date_of_checkin, string date_of_checkout, 
    string room_type, out string message) 
{ 
    DataSet dsGetBookingCheck = new DataSet(); 
    SqlConnection conn = new SqlConnection(Con); 
    SqlCommand command = new SqlCommand("BookingCheck", conn); 
    command.CommandType = CommandType.StoredProcedure; 
    SqlDataAdapter da = new SqlDataAdapter(); 
    SqlParameter param = new SqlParameter(); 
    param = command.Parameters.Add("@int_duration_of_stay", SqlDbType.Int); 
    param.Value = duration_of_stay; 
    param = command.Parameters.Add("@int_number_of_guests", SqlDbType.Int); 
    param.Value = number_of_guests; 
    param = command.Parameters.Add("@date_of_application", SqlDbType.Date); 
    param.Value = date_of_application; 
    param = command.Parameters.Add("@date_of_checkin", SqlDbType.Date); 
    param.Value = date_of_checkin; 
    param = command.Parameters.Add("@date_of_checkout", SqlDbType.Date); 
    param.Value = date_of_checkout; 
    param = command.Parameters.Add("@str_room_type", SqlDbType.VarChar, 50); 
    param.Value = room_type; 
    command.Parameters.Add("@ret_value", SqlDbType.String); 
    command.Parameters["@ret_value"].Direction = ParameterDirection.Output; 
    conn.Open(); 
    command.ExecuteNonQuery(); 
    da.SelectCommand = command; 
    da.Fill(dsGetBookingCheck); 
    message = command.Parameters["@ret_value"].Value.ToString(); 
    conn.Close(); 
    return dsGetBookingCheck; 
} 

注:執行SP

message = command.Parameters["@ret_value"].Value.ToString(); 

這裏是出PARAM功能後

command.Parameters.Add("@ret_value", SqlDbType.String); 
command.Parameters["@ret_value"].Direction = ParameterDirection.Output; 

則:我從來沒有使用過ExecuteNonQu然後使用填充數據適配器。這可能會搞砸了。

+0

@tster,我已經返回一個包含該行的數據集,如何返回消息呢? – Anuya 2009-10-01 02:54:17

+0

只需在SP中設置值就可以做到這一點,因爲您已將其聲明爲輸出參數。 – tster 2009-10-01 02:58:44

+0

我的意思是,如何在我的代碼隱藏中獲取返回消息?因爲我只返回該函數的數據集。 – Anuya 2009-10-01 03:00:55

0

這在C#中無法實現。您只能使用單行返回一個DataTable(在您的示例中使用Fill方法),或者可以返回單個值(使用帶有返回參數或ExecuteScalar的SqlCommand)。

相反,你應該在兩種情況下做一個SELECT,但是根據IF語句的不同而有不同的字段。即,

SET @ret_value = 'Max persons allowed is ' + CONVERT(VARCHAR(20), @MaxPer) 

轉化爲

SELECT 'Max persons allowed is ' + CONVERT(VARCHAR(20), @MaxPer) AS Return_Value 

然後你檢查你的回報數據表的字段名。例如。

if (BookingCheck(...).Tables[0].Columns.Contains("Return_Value")) { 
    // Handle my special condition here 
} 
1

當你沒有插入任何東西時,ExecuteNonQuery在你的代碼中做什麼?

有辦法做到這一點。一,使用DataReader。這在這種情況下會更有幫助。或者,您可以將輸出參數添加到存儲過程,並檢查在通過C#執行proc之後。

+0

我同意。應該做ExecuteQuery() – tster 2009-10-01 03:14:51