我正在使用时间跨度函数来计算天数,然后将此值添加到文本框并保存到数据库中。
我最近添加了一个单选按钮作为半天,所以如果选中它会增加0.5天假期而不是1天。这在保存之前显示在文本框中似乎正常工作。但是,当它实际保存并更新时,数据库显示的值为1而不是0.5,如文本框所示。
此字段的数据库中的字段类型最初设置为“int”但是当我添加半天它不会保存,因为它试图保存小数字,我已经将此字段类型更改为“十进制”它认为这会更合适..我不确定这是否正确或它应该是不同的东西?
我似乎无法弄清楚为什么它没有保存文本框中指定的值...任何建议
Private Sub btnHNoDays_Click(sender As Object, e As System.EventArgs) Handles btnHNoDays.Click
'declare dates
Dim dtStart As Date = txtHStart_Date.Text
Dim dtEnd As Date = txtHEnd_Date.Text
'timespan function used to minus one date to another and produce a value
Dim ts As TimeSpan = (dtEnd - dtStart)
If RadioButton1.Checked Then
ts = (dtEnd - dtStart) - TimeSpan.FromDays(0.5)
Else
ts = (dtEnd - dtStart)
End If
' the value is set to to textbox.
txtNoofDays.Text = ts.TotalDays()
' the today days value
Console.WriteLine(ts.TotalDays)
End Sub
Protected Sub btnHRequestSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnHRequestSave.Click
'collect data
Dim Sdate = txtHStart_Date.Text
Dim Edate = txtHEnd_Date.Text
Dim NoofDays = txtNoofDays.Text
Dim notes = TxtHRequestNotes.Text
Dim username = lblHolidayRequestLIU.Text
'define connection
Dim HRequestConnection As New SqlConnection
HRequestConnection.ConnectionString = HolidayRequestSqlDataSource.ConnectionString
'create command
Dim HRequestInsert As New SqlCommand("Insert into HolidayRequests (username, RequestDateStart, RequestDateEnd, RequestTotalDays, RequestNotes) VALUES (@username, @Sdate, @Edate, @NoofDays, @notes)", HRequestConnection)
'define parameters
HRequestInsert.Parameters.Add("username", SqlDbType.NVarChar, 20).Value = lblHolidayRequestLIU.Text
HRequestInsert.Parameters.Add("Sdate", SqlDbType.Date).Value = txtHStart_Date.Text
HRequestInsert.Parameters.Add("Edate", SqlDbType.Date).Value = txtHEnd_Date.Text
HRequestInsert.Parameters.Add("NoofDays", SqlDbType.Decimal).Value = txtNoofDays.Text
HRequestInsert.Parameters.Add("notes", SqlDbType.NVarChar).Value = TxtHRequestNotes.Text
' execute commands
HRequestConnection.Open()
HRequestInsert.ExecuteNonQuery()
lblHolRequestResponse.Text = "Your holidays request has been saved!
End Sub
答案 0 :(得分:0)
您是如何在数据库中指定decimal
列的?你有精确度和规模(see here)吗?如果没有,则默认为decimal(18,0)
,这将使小数点后没有数字。
根据您的要求,您应该根据decimal(5,1)
给它一个类型。
请记住,精度是总位数(到小数点的左右), scale 是数字到小数点的右。因此decimal(5,1)
最多可存储9999.9。
(我假设您正在使用SQL Server)