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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:26:16+00:00 2026-05-28T18:26:16+00:00

My problem is simple in nature- query a database, and write output to a

  • 0

My problem is simple in nature- query a database, and write output to a set of files (~1000).

I have a SQL query I need to execute from C# on SQL Server. The query is fairly simple except for one of the values in an IN clause.

As an example, the query may look like:

SELECT 
    ID, Name
FROM
    MyTable
WHERE
    SomeValue IN (/* list of values */)

The list of values is around 300 strings that are 6 characters each. The values will ultimately look something like (“A00001”, “A00002”, etc), and are specified as input by the user of the application.

My initial thought was to build the query out as a long string, with each of the values aggregated together. This doesn’t seem like the proper approach however, and seems to be error prone. My next thought was to use a stored procedure, but I’m unsure of how the data should be passed into it. How should this query be built in an efficient way where the data can be reliably passed in?

My other option is to read in ALL data from SQL Server and then filter the results in C# as I receive them. If I do this, however, I’ll get around 9 million records which seems to be too many to read into memory as a data set. Because I’m writing files after parsing ~5000 records, I don’t want to keep a data reader connected to the database while I chew through the data I’m receiving.

What is the best approach?

  • 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-28T18:26:17+00:00Added an answer on May 28, 2026 at 6:26 pm

    Here is one way to do it using xml:

    C#:

    StringBuilder sb = new StringBuilder();
    sb.AppendLine("<SelectedValues>");
    
    foreach (string s in userSelectedValues)
      sb.AppendFormat("<row val=\"{0}\" />{1}", s, Environment.NewLine);
    sb.AppendLine("</SelectedValues>");
    

    Pass this sb.ToString() AS XML TO your stored procedure

    SQL:

    CREATE PROCEDURE dbo.MyStoredProcedure
    (
      @UserSelectedValuesXml XML
    )     
    
    SELECT * 
    FROM dbo.MyTable
    WHERE Id IN ( SELECT ref.value('@val', 'varchar')
                  FROM @UserSelectedValuesXml.nodes('/SelectedValues/row') AS T(ref))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

simple problem with a simple question. I have a string, in C# i can
a possibly simple problem, but weird why I have no idea how to do
My problem is simple: I have a long list of elements that I want
Seems like a simple problem: I have an SVN repo inside our firewall. I
I have a query from a web site that takes 15-30 seconds while the
I have a computational algebra task I need to code up. The problem is
I have a bit of an interesting problem where I need to create an
Really simple problem: I want to split a connection string into its keyword /
Simple problem that I can't figure out... How can I print a '%' character
Simple problem (I think): I want to be able to invoke a click method

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.