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

The Archive Base Latest Questions

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

I am relatively new to C# and am developing an application that communicates with

  • 0

I am relatively new to C# and am developing an application that communicates with a local database (SQL Compact 3.5). I am fine with running standard select statements – but when it comes to getting that data into C# – I get a bit lost.

What I have so far is a ‘delete’ query that deletes all rows named ‘Master’.
Now I have worked out that I actually need to get the IDs of those rows before I delete them (for database integrity purposes). I have no problems running a standard select query – my problem is getting a selection of rows from SQL CE into a C# application (using arrays or datatables or whatever is most logical/convenient).

This is what I have at the moment, but it only returns one value, not a selection:

string sql = "select listid from list where ShortDesc='Master'";
SqlCeCommand cmdGetOldMasterId = new SqlCeCommand(sql, DbConnection.ceConnection);
int oldKey = (int)cmdGetOldMasterId.ExecuteScalar();
Console.WriteLine("Old ID: " + oldKey);

I need to be able to do perform a foreach{ } loop on each of the returned rows. Any ideas how I can do this in C#?

  • 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-18T10:50:55+00:00Added an answer on June 18, 2026 at 10:50 am

    You need to use a SqlCeDataReader instead of a ExecuteScalar if you suppose that your sql statement returns more thant one row of data

    string sql = "select listid from list where ShortDesc='Master'";
    SqlCeCommand cmdGetOldMasterId = new SqlCeCommand(sql, DbConnection.ceConnection);
    SqlCeDataReader reader = cmdGetOldMasterId.ExecuteReader();
    while(reader.Read())
    {
        Console.WriteLine(reader[0].ToString());
        // or, if your listid is an integer  
        // int listID = reader.GetInt32(0);   
    }
    

    another possibility is to use a SqlCeDataAdapter to fill a DataTable (this is less performant, but more useful if you need to process your data later in a different method)

    string sql = "select listid from list where ShortDesc='Master'";
    SqlCeCommand cmdGetOldMasterId = new SqlCeCommand(sql, DbConnection.ceConnection);
    SqlCeDataAdapter da = new SqlCeDataAdapter(cmdGetOldMasterId);
    DataTable dt = new DataTable();
    da.Fill(dt);
    
    ....
    foreach(DataRow r in dt.Rows)
    {
        Console.WriteLine(r["listid"].ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to C#/.Net. I'm developing a desktop application that requires multi
I am currently developing an application that requires random access to many (60k-100k) relatively
I am developing an iPad application and relatively new in the business. I am
I am relatively new to winforms and C# and I am developing an application
I'm relatively new to source control (at least complex source control). If I'm developing
I am relatively new to developing Cocoa applications on the Mac and come from
I'm relatively new to developing iOS apps. I'm developing a note-app where users can
I am currently developing a Rhomobile application. I have a backend database which holds
I am developing for iPad and have created a standard UISplitViewController application using the
I'm developing an application using C# and XAML and I've encountered a problem that

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.