0

How To Pass An array of bytes from a c# windows application to SQL server via stored procedure? my code in SQL :

        INSERT INTO Pers_UserMachine (userID,nbOfTemplates,fingerPrint1,fingerPrint2,fingerChecksum1,fingerChecksum2)
         VALUES(
            @UserID,            
            @NbOfTemplates,
            CONVERT(VARBINARY(MAX),@FingerPrint1),
            CONVERT(VARBINARY(MAX),@FingerPrint2),
            @fingerChecksum1,
            @fingerChecksum2
                )

and my code in c#:

    public int SaveUser(int ID, string UserID, int NbOfTemplates, byte [] FingerPrint1, byte [] FingerPrint2, Int32 fingerChecksum1, Int32 fingerChecksum2)
    {
        try
        {
            HybridDictionary paramsDictionary = new HybridDictionary();

            paramsDictionary.Add("@ID", ID);
            paramsDictionary.Add("@UserID", UserID);
            paramsDictionary.Add("@NbOfTemplates", NbOfTemplates);
            paramsDictionary.Add("@FingerPrint1", FingerPrint1);
            paramsDictionary.Add("@FingerPrint2", FingerPrint2);
            paramsDictionary.Add("@fingerChecksum1", fingerChecksum1);
            paramsDictionary.Add("@fingerChecksum2", fingerChecksum2);

            DABase da = new DABase();
            return int.Parse(da.GetScalar("PunchMachineSaveUser", paramsDictionary).ToString());
        }
        catch (Exception ex)
        {
            return -1;
        }
    }
ziad mansour
  • 349
  • 6
  • 25
  • 1
    And what happens when you try to use that code? (And why are you converting the results of `GetScalar` to a string and then parsing it?) – Jon Skeet Jun 26 '15 at 09:27
  • Same question here refer link http://stackoverflow.com/questions/8625709/inserting-bytearray-into-sql-through-stored-procedure-called-in-c-sharp – Mukesh Kalgude Jun 26 '15 at 09:30
  • I Got this error when i try to run this code: Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. – ziad mansour Jun 26 '15 at 09:41
  • i didn't convert the result, i only return the id of the record to use it.. – ziad mansour Jun 26 '15 at 09:44
  • I don't know what `DABase.GetScalar()` does, but my guess would be that within it you use `SqlParameterCollection.AddWithValue`, but because using a `HybridDictionary` to pass your parameters the `AddWithValue` method is inferring the wrong type for your parameter. A better solution would be to use a [ParameterCollection](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection%28v=vs.110%29.aspx) to pass your parameters rather than a dictionary. This way you can construct your parameter with the appropriate type, and not rely on inference. – GarethD Jun 26 '15 at 09:50
  • thanks all, it is done, i took the solution of Gareth..but instead of SqlDataAdapter i used the SQLCommand... – ziad mansour Jun 26 '15 at 11:00

0 Answers0