Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

Get a particular column value using C#

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

ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial