Solved

Get a particular column value using C#

Posted on 2012-04-03
1
207 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

730 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