2009-10-01 25 views
0

Dans la procédure stockée ci-dessous, je retourne une ligne si toutes les conditions vérifie, sinon je retourne un message comme quelle condition n'est pas satisfaite. Le proc stocké fonctionne parfaitement ...Comment obtenir la valeur de retour et les lignes dans C# thru procédure stockée

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 

Le problème est ne sais pas comment obtenir le message de retour ou de retour ligne de la SP en utilisant C#.

Le code ci-dessous me renvoie les lignes si la condition est satisfaite dans SP. sinon, je ne reçois pas le message de retour. Comment l'obtenir?

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

Répondre

1

Vous devez ajouter un paramètre sur:

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

puis après l'exécution de la SP

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

est fonction ici avec des param:

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

REMARQUE: Je n'ai jamais fini avec l'utilisation d'ExecuteNonQu puis en utilisant Fill sur un adaptateur de données. Cela pourrait gâcher ça.

+0

@tster, déjà je retourne un ensemble de données qui contient la ligne, comment retourner le message aussi? – Anuya

+0

Le simple paramétrage de la valeur dans le SP le fera parce que vous l'avez déclaré comme paramètre out. – tster

+0

Je veux dire, comment obtenir le message de retour dans mon codebehind? parce que je retourne seulement l'ensemble de données de cette fonction. – Anuya

0

Ce n'est pas faisable en C#. Vous pouvez uniquement renvoyer un DataTable avec une seule ligne (en utilisant la méthode Fill dans votre exemple) ou vous pouvez renvoyer une seule valeur (en utilisant un SqlCommand avec un paramètre de retour ou ExecuteScalar). Au lieu de cela, vous devriez faire un SELECT dans les deux cas mais avec des champs différents en fonction de l'instruction IF. À savoir,

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

est converti pour être

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

Et puis vous vérifiez le nom de champ dans votre DataTable de retour. Par exemple.

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

Que fait ExecuteNonQuery dans votre code lorsque vous n'insérez rien?

Il existe des moyens de le faire. Un, utilisez un DataReader. C'est un peu plus utile dans ce genre de scénarios. Ou vous pouvez ajouter un paramètre de sortie à la procédure stockée et vérifier qu'après avoir exécuté le proc via C#.

+0

Je suis d'accord. Devrait faire ExecuteQuery() – tster

Questions connexes