Domanda

I am trying to pass a list in string.format as the parameters to the SQL statement, but I get the following error:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

I know that I can get it to work when I list out each individual list member as the arguments, but I am wondering if there is a shortcut so I can just use the list object as the only argument.

Thanks!

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
È stato utile?

Soluzione

Try:

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top