Week2v2 (1)
Week2v2 (1)
Lab Manual 2
Learning Objectives
• Understand the key differences between C++ and C# syntax and
convert basic programs from C++ to C#.
• Establish a secure connection between a MySQL database and a
C# application.
• Perform CRUD (Create, Read, Update, Delete) operations in C#
using MySQL.
• Learn how to set up and configure a Google Cloud Platform
(GCP) account for database hosting.
LO1: Understand the key differences between C++ and C#
syntax and convert basic programs from C++ to C#.
Basic Program Structure
• C# uses using instead of #include
• C# code can execute without Main() function
• No explicit return statement needed in C#
C++ C#
#include <iostream>
Console.WriteLine(“Hello, World”);
int main() {
// code here
return 0;
}
Hello world
• C# uses Console.WriteLine() or Console.Write () to print any
line on console.
• Console.WriteLine() display line on the console and move the
cursor to next line.
• Console.Write() display line on the console and the cursor will
remain on the same line.
Reading Input
• C# uses Console.ReadLine() or Console.Read() to take input
from user.
• Console.ReadLine() read the string and Console.Read() just read
one character
• You need to convert the input into specific datatype as input of
C# is always in string datatype.
C++ C#
#include <iostream> using System;
using namespace std; class Program {
int main() { static void Main() {
int number; Console.Write("Enter a
cout << "Enter a number: number: ");
"; int number =
cin >> number; Convert.ToInt32(Console.ReadLine());
cout << "You entered: " Console.WriteLine("You
<< number << endl; entered: " + number);
return 0; }
} }
Conditional Statements
• The Conditional Statements are same in C#
C++ C#
if (x > 0) { if (x > 0) {
// code // code
} }
Loops
• The Loops are same in C#
C++ C#
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
// code // code
} }
While Loop:
C++ C#
while (i < 5) { while (i < 5) {
// code // code
} }
Arrays
• C# arrays are objects with properties like Length (to calculate
length of array)
• Array size can be determined at runtime
C++ C#
int arr[5]; int[] arr = new int[5];
Practice Tasks:
1. P1Task1.cs: Convert the following C++ program to C#:
#include <iostream>
using namespace std;
int main() {
cout << "Enter your name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
return 0;
}
Submission Requirements
• Submit the following files:
o P1Task1.cs
o P1Task2.cs
o P1Task3.cs
o P1Task4.cs
o P1Task5.cs
o P1Task6.cs
LO2: Establish a secure connection between a MySQL
database and a C# application.
Understanding Connection Strings
A connection string is a string that specifies information about a data
source and the means of connecting to it. Here's the anatomy of a
MySQL connection string:
server=127.0.0.1; // Database server address
port=3306; // Port on which server is running
user=your_username; // Username by default it is “root”
database=database_name; // Database name
password=your_password; // Password
SslMode=Required; // Security setting
Example:
Database Systems-Manual 2 Page | 4
server=127.0.0.1;port=3306;user=root;database=Lab2;password=
yourdatabasePassword;SslMode=Required;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Tls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab2
{
public class DatabaseHelper
{
private String serverName = "127.0.0.1";
private String port = "3306";
private String databaseName = "Lab2";
private String databaseUser = "root";
private String databasePassword = "2002";
private DatabaseHelper() { }
}
}
}
• Student Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab2
{
Database Systems-Manual 2 Page | 8
public class Student
{
public string RegNo { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public int Session { get; set; }
public float Cgpa { get; set; }
public string Address { get; set; }
• Driver Code:
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
Database Systems-Manual 2 Page | 10
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Lab2
{
public class Program
{
static void Main(string[] args)
{
string name, regNo, department, address;
int session;
float cgpa;
Student student;
}
public static int Menu()
{
Console.Clear();
Console.WriteLine("1. Add a New Student");
Console.WriteLine("2. Edit a Student");
Console.WriteLine("3. Delete a Student");
Console.WriteLine("4. Search a Student");
Console.WriteLine("5. Show all Students");
Console.WriteLine("6. Exit");
Database Systems-Manual 2 Page | 12
Console.WriteLine("Enter your choice: ");
int choice = Convert.ToInt32(Console.ReadLine());
return choice;
}
}
}
Tasks
P2Task1: Create Additional Tables
Create a database named Lab2_Task with the following schema:
1. Student Table:
CREATE TABLE Student (
RegistrationNumber VARCHAR(15) PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Department VARCHAR(50) NOT NULL,
Session INT NOT NULL,
Address VARCHAR(255)
);
2. Course Table:
CREATE TABLE Course (
Course_ID VARCHAR(10) PRIMARY KEY,
Course_Name VARCHAR(100) NOT NULL
);
3. Enrollments Table:
CREATE TABLE Enrollments (
StudentRegNo VARCHAR(15),
Course_ID VARCHAR(10),
FOREIGN KEY (StudentRegNo) REFERENCES
Student(RegistrationNumber),
FOREIGN KEY (Course_ID) REFERENCES
Course(Course_ID)
);
4. Attendance Table:
CREATE TABLE Attendance (
StudentRegNo VARCHAR(15),
Course_ID VARCHAR(10),
TimeStamp DATETIME NOT NULL,
Status BOOLEAN NOT NULL,
Database Systems-Manual 2 Page | 13
FOREIGN KEY (StudentRegNo) REFERENCES
Student(RegistrationNumber),
FOREIGN KEY (Course_ID) REFERENCES Course(Course_ID)
);
User Interfaces:
2. Click on "+ Add " > Scroll down to Additional Sources and Click
"Public Datasets".
3. Browse through the available public datasets.
Database Systems-Manual 2 Page | 17
4. Search for "COVID-19 Public Datasets" and add them to your project.
5. After adding a dataset, click on it to explore the schema and table
contents.
6. Write and run simple queries like:
SELECT *
FROM `bigquery-public-
data.covid19_open_data.covid19_open_data`
LIMIT 10;
Submission Requirements
1. Word document named "GCP.docx" with your understanding of:
o BigQuery
o GitHub dataset insights
2. Screenshots of:
o Your GCP account dashboard.
o Queries written and their results.
Submission on Eduko
1. Zip all the files of your tasks:
o LO1
o LO3
o LO4