Зміст курсу
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 CREATE
A grocery store's business is expanding, and they need an efficient way to keep track of their products. They hire you to make an application for them, which is based on a MySQL database where they can store and monitor all the products that they have in stock.
Your task is to write the C# code for:
- Connecting to your local MySQL database;
- Creating a new table called
products
with the following columns:
- id: A column of type integer;
- name: A string value up to 100 characters;
- category: A string representing the category of the product. Can be up to 100 characters;
- price: A float value representing the price;
- stock_quantity: An integer representing the quantity of the product available in stock.
You can either create and connect to a new database or use an existing one.
MySqlConnector
to establish a connection to the local database. 2. You also need to close the connection using the
Close()
method or by utilizing the using
statement. 3. Creating objects is a non-query operation. This command will be executed using the
ExecuteNonQuery()
method.
using System;
using MySql.Data.MySqlClient;
class Program
{
static void Main(string[] args)
{
// Write code below this line
string connectionString = "your connection string";
using(MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string cmdText = "CREATE TABLE products (id INT, name VARCHAR(100), category VARCHAR(100), price FLOAT, stock_quantity INT);";
MySqlCommand cmd = new MySqlCommand(cmdText, connection);
cmd.ExecuteNonQuery();
}
// Write code above this line
}
}
Все було зрозуміло?
Зміст курсу
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 CREATE
A grocery store's business is expanding, and they need an efficient way to keep track of their products. They hire you to make an application for them, which is based on a MySQL database where they can store and monitor all the products that they have in stock.
Your task is to write the C# code for:
- Connecting to your local MySQL database;
- Creating a new table called
products
with the following columns:
- id: A column of type integer;
- name: A string value up to 100 characters;
- category: A string representing the category of the product. Can be up to 100 characters;
- price: A float value representing the price;
- stock_quantity: An integer representing the quantity of the product available in stock.
You can either create and connect to a new database or use an existing one.
MySqlConnector
to establish a connection to the local database. 2. You also need to close the connection using the
Close()
method or by utilizing the using
statement. 3. Creating objects is a non-query operation. This command will be executed using the
ExecuteNonQuery()
method.
using System;
using MySql.Data.MySqlClient;
class Program
{
static void Main(string[] args)
{
// Write code below this line
string connectionString = "your connection string";
using(MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string cmdText = "CREATE TABLE products (id INT, name VARCHAR(100), category VARCHAR(100), price FLOAT, stock_quantity INT);";
MySqlCommand cmd = new MySqlCommand(cmdText, connection);
cmd.ExecuteNonQuery();
}
// Write code above this line
}
}
Все було зрозуміло?