This is how;
var zoo = new List<Zoo>();
int paramValue = 5;
var queryString = @"SELECT animalName FROM Jungle";
// Create and open the connection in a using block. This ensures that all resources will be closed and disposed when the code exits.
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand(queryString, connection);
//command.Parameters.AddWithValue("@howManyLegs", paramValue);
// Open the connection in a try/catch block. Create and execute the DataReader, writing the result set to the console window.
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}", reader[0]);
var animal = new Animal
{
AnimalName = reader[0].ToString()
};
zoo.Add(animal);
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Resources
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ado-net-code-examples
Add to favorites