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?
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 ...
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? 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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With