What is the equivalent of Parameters.AddWithValue in the MySqlClient?
I've tried Parameters.Add and it's not working, but AddWithValue gives me an error.
Thanks
Printable View
What is the equivalent of Parameters.AddWithValue in the MySqlClient?
I've tried Parameters.Add and it's not working, but AddWithValue gives me an error.
Thanks
Unless the error is a NotSupportedException then it's simply a matter that you're doing it wrong. If you show us what you're doing and tell us what the error message is then we can probably help. In general, AddWithValue requires the name of a parameter and the value for that parameter, where the value MUST be the correct data type, e.g. you cannot pass a String if the parameter expects a number.
Here's the code I'm attempting to run.
I'm not exactly getting an error, just no result. If I put in a breakpoint at "Dim i As Integer = 0" and check the value of count it is zero. If I run the query on the table with say the firstname of "mark" I get a result of 28, so I know the query technically works, but for some reason not in vb.net.Code:Public Shared Function GetClientsByFirstName(ByVal FirstName As String) As List(Of clients)
Dim connection As MySqlConnection = ConnectionFactory.GetConnection
connection.Open()
'make a command object
Dim selectCommand As New MySqlCommand
selectCommand.Connection = connection
'count data query
selectCommand.CommandText = "SELECT " & _
"COUNT(clients.client_id) AS count " & _
"FROM clients " & _
"WHERE clients.first_name = @firstname"
selectCommand.Parameters.Add("@firstname", FirstName)
Dim reader As MySqlDataReader = selectCommand.ExecuteReader()
Dim count As Integer = Nothing
Do While reader.Read
count = reader("count").ToString
Loop
Dim i As Integer = 0
'setup progress bar
frmMain2.StatusBar.Maximum = count
frmMain2.StatusBar.Minimum = 0
frmMain2.StatusBar.Value = 0
reader.Close()
End Function
Thanks!
Count is a reversed keyword. You have to either use a different word or put it in brackets
Also, if you just need to get 1 value (the count) then why not calling ExecuteScalar method?Code:selectCommand.CommandText = "SELECT " & _
"COUNT(clients.client_id) AS [count] " & _
"FROM clients " & _
"WHERE clients.first_name = @firstname"