2017-08-20 24 views
0

我得到「System.FormatException:輸入格式錯誤。」第二次嘗試錯誤,而第一次完全正常。Parameters.AddWithValue失敗

有人明白爲什麼這樣嗎?

嘗試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 

嘗試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 

我試圖通過去除參數隔離問題和值一個接一個,但最後,我得到了同樣即使使用此稀疏線,也會出錯:

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 

下面是嘗試3的例外屏幕截圖:

enter image description here

+1

AddWithValue的第二個參數是值本身,而不是類型https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue( v = vs.110).aspx在任何情況下嘗試始終使用第一種方法,因爲您對參數 – Steve

+0

的類型有更多的控制哇,謝謝澄清。但是什麼時候可以在「AddWithValue」之後添加「).Value」? – tmighty

+1

您可以這樣做,因爲Add方法返回參數,值是參數的屬性。 – Steve

回答

4

AddWithValue的第二個參數是價值本身,而不是類型

請參閱MSDN AddWithValue

在任何情況下嘗試,因爲你對更多的控制總是使用第一種方法參數的類型。

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