Results 1 to 5 of 5

Thread: [RESOLVED] using Like in sql statement in C#

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Resolved [RESOLVED] using Like in sql statement in C#

    Hi,

    Im passing a sql query using Like. It works fine when I run in Access.

    But when I try to run it thru C# it does not retrieve any data into the datatable. So its something to do on the c# end and not the database.

    this is my code:

    Code:
    string strSql = "Select * from tablename where timefield Like '* + s2 + "*'";
    timefield is of datetime datatype.
    s2 is a string value that has something like 9:45.

    What could be the problem??

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: using Like in sql statement in C#

    That's not valid C# code but I'm guessing that that's a typo. Use "%" and "_" as multi-character and single character wildcards in SQL, not the Windows wildcards "*" and "?".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: using Like in sql statement in C#

    You've got the quote characters mixed up.

    In any case, you should not be embedding variables in a query like that. You should use parameterised queries:
    Code:
    SqlCommand mycommand = new SqlCommand("select * from tablename where timefield like @s2");
    mycommand.Parameters.Add("@s2", s2);
    SqlDataReader results = mycommand.ExecuteReader()

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    83

    Re: using Like in sql statement in C#

    Hi guys,

    thanks for your input. As Jmcilhinney wrote * should be replaced with % as shown below.

    Code:
    string sSQL = "Select * from tablename where datefield Like '%" + s2 + "%'";
    I was passing the sql statment string to a method in another class. Thats why I didnt use parameters

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] using Like in sql statement in C#

    Then you should restructure your code so that you can pass parameters. Instead of passing a string to this other method you should pass a string and an array of parameters. It is just plain bad to use string concatenation to build SQL statements except in the rare cases where it's necessary. Those cases are where the values you're inserting are identifiers rather than values. In those cases you need to validate stringently to avoid SQL injection attacks.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width