繁体   English   中英

存储过程调用中将Int64强制转换为Byte []

[英]Int64 coercing to Byte[] in stored procedure call

我有一个看起来像这样的存储过程:

create procedure [dbo].[Batch_of_Things_Get] (@MaxTimestamp binary(8)) as 
begin
    set nocount on

    select top 3500
        i.ThingID,
        i.Name,
        i.CreationDate,
        i.local_timestamp
    from
        [dbo].[Thing] i
    where
        i.local_timestamp > @MaxTimestamp
end

以及一些通过System.Data.SqlClient调用它的C#代码(在.NET 3.5中),使用@MaxTimestamp参数的long @MaxTimestamp

    public static string GetUpdatedThings(long MinTimestamp)
    {
        // the ExecuteXmlDocument call takes: string SPName, params object[] SPParams
        return new SqlProcedure(Config.ConnectionString).ExecuteXmlDocument("Batch_of_Things_Get", MinTimestamp).OuterXml;
    }

它曾经可以工作(实际上在生产中仍然很快乐),但是我的开发副本现在抛出了:

InvalidCastException:无法将参数值从Int64转换为Byte []。

堆栈跟踪:

at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
at System.Data.SqlClient.SqlParameter.GetCoercedValue()
at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection  parameters, _SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteXmlReader()

现在我可以理解为什么现在失败了,但是为什么不更早呢?

编辑:我更改了C#代码以将long转换为Byte [],现在抛出: Failed to convert parameter value from a Byte[] to a Int64. ,这与最初抱怨的相反!

BitConverter.GetBytes是一个方便的实用程序函数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM