1

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,

2
  • 2
    sounds like you need a basic data access tutorial. Not something this site can provide. Commented Feb 20, 2012 at 22:50
  • 1
    well, providing an example would work as an answer then. Commented Feb 20, 2012 at 23:24

1 Answer 1

2

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 ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.