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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:09:22+00:00 2026-05-25T15:09:22+00:00

I am trying to implement an API with the following signature: public static List<string>

  • 0

I am trying to implement an API with the following signature:

public static List<string> SearchDatabase(
    string column,
    string value);

The implementation of the API needs to construct an SQL SELECT query with a WHERE clause that uses a column name specified in the column parameter.

The query:

string query =
    string.Format(
        "SELECT Name, @Column " +
        "FROM Db1 " +
        "INNER JOIN Db2 ON Db1.Id = Db2.Id " +
        "WHERE @Column = @Value");

The SQL command and parameters:

SqlCommand selectCmd =
    new SqlCommand(
        query,
        connection);
selectCmd.CommandTimeout = SqlCommandTimeout;
selectCmd.Parameters.AddRange(
    new SqlParameter[]
{
    new SqlParameter("@Column", column),
    new SqlParameter("@Value", value)
});

And I execute it like this:

SqlDataReader sqlDataReader = selectCmd.ExecuteReader();
while (sqlDataReader.Read())
{
    // ...
}

The problem is that sqlDataReader doesn’t return any rows, so I don’t go into the while loop above.

However, if I change the last line of the query above from:

"WHERE @Column = @Value");

to

"WHERE Vendor = @Value");

(i.e. hardcode the column name to ‘Vendor’) then it works.

My understanding from research I have done is that it’s not possible to pass column names as parameters, but only values we are querying on. But, it does appear to let me use the @Column parameter in the SELECT clause, just not the WHERE clause.

I don’t want to resort to dynamic SQL because of the issues with SQL injection. Is there another way around this?

  • 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-25T15:09:22+00:00Added an answer on May 25, 2026 at 3:09 pm

    Unfortunately table names, column names cannot be parametrized. Because you know the structure of the table you could have a whitelist of possible column names in this parameter and then use string concatenation for it to avoid SQL injection:

    "WHERE " + Sanitize(column) + " = @Value");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to implement a REST API for an application we have now.
I am trying to implement google map api into one of my web page
I'm trying to implement PayPal Adaptive Payments API. Into the headers I should include
I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).
I'm trying to implement some sort of API on tornado and I have such
I'm trying to implement Devise but new to it. When I request .../api/v1/projects.json, I
I'm trying to implement a RESTful API in Perl. My current idea is to
I'm trying to implement a server-side API method that allows a batch of API
I've been trying to implement a Gearman worker with their C API (libgearman). But
I'm trying to implement an API that performs some really lengthy tasks using a

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.