2010-11-03 32 views
1

我有一個存儲過程是這樣的:即通過System.Data.SqlClient調用它Int64的脅迫爲byte []在存儲過程調用

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 

和一些C#代碼(在.net 3.5),使用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; 
    } 

它用來工作(實際上仍在生產工作相當愉快),但我的dev副本現在拋出:

InvalidCastException的:無法參數值從Int64類型轉換爲字節[ ]。

堆棧跟蹤:

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.,這與它最初抱怨的內容相反!

+0

什麼是C#代碼的樣子?它使用'BitConverter'類嗎? – 2010-11-03 04:39:50

+0

添加了C#代碼... – geofftnz 2010-11-03 04:47:35

+0

最終寫了一個解決方法,我明確指定了SP參數名稱和類型。 – geofftnz 2010-11-03 23:03:54

回答

0

BitConverter.GetBytes是這個方便的實用函數

+0

嘗試過,然後拋出InvalidCastException:無法將參數值從Byte []轉換爲Int64。 (與第一個例外相反)。該死的你!!爲什麼不能讓你心動? – geofftnz 2010-11-03 19:46:07

+0

local_timestamp列的類型是什麼? – 2010-11-04 10:57:03