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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:28:49+00:00 2026-05-30T03:28:49+00:00

I’m relatively new to C# and SQL work and I currently have an application

  • 0

I’m relatively new to C# and SQL work and I currently have an application that I’m working on which has a few queries for one table all of which are used under different circumstances, e.g on a particular event update this or for example select this to see if it matches that etc

The reason I ask however is there a way to combine these queries as they all query the same table or are they best left seperate?

The other thing which is confusing me is how to get a particular value of a particular column in a select query? I’m not quite sure how to go about accessing that using C# code, any simple examples greatly appreciated.

Many thanks,

  • 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-30T03:28:51+00:00Added an answer on May 30, 2026 at 3:28 am

    If I’m understanding you correctly, you have one query which you use in several different places in your code – each time to access the same information, just under different circumstance? And you’re not sure how to get the particular column data you’re after?

    As has been mentioned, a beginner data access tutorial would probably be good reading. But here’s some quick and dirty info to get you nudged in the right direction. Most of this is either one of several ways you could do this (in which you should know the differences), is my typically preferred way, or I just thought would be most understandable for a complete newbie – Do look at a couple tutorials after this!

    First off, if you’re using the same query over and over, I prefer to put it into a stored procedure if there are several parameters passed to SQL. You should use parameters, always (see example), to avoid data injection and type casting issues. I didn’t know this when I first started, and thus got into the bad habit of not using parameters – which was very hard to break… so start now! Ultimately you’re getting a dataset back from the database… that set can represent anything from a single column of data to an entire database worth of data essentially – so you have to navigate that dataset of results to get at the one(s) you actually want in each particular case (like filling a textbox with one value, and another textbox with another value… both values are in the same dataset, there’s no need to query your DB for each textbox, just efficiently query it once and you should have the values you need.)

    Animals_Table:

    AnimalID, Name  , Type
    1       , Frog  , Amphibian
    2       , Snake , Reptile
    3       , Bear  , Mammal
    3       , Lion  , Mammal
    

    In C#:

            SqlCommand myCommand = new SqlCommand(); //The command you intend to pass to SQL
            DataSet myDataSet = new DataSet(); //The Dataset you'll fill and retrieve info from
            SqlConnection myConnection = new SqlConnection(Properties.Settings.Default.yourConnectionString); // I prefer my connection string to be in my project's properties. Don't put sensitive info like passwords here though
    
    
            myCommand.CommandType = CommandType.Text; // In this case we're using a text command, not stored procedure.  That means you're typing out your command, as a string, in c# (next line)
            myCommand.CommandText = "SELECT * FROM Animals_Table WHERE Type=@Type"; // @ denotes a parameter, in this case named Type
            myCommand.Parameters.AddWithValue("Type", "Mammal"); //You can also do Add instead of AddWithValue - this lets/forces you to input the type information manually.  It's more of a pain, but can resolve problems if c# doesn't make right assumptions
            myConnection.Open();  // Open your connection
            Command.Connection = myConnection;  // Plug that connection into your command
            SqlDataAdapter adapter = new SqlDataAdapter(Command); // Plug that command into a data adapter
            adapter.Fill(myDataSet); // populate your DataSet
    
            // Now you can use the data you've retrieved (your DataSet)
            textboxReturnedAnimalName1.Text = myDataSet.Tables[0].Rows[0]["Name"]; //You want the Name field from SQL, for the first table returned, and the first row in that table
    

    Since the dataset you retrieved could have multiple tables, each with multiple rows, you have to specify which you’re trying to access (typically the first/only table, and for this simple example, the first row. You can also make a for loop and iterate through the rows by replacing Rows[0] with an int variable that you increment each loop.

    I haven’t actually compiled and tested the above code, but I believe it should work. If not, the basic concept is there at least and I’m sure you can run with that and fix any typos I might have had 😉

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.