Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to implement method that return list of SQL result?

using (var connection = new SqlConnection("user id=xxx;" +
                   "password=xxx;server=xxx;" +
                   "Trusted_Connection=yes;" +
                   "database=xxx; " +
                   "connection timeout=30"))
{
    connection.Open();
    string sql = "SELECT * FROM testTable";
    using (var command = new SqlCommand(sql, connection))
    {
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                .
                .
                .
                .
                .
            } 
        }
    }
}

The code above can be used to retrive the list of rows of records and display them in my C# console application but it will be very messy to be in Main method. I'm wondering how to handle this chunk of records and store them into a list or something by calling a method. Will it be possible in C# console application?

like image 416
SuicideSheep Avatar asked Sep 12 '13 03:09

SuicideSheep


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

Not knowing what is in your database table, I will make up an example:

Account class:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

Now you can have your reader loop populate each Person, like this:

while (reader.Read())
{
    var person = new Person();
    person.FirstName = reader["FirstName"].ToString();
    person.LastName = reader["LastName"].ToString();
    person.Age = Convert.ToInt32(reader["Age"]);
}

Finally, we want to build a list of Person objects to return, like this:

var listOfPerson = new List<Person>();

while (reader.Read())
{
    var person = new Person();
    person.FirstName = reader["FirstName"].ToString();
    person.LastName = reader["LastName"].ToString();
    person.Age = Convert.ToInt32(reader["Age"]);

    listOfPerson.Add(person);
}

return listOfPerson;

Complete code:

public List<Person> LoadPeople()
{
    var listOfPerson = new List<Person>();

    using (var connection = new SqlConnection("user id=xxx;" +
               "password=xxx;server=xxx;" +
               "Trusted_Connection=yes;" +
               "database=xxx; " +
               "connection timeout=30"))
    {
        connection.Open();
        string sql = "SELECT * FROM testTable";
        using (var command = new SqlCommand(sql, connection))
        {
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var person = new Person();
                    person.FirstName = reader["FirstName"].ToString();
                    person.LastName = reader["LastName"].ToString();
                    person.Age = Convert.ToInt32(reader["Age"]);

                    listOfPerson.Add(person);
                }
            }
        }
    }

    return listOfPerson;
}
like image 127
Karl Anderson Avatar answered Oct 29 '22 22:10

Karl Anderson