2017-08-20 4 views
0

Je reçois le "System.FormatException: L'entrée a le mauvais format." erreur sur la deuxième tentative alors que le premier fonctionne parfaitement bien.Parameters.AddWithValue échouant

Est-ce que quelqu'un voit pourquoi c'est ainsi?

Tentative 1:

Using nCmdIns1 As SQLite.SQLiteCommand = cnUser.CreateCommand 
     With nCmdIns1 
      .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)" 
      .Parameters.Add("@1", DbType.String).Value = uOEMImageGUID 
      .Parameters.Add("@2", DbType.String).Value = uTitle 
      .Parameters.Add("@3", DbType.Int32).Value = iCat 
      .Parameters.Add("@4", DbType.Int32).Value = uImageSize 
      .Parameters.Add("@5", DbType.Binary).Value = uBytes 
      .ExecuteNonQuery() 
     End With 
    End Using 

Tentative 2:

Using nCmdIns2 As SQLite.SQLiteCommand = cnUser.CreateCommand 
     With nCmdIns2 
      .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)" 
      .Parameters.AddWithValue("@1", DbType.String).Value = uOEMImageGUID 
      .Parameters.AddWithValue("@2", DbType.String).Value = uTitle 
      .Parameters.AddWithValue("@3", DbType.Int32).Value = iCat 
      .Parameters.AddWithValue("@4", DbType.Int32).Value = uImageSize 
      .Parameters.AddWithValue("@5", DbType.Binary).Value = uBytes 
      .ExecuteNonQuery() 
     End With 
    End Using 

J'ai essayé d'isoler le problème en supprimant les paramètres et les valeurs une par une, mais à la fin, je suis le même erreur même avec cette ligne clairsemée:

Using nCmdIns3 As SQLite.SQLiteCommand = cnUser.CreateCommand 
     With nCmdIns3 
      .CommandText = "INSERT INTO images (oemimageguid) VALUES (@1)" 
      .Parameters.AddWithValue("@1", DbType.String).Value = uOEMImageGUID 
      .ExecuteNonQuery() 
     End With 
    End Using 

Voici une capture d'écran de l'exception pour tentative 3:

enter image description here

+1

Le second paramètre de AddWithValue est la valeur elle-même, et non pas du type https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue (v = vs.110) .aspx dans tous les cas essayez d'utiliser toujours la première méthode parce que vous avez plus de contrôle sur le type du paramètre – Steve

+0

Wow, merci pour la clarification. Mais quand puis-je ajouter un ") .Value" après "AddWithValue"? – tmighty

+1

Vous pouvez car la méthode Add renvoie le paramètre et la valeur est une propriété du paramètre. – Steve

Répondre

4

Le deuxième paramètre de AddWithValue est la valeur elle-même, pas le type

Voir MSDN AddWithValue

Dans tous les cas, essayez d'utiliser toujours la première méthode parce que vous avez plus de contrôle sur la type du paramètre.

Can we stop using AddWithValue already?

Using nCmdIns2 As SQLite.SQLiteCommand = cnUser.CreateCommand 
    With nCmdIns2 
     .CommandText = "INSERT INTO images (oemimageguid,imagetitle,imagecategory,imagesize,imageblob256) VALUES (@1,@2,@3,@4,@5)" 
     .Parameters.AddWithValue("@1", uOEMImageGUID) 
     .Parameters.AddWithValue("@2", uTitle) 
     .Parameters.AddWithValue("@3", iCat) 
     .Parameters.AddWithValue("@4", uImageSize) 
     .Parameters.AddWithValue("@5", uBytes) 
     .ExecuteNonQuery() 
    End With 
End Using