Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27569

VS 2010 [SqlServer CE] Error creating a parameterized SELECT query

$
0
0
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:
Code:

SELECT <all records> FROM <login data table> WHERE <username field> = <username value> AND <password field> = <password value>
So far what I have tried for my SQL statement is:
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 With

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 With

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 With

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:
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 Using

Everytime the code will fail on the Using statement line. The error I get is:
Quote:

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?

Viewing all articles
Browse latest Browse all 27569

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>