0

我有几个从数据库填充的字典这里是一些数据库示例数据库中的所有文件都表示为字符串

PromoStatus         3   Paid
PromoStatus         4   Escrowed
TickTransCode   &H0 Valid cashable ticket
TickTransCode   &H1 Valid restricted promo ticket
TickTransCode   &H2 Valid nonrestricted promo ticket
BIllDenom           0   1
BIllDenom           1   2

这是我的代码

Public Shared TickTransCode As New Dictionary(Of Byte, String)()
Public Shared BIllDenom As New Dictionary(Of Byte, Decimal)()
Public Shared PromoStatus As New Dictionary(Of Byte, String)()    
Dim myReader As SqlDataReader = Nothing
Dim myCommand As New SqlCommand("select * from dictionary", myConnection)
myReader = myCommand.ExecuteReader()
While myReader.Read()
    Select Case myReader.GetString(2)
        Case "PromoStatus"
           PromoStatus.Add(Convert.ToByte(myReader.GetString(3)), myReader.GetString(4))
        Case "BillDenom"
           BIllDenom.Add(Convert.ToByte(myReader.GetString(3)), Convert.ToByte(myReader.GetString(4)))
        Case "TickTransCode"
           TickTransCode.Add((myReader.GetString(3)), myReader.GetString(4))
     End Select
End While

第一个案例工作正常 3 案例我不确定正确的转换,所以 &HA 实际上应该是 10 &HF 将是 15 等等。你可能会问为什么我不只是将实际数字放在 DB 中,但十六进制数字是我从中得到的文档使用的通信流和编号,因此我需要保持不变,因此在解决数据问题时不必转换每个数字。

这是我得到的错误

Input string was not in a correct format

还有一种更好的方法可以从数据库中创建字典,我有比这个示例中要填充的字典多得多的字典,而且我真的不希望每个字典都有一个表。我使用 DB 的原因是我可以添加到字典而无需重新编译,还可以为提示和消息等添加一些额外的语言,这些语言可以在运行时加载和/或在运行时更改

4

2 回答 2

0

在您的 SQL 中,您可以执行以下操作:

SELECT CAST(SUBSTRING(HexNumber, 2, 9999) AS INT)

或在 VB 方面:

Dim tInt As Int32 = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber)
于 2012-09-11T17:18:30.787 回答
0

NumberStyles.HexNumber 不允许使用“&h”或“&H”。但是,您可以简单地删除它们:

Dim s = "&h1A"
s = s.Replace("&H", "").Replace("&h", "")
Dim n = Integer.Parse(s, Globalization.NumberStyles.HexNumber)
Console.WriteLine(n.ToString)
于 2012-09-11T18:03:52.353 回答