I've got a stored procedure that looks like this:
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 开发者_JAVA百科> @MaxTimestamp
end
and some C# code (in .NET 3.5) that calls it via System.Data.SqlClient
, using a long
for the @MaxTimestamp
parameter.
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;
}
It used to work (and indeed is still working quite happily in production), however my dev copy is now throwing:
InvalidCastException: Failed to convert parameter value from a Int64 to a Byte[].
StackTrace:
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()
Now I can understand why this is failing now, but why not earlier?
Edit: I changed the C# code to convert the long to a Byte[], now it's throwing: Failed to convert parameter value from a Byte[] to a Int64.
, which is the opposite to what it was complaining about initially!
BitConverter.GetBytes is a handy utility function for this
精彩评论