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 9232161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:14:54+00:00 2026-06-18T06:14:54+00:00

I need to create a string method that takes in a string and escapes

  • 0

I need to create a string method that takes in a string and
escapes it so that it can be used in a database SQL query, for example:

"This is john's dog" ==> "This is john''s dog"
"This is a 'quoted' string" ==> "This is a ''quoted'' string"

I want my method to look something like this:

string PrepareForSQLCommand(string text)
{
    ...
} 

Anyway, I don’t know all of the characters that need to be escaped in SQL query.
I am not sure what the best approach is to do this, or if there is some
existing robust built-in stuff to do this in C#.

Apologies for not mentioning this earlier: I DO NOT HAVE THE OPTION TO USE PARAMETRIZED QUERIES.

Ted

  • 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-18T06:14:55+00:00Added an answer on June 18, 2026 at 6:14 am

    Trigger Warning. This answer is in response to the following statement:

    I do not have the option to use parametrized queries.

    Please do not up-vote this answer and please don’t accept this as the correct way of doing things. I don’t know why the OP cannot use parametrized queries, so I am answering that specific question and not recommending this is how you should do this. If you are not the OP, please read the other answer I have given. Also, please bear in mind the above constraint before down-voting. Thanks.

    End of trigger warning!

    For Microsoft SQL Server (the answer is different depending on the server) you will need to escape the single quote characters.

    '
    

    But before you escape these characters, you should reject any character not on your white-list. This is because there are lots of very clever tricks out there and white-list validation is more secure than simply escaping characters you know are bad.

    Regex whiteList = new Regex("[^'a-zA-Z0-9 -]");
    query = whiteList.Replace(query, "");
    

    For example, this would remove [ and ] characters, and ‘;’ characters. You may need to adjust the regex to match your expectations as this is a very restrictive white-list – but you know what kind of data you are expecting to see in your application.

    I hope this helps. Feel free to check out the OWASP website for more details on security and if you can find a way of using parametrized queries you’ll sleep all the better for it.

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

Sidebar

Related Questions

I want create a rest method(http put) that takes a list of string as
Hi I need to create string like this: drawtext=fontfile=/Users/stpn/Documents/Video_Experiments/fonts/Trebuchet_MS.ttf:text='content':fontsize=100:fontcolor=red:y=h/2 I want to do something
I need to create a function that receives a string and checks if the
I have a function that takes a string object name and I need the
I'm trying to create a method which takes a String parameter and then returns
How do I create a method that takes a two-dimensional array as a parameter
I need to create a keyed hash for a string of XML to send
I need to create an array by searching a string for occurrences of '['
I need an advice about how to create new string array from 4 different
I need to create a perfect hash for a list of string identifiers, so

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.