1
I have this code to make a SELECT that returns multiple records or lines:
Public Sub ConsultaFimFerias()
Dim sqlCmd As String = "SELECT Id FROM tb_ferias WHERE Data2 < '24/03/2020' "
con = New SqlConnection(strCon.strCon2)
cmd = New SqlCommand(sqlCmd, con)
con.Open()
dr = cmd.ExecuteReader
Dim result As New ArrayList()
While dr.Read()
' Insert each column into a dictionary
Dim dict As New Dictionary(Of String, Object)
For count As Integer = 0 To dr.FieldCount - 1
dict.Add(dr.GetName(count), dr(count))
Next
' Add the dictionary to the ArrayList
result.Add(dict)
End While
dr.Close()
For Each dat As Dictionary(Of String, Object) In result
UpdateSituacaoByFerias(dat) '<----- NÃO SEI O QUE INSERIR AQUI
Next
End Sub
The result is:
id (column)
- 194
- 147
I need to do an UPDATE in another table using the SELECT results, Id column.
Public Function UpdateSituacaoByFerias(id As Integer)
strCmdUpdate = "UPDATE tb_funcionarios_ws SET situacao_contrato = '' WHERE id = " & id
con = New SqlConnection(strCon.strCon2)
cmd = New SqlCommand(strCmdUpdate, con)
Try
con.Open()
cmd.ExecuteNonQuery()
Return True
Catch ex As Exception
MsgBox("Não foi possível concectar ao banco de dados, erro: " & vbCrLf & ex.ToString, MsgBoxStyle.Information, "Erro ao concectar")
Return False
Finally
con.Dispose()
cmd.Dispose()
End Try
Return False
End Function
Anyway, how can I update every record that SELECT provides me?
your method
VerificaFerias
returns a Boolean, as expected to make aFor Each
of its integer values? You need to read the values inDatareader
and return an Array, List, etc, an object that isIEnumerable
to make the foreach.– Ricardo Pontual
see this example which is exactly what you need: https://stackoverflow.com/questions/11978127/storing-results-of-a-datareader-into-an-array-in-vb-net
– Ricardo Pontual
Thank you, @Ricardopunctual. How do I perform UPDATE with Dictionary(Of String, Object) ?
– DP WS Receptivo