I'm having a bit of an issue creating a parameterized SELECT query to preform against my DataTable. I am using the SqlServer Compact Edition too. What I'm trying to do is something along these lines:
So far what I have tried for my SQL statement is:
The one I thought was right is the first one I tried.
After I setup the connection and command, I try to preform the query using this:
Everytime the code will fail on the Using statement line. The error I get is:
There was an error parsing the query. [ Token line number = 1,Token line offset = 27,Token in error = user ]
Is my sql statement wrong or am I doing something else wrong that would give me that error?
Code:
SELECT <all records> FROM <login data table> WHERE <username field> = <username value> AND <password field> = <password value>Code:
Dim sql As String = "SELECT * " & _
"FROM login " & _
"WHERE user = '@username'" & _
"pass = '@password'"
Dim cmd As New SqlCeCommand(sql, con)
With cmd
.Parameters.AddWithValue("@username", user)
.Parameters.AddWithValue("@password", pass)
End WithCode:
Dim sql As String = "SELECT * " & _
"FROM login " & _
"WHERE user = @username" & _
"pass = @password"
Dim cmd As New SqlCeCommand(sql, con)
With cmd
.Parameters.AddWithValue("@username", user)
.Parameters.AddWithValue("@password", pass)
End WithCode:
Dim sql As String = "SELECT * " & _
"FROM login " & _
"WHERE user = ?username" & _
"pass = ?password"
Dim cmd As New SqlCeCommand(sql, con)
With cmd
.Parameters.AddWithValue("?username", user)
.Parameters.AddWithValue("?password", pass)
End WithAfter I setup the connection and command, I try to preform the query using this:
Code:
con.Open()
Using reader As SqlCeDataReader = cmd.ExecuteReader
Dim hasrows As Boolean = reader.Read
If hasrows AndAlso reader.Item("admin").ToString = "True" Then
'Admin login
ElseIf hasrows Then
'Normal login
Form1.Show() : Me.Close()
Else
'Failed login
MessageBox.Show("Invalid Login. Please try again.", Me.Text, MessageBoxButtons.OK)
End If
End UsingQuote:
There was an error parsing the query. [ Token line number = 1,Token line offset = 27,Token in error = user ]