SQL Char compare

Discussion in 'ASP .Net' started by Lam, Feb 17, 2005.

  1. Lam

    Lam Guest

    hi

    I using C# to write a ASP.NET page
    and I need to compare a string variable to a Char field in the SQL table in
    SQL Server 2000
    how can I write a SQL statement to do that?\

    I try to use "SELECT Name from Employee Where pass="+password
    it shows me an "Invalid column name error"

    How can I fix that ?

    Thanks
    Lam, Feb 17, 2005
    #1
    1. Advertising

  2. You must enclose the string into single quotes:

    "SELECT Name from Employee Where pass='"+password + "'"

    If password contains a quote, then you must double it before using it. (On
    many occasions, you can also use double quotes instead of single quotes but
    this require an option to be set on the SQL-Server side and I'm not sure if
    ADO.NET will take it.)

    If you don't want to get mixed with single quote, two single quotes and
    double quote, then the best way is to use a parameter:

    "SELECT Name from Employee Where pass=@password "

    Of course, in the later case, you must know how to use the parameters
    collection of ADO.NET.

    S. L.

    "Lam" <> wrote in message
    news:...
    > hi
    >
    > I using C# to write a ASP.NET page
    > and I need to compare a string variable to a Char field in the SQL table
    > in
    > SQL Server 2000
    > how can I write a SQL statement to do that?\
    >
    > I try to use "SELECT Name from Employee Where pass="+password
    > it shows me an "Invalid column name error"
    >
    > How can I fix that ?
    >
    > Thanks
    >
    >
    >
    >
    >
    Sylvain Lafontaine, Feb 17, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    546
  2. wwj
    Replies:
    24
    Views:
    2,500
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    470
    Tristan Miller
    Jan 17, 2004
  4. jamx
    Replies:
    11
    Views:
    831
    Barry Schwarz
    Mar 12, 2006
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,034
    Ian Collins
    May 9, 2006
Loading...

Share This Page