Solved

Get a particular column value using C#

Posted on 2012-04-03
1
203 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
Comment Utility
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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now