【问题标题】:insert an array into mysql using vb.net vs vba使用 vb.net vs vba 将数组插入 mysql
【发布时间】:2014-10-14 07:04:55
【问题描述】:

我已将所有来自互联网的数据检索到二维数组中,我知道如何使用 vba 记录集以及使用循环过滤和更新。以下是vba中的部分代码。

难题在这里:

Using cmd As New MySqlCommand("INSERT INTO `calls` (`CID`, `ctype`) VALUES (@CID, @ctype)", cnn)

这使我无法在数组中使用任何循环并相应地更新。

cmd.Parameters.Add ('@CID').value = arrValue(i,j)

我希望这可以通过以下方式完成:

for x = 0 to ubound(arrValue,0)
      for y = 0 to ubound(arrValue,1)
          .fields(arrHeader(y) = arrValue(x,y)
      next y
   next x

说i,第n个要更新的,j = header的值


vba 的提取:

    With rs
            'Worksheets(strWsName).Activate
            'iLastRow = Worksheets(strWsName).Cells(65535, 1).End(xlUp).row              'column B, column 2
            For i = 0 To UBound(arrValue, 1)
                    Debug.Print "Updating " & i + 1 & " of " & UBound(arrValue, 1) + 1 & " news ..." ' of " & strCodeAB & " ..."
                    'Start looping the news row
                    strNewsID = arrValue(i, 1) 'Worksheets(strWsName).Range(ColRefNewsID & i).Value
                    If strNewsID = "" Then
                        Debug.Print i - 1 & " news is updated to database"
                        i = UBound(arrValue, 1)
                    Else
                        strFilter = "fID='" & strNewsID & "'"
                        rs.Filter = strFilter
                        If rs.EOF Then
                            .AddNew 'create a new record
                            'add values to each field in the record
                            For j = 0 To UBound(arrTitle_ALL)
                                '20140814
                                strFieldValue = .Fields(arrAccessField(j))
                                strNewValue = arrValue(i, j)
                                If IsNull(strFieldValue) And strNewValue = "" Then
                                Else
                                    .Fields(arrAccessField(j)) = arrValue(i, j) 'Worksheets(strWsName).Cells(i, j + 1).Value
                                End If
                            Next j
                            On Error Resume Next                                        '***************
                            .Update 'stores the new record
                            On Error GoTo 0
                            iCounterNewsAdded = iCounterNewsAdded + 1
                            glcounterNewsAdded = iCounterNewsAdded
                        Else

下面的帖子似乎与我的要求相似,但我不知道该怎么做。 [参考]passing an Array as a Parameter to be used in a SQL Query using the "IN" Command

【问题讨论】:

    标签: mysql arrays vb.net


    【解决方案1】:

    您提到的帖子(使用 IN() 命令)在您的 WHERE 子句中有效,但它对值无效。 MySQL 无法传递数组,因此您需要遍历数组并运行多个 INSERT 语句:

    for y = 0 to ubound(arrValue,1)
        cmd.Parameters.AddWithValue(@CID,arrValue(0,y))
        cmd.Parameters.AddWithValue(@ctype,arrValue(1,y))
        cmd.ExecuteNonQuery
    next y
    

    【讨论】:

    • 是否可以像使用 vba like .fields('field name') = arrvalue(x,y) 一样使用数据集,就像我可以在 MySQL 中使用 vba 一样。
    • MySQL 不提供传递数据集/数据表或数组的方法。另外,你的要求是没有意义的。您希望单个记录/列中有 X 个值。也许你想要一个逗号分隔的字符串。顺便说一句,我已将逗号分隔的字符串传递给 SP,然后在 SP 中解析它们并创建多个记录,但没有其他方法。
    • 为澄清起见,X 不是新闻-> arrValue(X,1-13),表示第 x 条新闻,14 个字段的 14 个值。那个目前在我的 vba 到 mysql 中运行良好,我明白为什么只要代码运行良好就没有任何意义。此外,对于插入,你的意思是我可以把我的数组变成每个没有。将新闻转换为逗号分隔的字符串,以便我可以执行以下操作: strAllVALues = "'ID1','Equity'" ("INSERT INTO calls (CID, ctype) VALUES (strAllValues)", cnn ) 感谢您的贡献。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多