I have the following C# code and a SQL Server stored procedure.
private static long GetTestBigintOutput(SqlConnection dbConn)
{
var sqlParams = new DynamicParameters();
sqlParams.Add("@input_param", 1);
sqlParams.Add("@output_param", SqlDbType.BigInt, direction: ParameterDirection.Output);
dbConn.Execute("stp_zzz_test_output_bigint", sqlParams, commandType: CommandType.StoredProcedure);
var output = sqlParams.Get<long>("@output_param");
return output;
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
var output = GetTestBigintOutput(GetDbConnection());
Console.WriteLine($"Output = {output}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
Stored procedure:
CREATE PROCEDURE stp_zzz_test_output_bigint
(@input_param bigint,
@output_param bigint OUTPUT)
AS
BEGIN
SET @output_param = @input_param
END
The output of this simple console app is
Hello World!
Unable to cast object of type 'System.Int32' to type 'System.Int64'.
Is this to be expected? My other stored procedures return values that are tied to BIGINT
columns, but the actual returned value will fit in an Int32
. However I can't be sure beforehand what the actual returned value will be and this will change once the program is running.
I know I can change the
var output = sqlParams.Get<long>("@output_param");
to
var output = sqlParams.Get<dynamic>("@output_param");
However I feel that this is a kind of cheating.
Any thoughts?
regards Paul
I don't think the Add
method of DynamicParameters
accepts SqlDbType
as the dbType
parameter. It should be DbType
instead, so you can try to change following:
sqlParams.Add("@output_param", SqlDbType.BigInt, direction: ParameterDirection.Output);
to that:
sqlParams.Add("@output_param", dbType: DbType.Int64, direction: ParameterDirection.Output);