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

  • SEARCH
  • Home
  • 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 398977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:48:05+00:00 2026-05-12T16:48:05+00:00

I want to take a poorly designed SQL statement that’s embedded in C# code

  • 0

I want to take a poorly designed SQL statement that’s embedded in C# code and rewrite it as a stored procedure (presumably), and am looking for an appropriate means to address the following pattern:

sql = "SELECT <whatever> FROM <table> WHERE 1=1";

if ( someCodition.HasValue )
{
    sql += " AND <some-field> = " + someCondition.Value;
}

This is a simplification. The actual statement is quite long and contains several such conditions, some of which include INNER JOIN‘s to other tables if the condition is present. This last part is key, otherwise I’d probably be able to solve all of them with:

WHERE <some-condition-value> IS NULL OR <some-field> = <some-condition-value>

I can think of a few possible approaches. I’m looking for the correct approach.

Edit:
I don’t want to perform concatenation in C#. I consider this a serious compromise to security.

  • 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-05-12T16:48:05+00:00Added an answer on May 12, 2026 at 4:48 pm

    If I understand the question properly, the idea is to replace a whole section of code in C# in charge of producing, “long hand”, a specific SQL statement corresponding to a list of search criteria, by a single call to a stored-procedure which would, SQL-side, use a generic template of the query aimed at handling all allowed combinations of search criteria in a uniform fashion.

    In addition to the difficulty of mapping expressions evaluated on the application-side (eg. someCondition.HasValue) to expressions evaluated on the SQL-side (eg “some-condition-value”), the solution you envision may be logically/functionally equivalent to a “hand-crafted” SQL statement, but slower and more demanding of SQL resources.

    Essentially, the C# code encapsulates specific knowledge about the “physical” layout of the database and its schema. It uses this info to figure-out when a particular JOIN may be required or when a particular application-level search criteria value translate to say a SQL “LIKE” rather than an “=” predictate. It may also encaspsulate business rules such as “when the ZIP code is supplied, search by that rather than by State”.

    You are right to attempt and decouple the data model (the way the application sees the data) from the data schema (the way it is declared and stored in SQL), but the proper mapping needs to be done somehow, somewhere.
    Doing this at the level of the application, with all the expressive power of C# as opposed to say T-SQL, is not necessarily a bad thing, provided it is done
      – in a module that is independent of other features of the application
    and, where practical,
      – it is somewhat data/configuration-driven as so to allow small changes in the data model (say the addition of a search criteria) to be implemented by changing a configuration file, rather than plugging this in somewhere in the middle of a long series of C# conditional statements.

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

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One of my favorite hobbies is Reverse Engineering. It requires… May 13, 2026 at 12:07 am
  • Editorial Team
    Editorial Team added an answer If you build the CMS yourself you have the great… May 13, 2026 at 12:07 am
  • Editorial Team
    Editorial Team added an answer Here is the possible solution: http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html Hope that helps :) May 13, 2026 at 12:07 am

Related Questions

I want to take a poorly designed SQL statement that's embedded in C# code
I'm importing from a CSV and getting data roughly in the format { 'Field1'
I want to take a floating point number in C++, like 2.25125, and a
I want to take a snapshot of a view (WebView) or, if that is
I want to take a picture or select an existing picture from the users

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.