簡體   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