1

我试图将 string.format 中的列表作为参数传递给 SQL 语句,但出现以下错误:

索引(从零开始)必须大于或等于零且小于参数列表的大小。

我知道当我列出每个单独的列表成员作为参数时,我可以让它工作,但我想知道是否有一个快捷方式,所以我可以使用列表对象作为唯一的参数。

谢谢!

Public Sub updateSecurityMasterTable(ByVal params As Dictionary(Of String, String))

    Dim updateList As New List(Of String)

    Try
        updateList.Add(params.Item("ticker"))
        updateList.Add(String.Empty)
        updateList.Add(params.Item("asset_class"))
        updateList.Add(params.Item("sub_class"))
        updateList.Add(params.Item("asset_type"))
        updateList.Add(params.Item("current_price"))
        updateList.Add(params.Item("market_cap"))
        updateList.Add(params.Item("dividend_yield"))
        updateList.Add(params.Item("pe_ratio"))
        updateList.Add(params.Item("eps"))
        updateList.Add(params.Item("sector"))
    Catch ex As Exception
        Throw ex
    End Try


    Dim strSql As New StringBuilder

    strSql.Append("INSERT INTO SecurityMaster ")
    strSql.Append("(ticker, cusip, asset_class, sub_class, asset_type, current_price, market_cap, dividend_yield, pe_ratio, eps, sector) ")
    strSql.Append(String.Format("VALUES (N'{0}', N'{1}', N'{2}', N'{3}', N'{4}', N'{5}', N'{6}', N'{7}', N'{8}', N'{9}', N'{10}')", updateList))

    Try
        If checkConnection() Then
            'Do Nothing
        Else
            Me.createConnection()
        End If

        Using cmdExe As New SqlCeCommand(strSql.ToString(), conn)
            cmdExe.ExecuteNonQuery()
        End Using
    Catch ex As Exception
        Throw ex
    End Try

End Sub
4

1 回答 1

4

尝试:

String.Format("VALUES (N'{0}', N'{1}', N'{2}', N'{3}', N'{4}', N'{5}', N'{6}', N'{7}', N'{8}', N'{9}', N'{10}')", updateList.ToArray)
于 2012-02-10T03:54:11.887 回答