我正在尝试将 CSV 文件解析为 VB6 应用程序,以便使用表单中已有的单记录更新代码来更新 SQL 表上的多条记录。 CSV 文件将有一个标题行,可用于验证进入 ADODB 记录集中正确位置的信息。在 C++ 中,你可以使用映射来表示
map<String s, int x> column
column<"First Name", -1>
column<"Last Name",-1>
然后创建一个跨逗号分隔值的计数器,其中如果第三个值是姓氏,则可以编写代码来更改 column<"Last Name",-1> 到 column<"Last Name",3> 并且如果 x != -1 在任何映射中该文件都有效,那么我将循环遍历剩余的记录并解析为使用类似的容器
strLastName = Array<column[3]>
将记录值分配给正确的变量。我对 VB6 还很陌生,如何在 VB6 中完成类似的事情以及应该使用哪些容器?到目前为止我已经
Public Sub GetImportValues()
On Error GoTo GetImportValues_Error:
Dim intFileNum As Integer
Open Path For Input As #intFileNum
Do Until EOF(intFileNum)
Line Input #intFileNum, vbCrLf
FunctionThatSavesInformationToSQL
Loop
Close #intFileNum
GetImportValues_Exit:
Exit Sub
GetImportValues_Error:
Err.Source = "frmMemberAdd.GetImportValues" & " | " & Err.Source
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
带有一个对话框,在单独的函数中使用 App.path 以字符串形式返回路径
**************************************************** ******答案略有改变 该集合符合我的要求,但我确实必须将其更改为字典,因为您无法返回集合中的项目,这使我无法比较项目并更改键,但字典可以。确保如果您使用字典,则切换项目和键。
最佳答案
如果我正确理解你的问题,你正在尝试创建一个 map (在 C# 中为 Dictionary<string, int>
)。在VB6中,您可以使用Collection
为此目的 - 它大致相当于 C# 的 Dictionary<string, object>
。它使用 String
键并将所有值存储为 Variant
。例如:
Dim oColl As Collection
Set oColl = New Collection
oColl.Add -1, "ColumnName"
Dim nColumnIndex As Long
'Get column index for column name.
nColumnIndex = oColl.Item("ColumnName")
If nColumnIndex = -1 Then
nColumnIndex = ...
'When you want to update a column index in the collection, you
'first have to remove the item and then add it back with the right
'index.
oColl.Remove "ColumnName"
oColl.Add nColumnIndex, "ColumnName"
End If
编辑 1:
关于 VB6 的一个警告:您会看到许多示例这样做:
Dim oObj As New SomeClass
可以在 VB.Net 中执行此操作,但永远不要在 VB6 中执行此操作。在单独的语句上声明并实例化该对象,因为单语句形式会生成代码 where oObj
检查 Nothing
并在每次使用前设置为一个实例。如果您使用的实例本来应该消失,这会减慢您的代码速度(不必要的检查)并产生难以发现的错误。
始终这样做:
Dim oObj As SomeClass
Set oObj = New SomeClass
...
'Clean up the object when you're done with it. Remember, there's
'no garbage collection in COM / VB6, you have to manage object
'lifetimes.
Set oObj = Nothing
另外,使用Long
而不是Integer
尽你所能 - Long
是一个 32 位整数,而 Integer
只有16位。 VB6 类型名称经常会产生误导。 Here's an old answer我的更详细一些(与您的问题并不严格相关,但很有用)。
或者,您可以围绕 .NET Dictionary
创建一个简化的包装器类并将其公开为 COM 对象:这将允许您从 VB6 调用它。这可能会(稍微)慢于 Collection
并且您的 VB6 项目需要 .NET Framework 才能运行。
编辑 2:
正如@CMaster 评论的那样,Dictionary
可从 Microsoft Scripting Runtime
获取库 - 您需要添加对它的引用才能使用它(这就是为什么我更喜欢 Collection
- 它没有依赖性)。 This answer有关于如何使用它的详细信息。
关于sql - VB6 将字符串映射到标题的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28595538/