我的VB.NET应用程序正在将第三方网站生成的XML文件导入SQL Server表中。网站(和我的电脑(使用句点字符表示小数(例如42.015(,一切都很好。但一位欧洲用户报告称,输入的数字正乘以1000或10000的倍数。事实证明,他的计算机正在寻找逗号小数(例如42015(,当它看到XML输入时,它会将其转换为42015.00。
我使用的是DataSet.ReadXML和SqlBulkCopy.WriteToServer,我不确定在哪里可以告诉程序使用句点小数。我的代码如下:
Dim ds As New DataSet
Try
ds.ReadXml(tempfile, 0)
Catch ex As XmlException
Log($"Error reading XML: {ex.Message} with {ex.Data}")
Exit Sub
End Try
Dim columnMap As New Dictionary(Of String, String) From {
{"LOTID", "InventoryID"},
{"ITEMTYPE", "ItemType"},
{"ITEMID", "ItemNum"},
{"COLOR", "ColorID"},
{"CONDITION", "Cond"},
{"REMARKS", "LocationName"},
{"QTY", "Qty"},
{"DESCRIPTION", "Description"},
{"SUBCONDITION", "Completeness"},
{"SALE", "Discount"},
{"STOCKROOM", "Stockroom"},
{"BULK", "Bulk"},
{"BUYERUSERNAME", "Reserve"},
{"PRICE", "Price"}
}
Using conn = New SqlConnection(GetDBConnectionString)
Using sbc As New SqlBulkCopy(conn)
conn.Open()
DoSql(conn, "TRUNCATE TABLE dbo.Online_Inventories;")
For Each column As DataColumn In ds.Tables("ITEM").Columns
If columnMap.ContainsKey(column.ColumnName) Then
sbc.ColumnMappings.Add(column.ColumnName, columnMap(column.ColumnName))
End If
Next
sbc.DestinationTableName = "Online_Inventories"
sbc.WriteToServer(ds.Tables("ITEM"))
conn.Close()
End Using
End Using
导入的XML如下所示:
<ITEM>
<LOTID>217770136</LOTID>
<DATEADDED>9/20/2020 3:02:00 PM</DATEADDED>
<CATEGORY>771</CATEGORY>
<COLOR>0</COLOR>
<PRICE>11.7563</PRICE>
<QTY>1</QTY>
<BULK>1</BULK>
<IMAGE></IMAGE>
<DESCRIPTION></DESCRIPTION>
<CONDITION>U</CONDITION>
<SUBCONDITION>I</SUBCONDITION>
<ITEMTYPE>S</ITEMTYPE>
<ITEMID>41110-1</ITEMID>
<SALE>0</SALE>
<REMARKS></REMARKS>
<STOCKROOM>Y</STOCKROOM>
<MYWEIGHT>0</MYWEIGHT>
<ITEMWEIGHT>0</ITEMWEIGHT>
<DATELASTSOLD></DATELASTSOLD>
<BASECURRENCYCODE>USD</BASECURRENCYCODE>
</ITEM>
因此,在本例中,在第三行(ds.ReadXml(之后,ds("Price"(=";11.7563〃;,字符串线路sbc之后。WriteToServer,dbo的值。Online_Inventories.Price为117563.0(在这种情况下实际上是一个错误,因为Price是一个NUMERIC(9,4((
当用户的家庭文化使用逗号作为小数时,我如何让.net将句点读取为小数?谢谢
默认线程CultureInfo基于运行机器的设置区域性。默认字符串解析将使用默认的CultureInfo。您可以在执行发布的代码时更改线程CultureInfo以使用InvariantCulture(基本上是en-US(。InvariantCulture使用句点(.(作为小数点。
Dim currentCulture As CultureInfo = Threading.Thread.CurrentThread.CurrentCulture
Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture
' ***
' insert your code here
' ***
Threading.Thread.CurrentThread.CurrentCulture = currentCulture