Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get a particular column value using C#

Posted on 2012-04-03
1
Medium Priority
?
212 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
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.
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.
Suggested Courses

810 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