Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get a particular column value using C#

Posted on 2012-04-03
1
206 Views
Last Modified: 2012-04-03
Hello,

I want to to retrieve a column value from a table.
Thanks for help.
string commandtext = "select column1  FROM [t1]";
            string[] list = DataAccess.ExecuteReader(commandtext).ToArray();
 public static List<string> ExecuteReader(string commandtext)
        {
            List<string> sVal = new List<string>();
            try
            {
                using (SqlConnection cnn = new SqlConnection(conn))
                {
                    cnn.Open();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection = cnn;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = commandtext;

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                              // here need help.
                        }
                    }
                    cnn.Close();
                }
            }
            catch (Exception ex)
            {

            }
            return sVal.ToList().Distinct().ToList();
        }

Open in new window

0
Comment
Question by:zhshqzyc
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37803568
Please try inserting the following code:
        // here need help.
        while (reader.Read())
        {
            sVal.Add(reader["column1"].ToString());
        }

Open in new window


I hope this helps.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question