簡體   English   中英

調用存儲過程並返回消息

[英]Call stored procedure and return a message

我有以下 SQL Server 存儲過程(缺少聲明的參數,但我沒有問題):

DECLARE @TotalRegistros AS INT = 0
DECLARE @ShowMsg AS NVARCHAR(50)    

SELECT @TotalRegistros = COUNT(*) 
FROM Tbl_Productos 
WHERE IdProducto = @Producto

IF @TotalRegistros < 3
BEGIN
    INSERT INTO Tbl_Productos (IdProducto, UnidadMedida, Precio, Activo) 
    VALUES (@Producto, @Medida, @Precio, @Activo)

    SET @ShowMsg = 'All fine!'
END
ELSE
BEGIN
    SET @ShowMsg = 'Error, you already have 3 rows.!'
END

SELECT @ShowMsg

我用這個 C# 代碼執行這個存儲過程:

public string ExecuteSP(Producto prod)
{
    int res = 0;
    string resp = "";

    SqlCommand cmd = new SqlCommand("SPName", conn);

    try
    {
        Open();
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@Producto", prod.Id);
        cmd.Parameters.AddWithValue("@Medida", prod.Tipo);
        cmd.Parameters.AddWithValue("@Precio", prod.Precio);
        cmd.Parameters.AddWithValue("@Activo", prod.Active);

        //CodeUpdated
        SqlParameter returnMessage = new SqlParameter("@ShowMsg", SqlDbType.NVarChar);
        returnMessage.Direction = ParameterDirection.ReturnValue;

        cmd.Parameters.Add(returnMessage);

        res = cmd.ExecuteNonQuery();

        resp = returnMessage.Value.ToString();
    }
    finally
    {
        Close();
    }

    return resp;
}

我知道我需要添加類似OutputParameter東西並添加@ShowMsg但我不知道該怎么做! 主要問題是我使用以下代碼從WebMethod執行此代碼:

[WebMethod]
public string ExecuteC#Code(int id, string medida, string precio, string activo)
{
    string respuesta = "Error";

    try
    {
        Producto prod = new Producto(id, medida, precio, activo);
        //Code updated
        string resp = conn.ExecuteSP(prod);

        if (resp == "All fine!")
            respuesta = "Operacion realizada existosamente";
        else
            respuesta = "Already 3 rows with same Id..."
    }
    catch (Exception ex)
    {
        respuesta = "A ocurrido un error: " + ex.Message;
    }

    return respuesta;
}

就像我說的,我知道在執行存儲過程后我需要做什么來獲取消息,但是我不知道如何將該消息發送到我的 Web 方法。 我知道我需要更改我的方法的數據類型,因為我需要一個string而不是int

您應該使用 SQL Server 標量函數:

CREATE FUNCTION [dbo].[GetUserNameByID](@UserID int)
RETURNS nvarchar(max)
AS
BEGIN
    DECLARE @UserName nvarchar(max);

    SET @UserName = (SELECT TOP (1) [Login] 
                     FROM [dbo].[Users] 
                     WHERE ID = @UserID)
    RETURN @UserName
END

解決了! 這是我從代碼中修改的內容:

存儲過程:

Declare @ShowMsg as nvarchar(50) TO Declare @ShowMsg as int 

set @ShowMsg = a number (10, 20, whatever)

C#代碼:

public string ExecuteSP(Producto prod) TO public int ExecuteSP(Producto prod)

SqlParameter returnMessage = new SqlParameter("@ShowMsg", SqlDbType.NVarChar);

SqlParameter returnMessage = new SqlParameter("@ShowMsg", SqlDbType.Int);

resp = returnMessage.Value.ToString(); to Convert.ToInt32(resp = returnMessage.Value);

網絡方法:

string resp = conn.ExecuteSP(prod); TO int resp = conn.ExecuteSP(prod);

最后

if (resp == "All fine!")
        respuesta = "Operacion realizada existosamente";
    else
        respuesta = "Already 3 rows with same Id..."

if (resp == 10)
    respuesta = "Operacion realizada existosamente";
else if (resp == 5)
    respuesta = "Already 3 rows with same Id...";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM