SQL 服务器 INFORMATION_SCHEMA 报告的浮点精度与 ADO.NET GetSchemaTable() 不同?
SQL Server INFORMATION_SCHEMA reports different float precision than ADO.NET GetSchemaTable()?
对于声明为 float
的 SQL 服务器列,INFORMATION_SCHEMA
报告数值精度为 53,而 ADO.NET SqlDataReader.GetSchemaTable()
方法报告 15 . 造成这种明显差异的原因是什么?
这是 VB 中的一个最小代码示例:
Private Sub MinimalCodeExample(conn As DbConnection)
Try
CreateTestTable(conn)
Dim precisionFromInformationSchema As Integer =
GetPrecisionFromInformationSchema(conn) ' 53
Dim precisionFromADONETSchemaTable As Integer =
GetPrecisionFromADONETSchemaTable(conn) ' 15
' Why does this assertion fail, 53 <> 15?
Debug.Assert(precisionFromInformationSchema = precisionFromADONETSchemaTable)
Finally
CleanUpTestTable(conn)
End Try
End Sub
Private Sub CreateTestTable(conn As DbConnection)
Dim cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "CREATE TABLE WhosebugEXAMPLE(value float)"
cmd.ExecuteNonQuery()
End Sub
Private Function GetPrecisionFromInformationSchema(conn As DbConnection) As Integer
Using cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "SELECT NUMERIC_PRECISION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='WhosebugEXAMPLE'"
Return CInt(cmd.ExecuteScalar())
End Using
End Function
Private Function GetPrecisionFromADONETSchemaTable(conn As DbConnection) As Integer
Using cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "SELECT value FROM WhosebugEXAMPLE"
Using reader As DbDataReader = cmd.ExecuteReader(CommandBehavior.SchemaOnly Or CommandBehavior.KeyInfo)
Using dt As DataTable = reader.GetSchemaTable()
Return CInt(dt.Select("ColumnName='value'").Single()("NumericPrecision"))
End Using
End Using
End Using
End Function
Private Sub CleanUpTestTable(conn As DbConnection)
Using cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "DROP TABLE WhosebugEXAMPLE"
cmd.ExecuteNonQuery()
End Using
End Sub
基本上,53个在bits
,15个在digits
。
float [ (n) ] Where n is the number of bits that are used to store the
mantissa of the float number in scientific notation and, therefore,
dictates the precision and storage size. If n is specified, it must be
a value between 1 and 53. The default value of n is 53.
并且在 ADO.NET 中,所有数字数据类型的 "NumericPrecision" 表示该数据类型可以存储的小数位数。
对于声明为 float
的 SQL 服务器列,INFORMATION_SCHEMA
报告数值精度为 53,而 ADO.NET SqlDataReader.GetSchemaTable()
方法报告 15 . 造成这种明显差异的原因是什么?
这是 VB 中的一个最小代码示例:
Private Sub MinimalCodeExample(conn As DbConnection)
Try
CreateTestTable(conn)
Dim precisionFromInformationSchema As Integer =
GetPrecisionFromInformationSchema(conn) ' 53
Dim precisionFromADONETSchemaTable As Integer =
GetPrecisionFromADONETSchemaTable(conn) ' 15
' Why does this assertion fail, 53 <> 15?
Debug.Assert(precisionFromInformationSchema = precisionFromADONETSchemaTable)
Finally
CleanUpTestTable(conn)
End Try
End Sub
Private Sub CreateTestTable(conn As DbConnection)
Dim cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "CREATE TABLE WhosebugEXAMPLE(value float)"
cmd.ExecuteNonQuery()
End Sub
Private Function GetPrecisionFromInformationSchema(conn As DbConnection) As Integer
Using cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "SELECT NUMERIC_PRECISION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='WhosebugEXAMPLE'"
Return CInt(cmd.ExecuteScalar())
End Using
End Function
Private Function GetPrecisionFromADONETSchemaTable(conn As DbConnection) As Integer
Using cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "SELECT value FROM WhosebugEXAMPLE"
Using reader As DbDataReader = cmd.ExecuteReader(CommandBehavior.SchemaOnly Or CommandBehavior.KeyInfo)
Using dt As DataTable = reader.GetSchemaTable()
Return CInt(dt.Select("ColumnName='value'").Single()("NumericPrecision"))
End Using
End Using
End Using
End Function
Private Sub CleanUpTestTable(conn As DbConnection)
Using cmd As DbCommand = conn.CreateCommand()
cmd.CommandText = "DROP TABLE WhosebugEXAMPLE"
cmd.ExecuteNonQuery()
End Using
End Sub
基本上,53个在bits
,15个在digits
。
float [ (n) ] Where n is the number of bits that are used to store the mantissa of the float number in scientific notation and, therefore, dictates the precision and storage size. If n is specified, it must be a value between 1 and 53. The default value of n is 53.
并且在 ADO.NET 中,所有数字数据类型的 "NumericPrecision" 表示该数据类型可以存储的小数位数。