|
-
Mar 10th, 2011, 05:21 PM
#1
Thread Starter
Addicted Member
Parameters.AddWithValue equivalent for MySqlClient
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
-
Mar 10th, 2011, 05:38 PM
#2
Re: Parameters.AddWithValue equivalent for MySqlClient
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.
-
Mar 11th, 2011, 12:35 PM
#3
Thread Starter
Addicted Member
Re: Parameters.AddWithValue equivalent for MySqlClient
Here's the code I'm attempting to run.
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
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.
Thanks!
-
Mar 11th, 2011, 04:44 PM
#4
Re: Parameters.AddWithValue equivalent for MySqlClient
Count is a reversed keyword. You have to either use a different word or put it in brackets
Code:
selectCommand.CommandText = "SELECT " & _
"COUNT(clients.client_id) AS [count] " & _
"FROM clients " & _
"WHERE clients.first_name = @firstname"
Also, if you just need to get 1 value (the count) then why not calling ExecuteScalar method?
Last edited by stanav; Mar 11th, 2011 at 04:48 PM.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|