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 - Using INSERT
In the last task, we created a table that stores the products of a grocery store. They contact you again to fix the previous table making sure that the id
of each item in the products table is unique. They also want you to insert the following data into the table:
Your task is to:
- Create a new table called
store
and, this time, make theid
column a primary key and set it to automatically increment; - Insert the data given in the
groceryItemsData
array into the new table.
PRIMARY KEY
keyword for making a column a primary key. 2. The
AUTO_INCREMENT
is used for making a column automatically take up incremented values. 3. Use the
INSERT INTO
statement for inserting data into the table.
using System;
using MySql.Data.MySqlClient;
public class CRUD_Task_2
{
// Modify the connection string here
public static string connectionString = "server=localhost;port=7777;user=root;password=Th!sIsMyPassword;database=school;";
public static object[,] groceryItemsData = new object[,]
{
{ "Bananas", "Fruits", 0.99f, 150 },
{ "Apples", "Fruits", 1.49f, 100 },
{ "Carrots", "Vegetables", 0.79f, 200 },
{ "Potatoes", "Vegetables", 1.29f, 180 },
{ "Milk", "Dairy", 2.49f, 80 },
{ "Eggs", "Dairy", 1.99f, 120 },
{ "Bread", "Bakery", 1.99f, 90 },
{ "Chicken", "Meat", 4.99f, 50 },
{ "Rice", "Grains", 3.99f, 120 },
{ "Pasta", "Grains", 1.49f, 150 }
};
static void Main(string[] args)
{
// Write code below this line
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string cmdText = "CREATE TABLE store (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), category VARCHAR(100), price FLOAT, stock_quantity INT);";
MySqlCommand cmd = new MySqlCommand(cmdText, connection);
cmd.ExecuteNonQuery();
for (int i = 0; i < 10; ++i)
{
string name = (string)groceryItemsData[i, 0];
string category = (string)groceryItemsData[i, 1];
float price = (float)groceryItemsData[i, 2];
int stock_quantity = (int)groceryItemsData[i, 3];
cmdText = $"INSERT INTO store (name, category, price, stock_quantity) VALUES ('{name}', '{category}', {price}, {stock_quantity});";
cmd = new MySqlCommand(cmdText, connection);
cmd.ExecuteNonQuery();
}
}
// Write 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 - Using INSERT
In the last task, we created a table that stores the products of a grocery store. They contact you again to fix the previous table making sure that the id
of each item in the products table is unique. They also want you to insert the following data into the table:
Your task is to:
- Create a new table called
store
and, this time, make theid
column a primary key and set it to automatically increment; - Insert the data given in the
groceryItemsData
array into the new table.
PRIMARY KEY
keyword for making a column a primary key. 2. The
AUTO_INCREMENT
is used for making a column automatically take up incremented values. 3. Use the
INSERT INTO
statement for inserting data into the table.
using System;
using MySql.Data.MySqlClient;
public class CRUD_Task_2
{
// Modify the connection string here
public static string connectionString = "server=localhost;port=7777;user=root;password=Th!sIsMyPassword;database=school;";
public static object[,] groceryItemsData = new object[,]
{
{ "Bananas", "Fruits", 0.99f, 150 },
{ "Apples", "Fruits", 1.49f, 100 },
{ "Carrots", "Vegetables", 0.79f, 200 },
{ "Potatoes", "Vegetables", 1.29f, 180 },
{ "Milk", "Dairy", 2.49f, 80 },
{ "Eggs", "Dairy", 1.99f, 120 },
{ "Bread", "Bakery", 1.99f, 90 },
{ "Chicken", "Meat", 4.99f, 50 },
{ "Rice", "Grains", 3.99f, 120 },
{ "Pasta", "Grains", 1.49f, 150 }
};
static void Main(string[] args)
{
// Write code below this line
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string cmdText = "CREATE TABLE store (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), category VARCHAR(100), price FLOAT, stock_quantity INT);";
MySqlCommand cmd = new MySqlCommand(cmdText, connection);
cmd.ExecuteNonQuery();
for (int i = 0; i < 10; ++i)
{
string name = (string)groceryItemsData[i, 0];
string category = (string)groceryItemsData[i, 1];
float price = (float)groceryItemsData[i, 2];
int stock_quantity = (int)groceryItemsData[i, 3];
cmdText = $"INSERT INTO store (name, category, price, stock_quantity) VALUES ('{name}', '{category}', {price}, {stock_quantity});";
cmd = new MySqlCommand(cmdText, connection);
cmd.ExecuteNonQuery();
}
}
// Write code above this line
}
}
Tudo estava claro?