2010-11-03 19 views
1

J'ai une procédure stockée qui ressemble à ceci: qu'il appelle via System.Data.SqlClientInt64 qui exerce la contrainte à l'octet [] dans l'appel de procédure stockée

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 

et du code C# (.NET 3.5), en utilisant un Pour le paramètre @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; 
    } 

Il travaillait (et même travaille encore tout à fait avec bonheur dans la production), mais ma copie dev jette maintenant:

InvalidCastException: Impossible de convertir la valeur de paramètre à partir d'un Int64 à un octet [ ]

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() 

Maintenant, je peux comprendre pourquoi cela échoue maintenant, mais pourquoi pas plus tôt? Editer: J'ai changé le code C# pour convertir le long en Byte [], maintenant il lance: Failed to convert parameter value from a Byte[] to a Int64., ce qui est le contraire de ce dont il se plaignait initialement!

+0

Qu'est-ce que le look de code C# comme? Utilise-t-il la classe 'BitConverter'? –

+0

ajouté le code C# ... – geofftnz

+0

a fini par écrire une solution de contournement où je spécifie explicitement les noms et les types de paramètres SP. – geofftnz

Répondre

0

BitConverter.GetBytes est une fonction d'utilité pratique pour ce

+0

Essayé, il a ensuite lancé InvalidCastException: Impossible de convertir la valeur du paramètre d'un octet [] en un Int64. (L'inverse de la première exception). Allez au diable!! POURQUOI NE POUVEZ-VOUS PAS RENDRE VOTRE ESPRIT? – geofftnz

+0

Quel est le type de la colonne local_timestamp? –