2017-02-13 34 views
0
存儲負數在MySQL數據庫

我已經能夠成功地將數據保存到數據庫然而,當我嘗試存儲的條目,如:從VB.NET

1,1,"Description 1",-195.37 
1,2,"Description 2",-175.90 
1,3,"Description 3",108.00 
1,4,"Description 4",-57.33 
1,5,"Description 5",95.32 

它吐出了這一點:

--------------------------- 
Application Name Here 
--------------------------- 
System.FormatException: Input string was not in a correct format. 
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) 
at System.String.System.IConvertible.ToInt32(IFormatProvider provider) 
at System.Convert.ToInt32(Object value) 
at MySql.Data.Types.MySqlInt32.MySql.Data.Types.IMySqlValue.WriteValue(MySqlPacket packet, Boolean binary, Object val, Int32 length) 
at MySql.Data.MySqlClient.MySqlParameter.Serialize(MySqlPacket packet, Boolean binary, MySqlConnectionStringBuilder settings) 
at MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection parameters, MySqlPacket packet, String parmName, Int32 parameterIndex) 
at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet) 
at MySql.Data.MySqlClient.Statement.BindParameters() 
at MySql.Data.MySqlClient.PreparableStatement.Execute() 
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() 
at ApplicationNameHere.frmPaySlip.Button2_Click(Object sender, EventArgs e) in frmPaySlip.vb:line 210 
--------------------------- 
OK  
--------------------------- 

該數據庫佈局如下:我使用

psid int(5) 
rowid int(5) 
description varchar(255) 
value decimal(20,2) 

代碼爲:

Dim cmd_result As Integer = CInt(selectCommand.ExecuteScalar()) 
Dim rowID As Integer = 1 
selectCommand2.Connection = connection 
selectCommand2.CommandText = "INSERT INTO `ps_payslip_datrow` (`psid`, `rowid`, `description`, `value`) VALUES (@psid, @rowid, @description, @value)" 
selectCommand2.Parameters.AddWithValue("@psid", cmd_result) 
selectCommand2.Parameters.AddWithValue("@rowid", 0) 
selectCommand2.Parameters.AddWithValue("@description", 0) 
selectCommand2.Parameters.AddWithValue("@value", 0.00) 
For Each item As ListViewItem In lbDedAddTot.Items 
    selectCommand2.Parameters("@rowid").Value = rowID 
    selectCommand2.Parameters("@description").Value = item.SubItems(0).Text 
    selectCommand2.Parameters("@value").DbType = DbType.Decimal 
    selectCommand2.Parameters("@value").Value = Convert.ToDecimal(item.SubItems(1).Text) 
    selectCommand2.ExecuteNonQuery() 
    rowID += 1 
Next 

說明: cmd_result工作得很好。只是這個循環失敗了。 VB.NET正在將數字轉換爲十進制。一直在做休息和觀看來驗證。 我一直在試圖查看過去兩天我錯了什麼地方。

+0

你真的想要說明是一個數字嗎? –

+1

這是你真正應該使用的地方add addwithvalue,所以你不會得到混合值類型。 –

+1

您也設置了兩次'@ value'的值。如果它確實是MySql,則使用'MySqlDbType'來指定類型。你可以擺脫整個事情,如果你使用綁定datagridview – Plutonix

回答

0

在此行中, selectCommand2.Parameters.AddWithValue( 「@描述」,0) 你們真的沒有忘記爲0(零)的報價?

+0

當然。當你盯着代碼太久時,會發生這種情況。 :-P謝謝! –

0

它看起來像是你無意中用「AddWithValue」語句使「Description」成爲一個整數。改用Add方法。

selectCommand2.CommandText = "INSERT INTO `ps_payslip_datrow` (`psid`, `rowid`, `description`, `value`) VALUES (@psid, @rowid, @description, @value)" 
    selectCommand2.Parameters.Add("@psid", SqlDbType.Int).Value = cmd_result 
    selectCommand2.Parameters.Add("@rowid", SqlDbType.Int) 
    selectCommand2.Parameters.Add("@description", SqlDbType.VarChar) 
    selectCommand2.Parameters.Add("@value", SqlDbType.Decimal) 
    For Each item As ListViewItem In lbDedAddTot.Items 
     selectCommand2.Parameters("@rowid").Value = rowID 
     selectCommand2.Parameters("@description").Value = item.SubItems(0).Text 
     selectCommand2.Parameters("@value").Value = Convert.ToDecimal(item.SubItems(1).Text) 
     selectCommand2.ExecuteNonQuery() 
     rowID += 1 
    Next 

您還應該測試Value來源以確保它確實是小數。