Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:38:43+00:00 2026-06-09T21:38:43+00:00

Suppose I’m querying a Sql Server DB for row count based on a LIKE

  • 0

Suppose I’m querying a Sql Server DB for row count based on a LIKE comparison on a column whose value is supplied via windows form text input. Using parameters here is important due to possible user input leading to injection. Eventually I’m to execute member function ExecuteScalar() on an instantiated SqlCommand object I named cmd, but first I’ve to add the parameter. For example:

cmd.Parameters.AddWithValue("@param1", textBox1.Text); 

Sql Server uses the special character % as a wildcard match for the LIKE comparison. What I wanted to do was allow the user to use * for wildcards instead. Hence, a simple replace:

textBox1.Text.Replace('*','%');

The problem is I run into issues with values containing special symbols % and _. One way to search for a literal % rather than use it as a wildcard is to enclose it in square brackets: [%].

So, now my replace has to become:

textBox1.Text.Replace("%","[%]").Replace("_","[_]").Replace('*','%');

Order is important here as well, since if the last Replace were made sooner the % would be treated incorrectly.

I’m not sure I’ve covered all my bases, are there other characters I need to worry about here? Does this really protect from injection? Is there some other preferred way of doing this?

An example query might be something like this:

SELECT COUNT(*) FROM [MyTable] WHERE [Column1] = @param1

Where MyTable is your table name, and Column1 is a valid column name within MyTable. We can assume that Column1 is some nvarchar type.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:38:45+00:00Added an answer on June 9, 2026 at 9:38 pm

    You shouldn’t really need anything else, but I’d make sure to test oddball things from user input. Some characters you haven’t accounted for have special meaning in LIKE:

    ^
    -
    

    Since you’re passing a parameter into the statement and not blindly appending the string, there should be little danger of injection, but you may want to try variations of user input such as:

    foo'; DELETE dbo.[UnimportantTable];
    foo''; DELETE dbo.[UnimportantTable];
    foo''''; DELETE dbo.[UnimportantTable];
    

    Again, I’m not sure if you’re vulnerable because I can’t see the whole thing, but I do think it’s very easy to construct a variety of tests so that you know all of the potential outcomes with a wide sampling of potential inputs.

    As @Bryan pointed out, certainly a good way to limit the risk is to connect using a login that has very explicit read-only permissions only on the objects you want them to be able to read. Then even if they do exploit some hole in your scaffolding, getting in doesn’t buy them much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have a grid of squared defined like so in a class: Square
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose a mysql 5.5 server uses the following configuration for storing innodb data innodb_data_file_path
Suppose I have a table like: Tab(MyDate datetime null, MyIs bit null, ...) Then
suppose I have a html markup like this: <div> <p> this is the parent
Suppose a scenario like below : Class User {} // User has Admin Role
Suppose there's a system like codepad.org - a black box that can accept C++
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.