简体   繁体   English

如何在vb.net sql中使用十进制更新

[英]How to Update with a Decimal in vb.net sql

I have a program that selects information and puts it into a datagridview . 我有一个选择信息并将其放入datagridview Once there, the user can update it and then save the information which will go back to the database. 到达那里后,用户可以对其进行更新,然后保存信息,该信息将返回数据库。 When they're saved, it always rounds the decimal that was entered. 保存它们时,它总是四舍五入输入的小数。 How can I get it to save the decimal? 如何保存小数位数?

Private Sub ManagerTableGridView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim SQLcmd As String = "Select Hours_ID, PayPeriod, LastName, FirstName, Title, BillingStore1 as Billable, Comments, TotalSalaryHours, TotalHourlyHours, RegHours, OTHours, PTO, Deductions, DeductReason, OtherPay, OtherReason from Hours join Managers on Managers.Manager_ID = Hours.Manager_ID where PayPeriod = '" & WeeklyHours.SelectedTable & "' order by Billable"    
    Dim cmd As SqlCommand = New SqlCommand(SQLcmd, con)

    con.Open()
    myDA = New SqlDataAdapter(cmd)

    'Automatically generates DeleteCommand, UpdateCommand and InsertCommand for DataAdapter object'
    Dim builder As SqlCommandBuilder = New SqlCommandBuilder(myDA)

    myDA.UpdateCommand = New SqlClient.SqlCommand("Update Hours Set Comments = @Comments, cast(TotalSalaryHours as decimal(4,2)), TotalHourlyHours = @TotalHH, RegHours = @RegH, OTHours = @OT, PTO = @PTO, Deductions = @Deduct, DeductReason = @DeductR, OtherPay = @OtherPay, OtherReason = @OtherR where Hours_ID = @Hours_ID", con)
    myDA.DeleteCommand = New SqlClient.SqlCommand("Delete Hours where Hours_ID = @Hours_ID", con)
    myDataSet = New DataSet()
    myDA.Fill(myDataSet, "MyTable")
    myBS = New BindingSource
    myBS.DataSource = myDataSet.Tables("MyTable").DefaultView
    BindingNavigator1.BindingSource = myBS
    DataGridView1.DataSource = myBS

    con.Close()
    con = Nothing
End Sub

Private Sub SaveToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripButton.Click
    myDA.UpdateCommand.Parameters.Add("@Comments", SqlDbType.NVarChar, 8, "Comments").SourceColumn = "Comments"
    myDA.UpdateCommand.Parameters.Add("@TotalSH", SqlDbType.Decimal, 8, "TotalSalaryHours").SourceColumn = "TotalSalaryHours"
    myDA.UpdateCommand.Parameters.Add("@TotalHH", SqlDbType.Decimal, 8, "TotalHourlyHours").SourceColumn = "TotalHourlyHours"
    myDA.UpdateCommand.Parameters.Add("@RegH", SqlDbType.Decimal, 8, "RegHours").SourceColumn = "RegHours"
    myDA.UpdateCommand.Parameters.Add("@OT", SqlDbType.Decimal, 8, "OTHours").SourceColumn = "OTHours"
    myDA.UpdateCommand.Parameters.Add("@PTO", SqlDbType.Decimal, 8, "PTO").SourceColumn = "PTO"
    myDA.UpdateCommand.Parameters.Add("@Deduct", SqlDbType.Decimal, 8, "Deductions").SourceColumn = "Deductions"
    myDA.UpdateCommand.Parameters.Add("@DeductR", SqlDbType.NVarChar, 8, "DeductReason").SourceColumn = "DeductReason"
    myDA.UpdateCommand.Parameters.Add("@OtherPay", SqlDbType.Decimal, 8, "OtherPay").SourceColumn = "OtherPay"
    myDA.UpdateCommand.Parameters.Add("@OtherR", SqlDbType.NVarChar, 8, "OtherReason").SourceColumn = "OtherReason"
    myDA.UpdateCommand.Parameters.Add("@Hours_ID", SqlDbType.Int, 8, "Hours_ID").SourceColumn = "Hours_ID"
    myDA.DeleteCommand.Parameters.Add("@Hours_ID", SqlDbType.Int, 8, "Hours_ID").SourceColumn = "Hours_ID"

    Me.Validate()
    Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
    Me.myDataSet.AcceptChanges()
End Sub

Thanks. 谢谢。

My guess is that you only specify a precision of 8 for your Decimal paramatets and no scale which causes it to throw away your "decimal points" and only keep the integer part. 我的猜测是,您只能为小数参数指定8的精度,而没有小数位数,这会导致它丢弃“小数点”并仅保留整数部分。

myDA.UpdateCommand.Parameters.Add("@OtherPay", SqlDbType.Decimal, 8, OtherPay").SourceColumn = "OtherPay"

Either remove precision from your parameter; 从参数中删除精度;
or use AddWithValue instead; 或者使用AddWithValue代替;
or take a look here (msdn link) 或在这里看看(msdn链接)

private static void AddSqlParameter(SqlCommand command)
{
    SqlParameter parameter = new SqlParameter("@Price", SqlDbType.Decimal);
    parameter.Value = 3.1416;
    parameter.Precision = 8;
    parameter.Scale = 4;

    command.Parameters.Add(parameter);
}

so you can add precision and scale to your parameter. 因此您可以为参数增加精度小数位数。

In the end, the resolution to my question was the sql server "decimal" column defaults to (18,0) which in turn rounds the numbers coming back to it. 最后,我的问题的解决方案是sql server的“ decimal”列默认为(18,0),这又会将返回的数字四舍五入。 I changed the sql server columns to (4,2) and that resolved the issue without needing to change anything in my code. 我将sql server列更改为(4,2),这解决了该问题,而无需更改代码中的任何内容。

thanks for your assistance though Allan 感谢艾伦的协助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM