簡體   English   中英

如何在c#中獲取SQL Server存儲過程的返回值?

[英]How can I get SQL Server stored procedure return value in c#?

我是C#和SQL Server的初學者,我編寫了這個查詢,用於在SQL Server中創建存儲過程:

create procedure newBehzad 
    @id bigint
as
    DECLARE @ResultValue int

    select *
    from TABLEA
    where id > @id

    SET  @ResultValue = -5
go

一切正常,我編寫了這個C#代碼來調用該存儲過程並返回一個值:

using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("newBehzad", conn)
{
    CommandType = CommandType.StoredProcedure
})
{
    conn.Open();

    command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2;
    command.Parameters.Add("@ResultValue", SqlDbType.Int);

    SqlParameter retval = command.Parameters.Add("@ResultValue", SqlDbType.Int);
    retval.Direction = ParameterDirection.ReturnValue;

    retunvalue = (string)command.Parameters["@ResultValue"].Value;

    //SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar);
    command.ExecuteNonQuery();
    conn.Close();
}

MessageBox.Show(returnValue);

但是當我運行C#windows應用程序時,我收到此錯誤:

過程或函數newBehzad指定了太多參數。

我怎么解決這個問題? 謝謝。

using (var conn = new SqlConnection(connectionString))
            using (var command = new SqlCommand("newBehzad", conn)
            {
                CommandType = CommandType.StoredProcedure
            })
            {
                conn.Open();
                command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2;
               // command.Parameters.Add("@ResultValue", SqlDbType.Int); Comment this line


                SqlParameter retval = command.Parameters.Add("@ResultValue", SqlDbType.Int);
                retval.Direction = ParameterDirection.ReturnValue;

                retunvalue = (string)command.Parameters["@ResultValue"].Value;

                //SqlParameter retval = sqlcomm.Parameters.Add("@b", SqlDbType.VarChar);
                command.ExecuteNonQuery();
                conn.Close();
            }
            MessageBox.Show(returnValue);

將您的程序更改為:

create procedure newBehzad @id bigint, @ResultValue int OUT
as
SET  @ResultValue = 0
BEGIN
    select *from TABLEA
    where id>@id
    SET  @ResultValue = -5
END
go

請試試這樣的想法:

object returnValue = null;
            using (var conn = new System.Data.SqlClient.SqlConnection(AbaseDB.DBFactory.GetInstance().GetConnectionString()))
            {
                using (System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("newBehzad", conn) { CommandType = CommandType.StoredProcedure })
                {
                    conn.Open();
                    command.Parameters.Add("@id", SqlDbType.BigInt).Value = 2;
                    command.Parameters.Add("@ResultValue", SqlDbType.Int).Direction  = ParameterDirection.Output;

                    command.ExecuteNonQuery();

                    returnValue = command.Parameters["@ResultValue"].Value;

                    conn.Close();
                }
                if (returnValue != null)
                    MessageBox.Show(returnValue.ToString());
            }

首先,您需要更改存儲過程以返回值:

create procedure newBehzad @id bigint
as
    DECLARE @ResultValue int
    select *from TABLEA
    where id>@id
    SET  @ResultValue = -5

    Return @ResultValue
go

然后抓住它:

using (var conn = new SqlConnection(connectionString))
{
    conn.Open();        

    using (var cmd = new SqlCommand("newBehzad", conn)
    {
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter retval = new SqlParameter();
        retval.Direction = ParameterDirection.ReturnValue;

        cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = 2;  
        cmd.Parameters.Add(retval);

        cmd.ExecuteNonQuery();

        returnValue = (int)retval.Value;
    }
}

但我真的不明白為什么你在存儲過程中選擇數據...

暫無
暫無

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

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