-1

I'm trying to get two column details like this way:

    public string GetData()
    {

        using (SqlConnection con = new SqlConnection(this.Connection))
        {
            con.Open();

            SqlCommand command = new SqlCommand("Select TITTLE,VALUE from T_PROJECTS ", con);



            // int result = command.ExecuteNonQuery();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                reader.Read();
                return reader["TITTLE,VALUE"]?.ToString();
            }

        }
    }

How can I do this?

0

2 Answers 2

1

You need to have a custom class of columns that you want to retrieve, for example,

 public class Project
        {
          public int Title { get; set; }
          public string Value { get; set; }         
        }

and then like this,

 public Project GetData()
    {

        using (SqlConnection con = new SqlConnection(this.Connection))
        {
            con.Open();
            SqlCommand command = new SqlCommand("Select TITTLE,VALUE from T_PROJECTS ", con);
            Project proObj = new Project();            
            using (SqlDataReader reader = command.ExecuteReader())
            {
                reader.Read();
                proObj.Title = reader["TITTLE"].ToString();
                proObj.Value = reader["VALUE"].ToString();
            }

        }
     return proObj;
    }
Sign up to request clarification or add additional context in comments.

8 Comments

can i do with out creating class. means like Convert string builder to json type?
Yes you can do that too
please help me to convert string bulider to jason type
@mr.cool Look at this sample stackoverflow.com/questions/5554472/…, mark as answer if the above has helped you
sure,can you please re edit this syntax sb.Append("["{tittle:).append("["{duration:"); like this way
|
1

You could also return a Tuple although I feel a custom class is a much better solution. -> https://msdn.microsoft.com/en-us/library/system.tuple(v=vs.110).aspx

public IEnumerable<Tuple<string,string>> GetData()
{
    List<Tuple<string,string> results = new List<Tuple<string,string>>();
    using (SqlConnection con = new SqlConnection(this.Connection))
    {
        con.Open();
        SqlCommand command = new SqlCommand("Select TITTLE,VALUE from T_PROJECTS ", con);
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while(reader.Read())
                results.add(new Tuple<string,string>(reader["TITLE"].ToString(),reader["VALUE"].ToString()));
        }
        return results;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.