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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:08:11+00:00 2026-06-13T13:08:11+00:00

Suppose that I want to create an SQL SELECT statement dynamically with reflection on

  • 0

Suppose that I want to create an SQL SELECT statement dynamically with reflection on primary key. I search in the table for primary keys and then, I make the statement.

Problem is, I don’t know the type of fields that compose the primary key before getting them. So, if it’s a string or date, I must add quotation marks but not if it’s an int.

Atm, I am doing like that :

var type = field.GetType().Name;
if (type.ToLower().StartsWith("string") || type.ToLower().StartsWith("date"))
{
    field = "\"" + field + "\"";
} else if (type.ToLower().StartsWith("char"))
{
    field = "\'" + field + "\'";
}

With this code, I can handle some SQL types but there are a lot more.

My problem is that it’s combined with LinQ. I got a DataContext object and a generic type table from the context. And context.ExecuteQuery only allows parameters to be passed has values. I also tried with Dynamic LinQ but I got the same problem

Does anyone know a better solution?

  • 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-13T13:08:12+00:00Added an answer on June 13, 2026 at 1:08 pm

    That is simply the wrong way to write SQL. Parameterize it and all these problems evaporate (as do problems with “which date format to use”, etc. And of course the biggie: SQL injection.

    Then it just becomes a case of adding @whatever into the TSQL, and using cmd.Parameters.AddWithValue("whatever", field) (or similar).


    Update (from comments): since you mention you are using DataContext.ExecuteQuery, this becomes easier: that method is fully parameterized using the string.Format convention, i.e.

    object field = ...;
    var obj = db.ExecuteQuery<SomeType>(
          "select * from SomeTable where Id = {0}", field).ToList(); // or Single etc
    

    No string conversions necessary.

    (the last parameter is a params object[], so you can either pass multiple discreet terms, or you can populate an object[] and pass that, if the number of terms is not fixed at compile-time; each term in the array maps by (zero-based) index to the {0}, {1}, {2}… etc token in the query)

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

Sidebar

Related Questions

Suppose I have a myTest.sql scripts, which contains thousands of create table blahblah statements.
I want to dynamically generate bands, that will then be grouped in reports. My
Ok another question in VHDL. Below is my code. Suppose that I want my
Actually i want that, suppose i have a colorcode #FF3366. How can i examine
Suppose I have a Python class that I want to add an extra property
Suppose I want to put objects that identify a server into a stl set
Suppose I want to find the longest subsequence such that first half of subsequence
Suppose I want to write an app for Android OS that is not going
Suppose you have a module that you know is safe. You want to mark
Example: Suppose in the following example I want to match strings that do not

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.