Basically, if you’re doing it wrong, a simple example solution is like this:
Implicit:
Dim SQL As String
Dim Dados As New DataTable
SQL = "SELECT * From Diaria where data=@data and prefixo=@prefixo"
Using db = New SqlConnection("")
Using command = New SqlCommand(SQL, db)
command.Parameters.AddWithValue("@data", New DateTime())
command.Parameters.AddWithValue("@prefixo", "texto")
Dados.Load(command.ExecuteReader())
End Using
End Using
where Data is Datatable with the information, but that was really just for show. The problem with your code is because it has parameters where you were going wrong and can cause conversion problems, security problems and architecture. NET already plays the heavy role for you with the help of Parameters as exemplified in that reply, instead of New DateTime()
pass a variable of the type with the search value and so serves for all others Parameters, always pass the data type with the reference value.
A small example has been made, but it covers the exchange of an untreated data with that of the Parameters, avoiding many problems mainly conversion and handling a code most expected by the architecture.
You can also specify the type of data sent, example:
command.Parameters.Add("@data", SqlDbType.Date).Value = New DateTime()
This is more usual code:
Explicit:
Dim SQL As String
Dim Dados As New DataTable
SQL = "SELECT * From Diaria where data=@data and prefixo=@prefixo"
Using db = New SqlConnection("")
Using command = New SqlCommand(SQL, db)
command.Parameters.Add("@data", SqlDbType.Date).Value = New DateTime()
command.Parameters.Add("@prefixo", SqlDbType.NChar, 6).Value = "123456"
Dados.Load(command.ExecuteReader())
End Using
End Using
References
Examples:
data
is what kind of field in your database? andprefixo
What is the type of field in your database? (Note: in addition to doing wrong is using a wrong proposal too, forget VB 6 and try to focus on VB.Net things resemble but, the bank part for example is totally different)– novic
TABLE data date nchar(6) smallint diesel Numeric(4, 1)
– Rodrigo Merce