Task - Reading Tables using MySqlDataAdapter
We are working on the employees
table from the last task. You have a program given below that reads some columns from a table. It uses the MySqlDataReader
object to read and display the data. Your task is to read and display the exact same data using MySqlDataAdapter
instead.
decimal
data type in C#. The decimal
data type can store decimal numbers of higher precision than both float
and double
. It is suitable for representing financial and monetary values and calculations.
This task doesn't have test cases. If you successfully manage to output the correct data using the DataAdapter then it means your solution is correct. You can also verify the correctness of your solution by comparing it with the solution given below.
DataSet
object to store the retrieved data.
using System;
using System.Data;
using System.Reflection.PortableExecutable;
using MySql.Data.MySqlClient;
class Program
{
static void Main(string[] args)
{
string connectionString = "host=your_host;port=your_port;user=your_user;password=your_pwd;database=your_db";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// Change code below this line
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM Employees;", connection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
DataTable table = dataset.Tables[0];
foreach(DataRow row in table.Rows)
{
int id = (int) row["EmployeeID"];
string firstName = (string) row["FirstName"];
string lastName = (string) row["LastName"];
int age = (int) row["Age"];
string gender = (string) row["Gender"];
string department = (string) row["Department"];
string position = (string) row["Position"];
decimal salary = (decimal) row["Salary"];
Console.WriteLine($"{id}, {firstName} {lastName}, {age}, {gender}, {department}, {position}, {salary}");
}
// Change code above this line
}
}
}
Tudo estava claro?
Conteúdo do Curso
Introduction to .NET with C#
2. Asynchronous Programming and Introduction to Web Services
3. Introduction to Databases with .NET
Introduction to .NET with C#
Task - Reading Tables using MySqlDataAdapter
We are working on the employees
table from the last task. You have a program given below that reads some columns from a table. It uses the MySqlDataReader
object to read and display the data. Your task is to read and display the exact same data using MySqlDataAdapter
instead.
decimal
data type in C#. The decimal
data type can store decimal numbers of higher precision than both float
and double
. It is suitable for representing financial and monetary values and calculations.
This task doesn't have test cases. If you successfully manage to output the correct data using the DataAdapter then it means your solution is correct. You can also verify the correctness of your solution by comparing it with the solution given below.
DataSet
object to store the retrieved data.
using System;
using System.Data;
using System.Reflection.PortableExecutable;
using MySql.Data.MySqlClient;
class Program
{
static void Main(string[] args)
{
string connectionString = "host=your_host;port=your_port;user=your_user;password=your_pwd;database=your_db";
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
// Change code below this line
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM Employees;", connection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
DataTable table = dataset.Tables[0];
foreach(DataRow row in table.Rows)
{
int id = (int) row["EmployeeID"];
string firstName = (string) row["FirstName"];
string lastName = (string) row["LastName"];
int age = (int) row["Age"];
string gender = (string) row["Gender"];
string department = (string) row["Department"];
string position = (string) row["Position"];
decimal salary = (decimal) row["Salary"];
Console.WriteLine($"{id}, {firstName} {lastName}, {age}, {gender}, {department}, {position}, {salary}");
}
// Change code above this line
}
}
}
Tudo estava claro?