Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NET保存到DB会截断我的小数,而不是四舍五入。。可能出现什么问题?_.net_Vb.net_Enterprise Library - Fatal编程技术网

.NET保存到DB会截断我的小数,而不是四舍五入。。可能出现什么问题?

.NET保存到DB会截断我的小数,而不是四舍五入。。可能出现什么问题?,.net,vb.net,enterprise-library,.net,Vb.net,Enterprise Library,我不是在使用实体框架,而是在使用Microsoft企业库 我有一个类似于:1.125979843654984的值,它被截断为1.125,而不是四舍五入为1.126 该值所在列的精度为小数5,3 这可能发生的原因是什么 我还可以提供其他信息来帮助您吗 我不熟悉.NET 更新1 添加相关代码 调用保存过程的代码: Dim dsCalcGroupByBand As New DataSet dsCalcGroupByBand.Tables.Add(dtCalcGroupByBand) dsCalcGro

我不是在使用实体框架,而是在使用Microsoft企业库

我有一个类似于:1.125979843654984的值,它被截断为1.125,而不是四舍五入为1.126

该值所在列的精度为小数5,3

这可能发生的原因是什么

我还可以提供其他信息来帮助您吗

我不熟悉.NET

更新1 添加相关代码 调用保存过程的代码:

Dim dsCalcGroupByBand As New DataSet
dsCalcGroupByBand.Tables.Add(dtCalcGroupByBand)
dsCalcGroupByBand.Tables(0).TableName = "Table"
Dal.SaveGridDataSet(dsCalcGroupByBand, "Select * from tblLTDCalcGroupByBand where iGroupKY=" & GroupData.GroupNo, False, False)
变量Dal中的方法:

以上代码片段的免责声明:它们都来自遗留代码。 以上述小数为例: 在dtCalcGroupByBand数据表中,存在一个值为1.125979843654984的a行字段,对应于数据库中的一列数据类型Decimal5,3-但是,特定的对应DataColumn对象是datatype System.Decimal-这可能是问题的根源?也许吧

无论如何,一旦保存,在调用Dal.SaveGridDataSet…之后,我在表中查找该值,它显示1.125被截断,而不是1.126四舍五入

更新2
使用Microsoft Enterprise Library或其他OO访问数据库的方式,如何检索列的精度?

该列声明为小数5,3,因此小数点右侧将有3位精度,其余将被截断


如果要对值进行四舍五入,则必须在将其保存到数据库之前在datatable列或正在执行的SQL语句中对其进行四舍五入

我解决了在应用程序启动时加载整个架构,然后根据需要从架构中引用列信息的问题

希望其他人不必像我一样搜索来解决这个问题

安装程序 用法
我不知道封面下到底发生了什么,但你有权自己把这个数字四舍五入吗?我可以,但我不知道每个十进制列的精度是否相同。有些是两位小数,有些是3位小数,等等。DataColumn的数据类型是System.decimal。您可以显示更多代码,或者指定执行截断的类和方法吗?是的,我会立即添加相关代码。待机。有没有办法从数据库中获取精度并在保存前取整到数据库的精度?您可以通过运行SELECT*from information_SCHEMA.COLUMNS版本来获取这样的列信息,但这并不能告诉您正在执行的SQL存储过程的数据类型,等等,您有源吗?表示如果超过最大精度,该值将被截断,因此,根据您所说的,可以推断msdn文章暗示了您所说的内容,但我认为msdn文章不是很清楚。msdn文章正好说明了我所说的内容。如果超过最大精度超过3,则该值将被截断。
Public Function SaveGridDataSet(ByVal pDS As DataSet, ByVal pSQL As String, _
    Optional ByVal pCheckIsNewGroup As Boolean = True, _
    Optional ByVal pForceAsNewGroup As Boolean = False, _
    Optional ByVal pSQLDelete As String = "") As Boolean

  Dim DA As New SqlDataAdapter
  Dim commandBuilder As SqlCommandBuilder
  Dim adapter As SqlDataAdapter
  Dim updatedRows As Integer

  Using connection As SqlConnection = _database.CreateConnection()

    Try
      If ((pCheckIsNewGroup = True) And (GroupData.isNewGroup = True)) Or _
      (pForceAsNewGroup = True) Then
        pDS = IsNewGroup(pDS)
      End If
      DA = New SqlDataAdapter(pSQL, connection) '_database.GetDataAdapter
      ' Make the CommandBuilder generate the insert, update, and delete commands.
      commandBuilder = New SqlCommandBuilder(DA)

      ' Save the changes.
      DA.Update(pDS)

    Catch e As InvalidOperationException
      Try
        ' it's horrible to run code, in here, but there need to be tests
        ' implemented before modifying the above code.
        adapter = New SqlDataAdapter(pSQL, connection)
        commandBuilder = New SqlCommandBuilder(adapter)
        'adapter.SelectCommand = commandBuilder.GetUpdateCommand
        updatedRows = adapter.Update(pDS)
      Catch ee As DBConcurrencyException
        ' there was no change (data already exists, no need to update)
        Console.WriteLine("no data written")
      End Try

    Catch e As DBConcurrencyException

      ' Delete the current records using the optional delete pSQLDelete
      If pSQLDelete = "" Then
        pSQLDelete = pSQL.Replace("Select *", "Delete")
      End If
      UpdateSQL(pSQLDelete)

      ' Now Create the dataset as if a new group and try the update again
      DA.Update(IsNewGroup(pDS))
    Catch e As Exception

      Console.WriteLine("Un-mitigated exception")
      Console.WriteLine(e.Message)
    End Try


  End Using


End Function
' in whatever class you do your database communication:
Private _database As SqlDatabase
Private Shared _schema As DataTable

Sub New()
  ' or however you handle the connection string / database creation
  Dim connectionString as String = GetConnectionString()
  _database = New SqlDatabase(connectionString)

  RetrieveSchema()
End Sub


Private Function RetrieveSchema() as DataTable
  If _schema Is Nothing Then
    Using connection As SqlConnection = _database.CreateConnection()
      connection.Open()
      _schema = connection.GetSchema("Columns")
    End Using
  End If

  return _schema
End Function


Public Function GetColumnInformation(tableName As String, columnName As String) as DataRow
  Dim firstMatchingRow as DataRow = (
    From row In _schema.Rows _
    Where (
      row("TABLE_NAME") = tableName AndAlso row("COLUMN_NAME") = columnName)
    )).FirstOrDefault()

  Return firstMatchingRow
End Function
Dim columnInformation As DataRow = Dal.GetColumnInformation(tableName, columnName)

' find the precision
Dim precision = columnInformation("NUMERIC_PRECISION")
Dim scale = columnInformation("NUMERIC_SCALE")

' convert the decimal to the column's format
' e.g.: 2.345 with a scale of 2 would result in 
'       2.35
value = Decimal.Round(value, scale)