mysql - 使用 vb.net 与 vba 将数组插入 mysql

标签 mysql arrays vb.net

我已将互联网上的所有数据检索到二维数组中,我知道如何使用 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

最佳答案

您提到的帖子(使用 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

关于mysql - 使用 vb.net 与 vba 将数组插入 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25412919/

相关文章:

sql - 查询返回的行太少

mysql - 带求和函数的 GROUP CONCAT

MySQL 和 Regex(检查正确名称大写)

python - 更改 Numpy 数组第 0 行中的值

java - 如何查找数组中重复的元素?

mysql - 使用 `rand()` 和 `having`

java - 从动态参数创建一个字符串数组和一个字符串变量

vb.net - 如何在 vb.Net 中设置默认表单?

vb.net - 为什么 True 等于 -1

mysql - 具有用于 Crystal Reports 的 MySQL DSN 连接的 VB.NET 应用程序要求登录信息