C
C#11mo ago
RebornJcush

Adding a Database

Hey everyone, I'm working on an assignment and I've got my code written but I do not understand how to implement a database into it. I had 1 example from my course on this area and it wasn't enough, if anyone could help I'd greatly appreciate it! ```using System; using System.Data.SqlClient; class Program { static void Main() { // Connection string for your database string connectionString = $"Data Source={orkout};Version=3;"; // Create a SqlConnection using the connection string using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Insert user int userId; using (SqlCommand insertUserCommand = new SqlCommand("INSERT INTO Users (UserName) VALUES (@User); SELECT SCOPE_IDENTITY();", connection)) { insertUserCommand.Parameters.AddWithValue("@User", "Justyn Cushing"); // Execute the command and get the UserId userId = Convert.ToInt32(insertUserCommand.ExecuteScalar()); } // Insert workouts using (SqlCommand insertWorkoutCommand = new SqlCommand("INSERT INTO Workouts (UserName, Day, Exercise, Weight) VALUES (@User, @Day, @Exercise, @Weight);", connection)) { insertWorkoutCommand.Parameters.AddWithValue("@User", userId); insertWorkoutCommand.Parameters.AddWithValue("@Day", "Monday"); insertWorkoutCommand.Parameters.AddWithValue("@Exercise", "Squats"); insertWorkoutCommand.Parameters.AddWithValue("@Weight", 50); // Execute the command for each workout insertWorkoutCommand.ExecuteNonQuery(); // Repeat for other workouts... } } WeeklyWorkoutTracker weeklyTracker = new WeeklyWorkoutTracker(); // Create a user User user = new User("Justyn Cushing"); // Add workouts for the user weeklyTracker.AddWorkout(user, "Monday", "Squats", 50); weeklyTracker.AddWorkout(user, "Wednesday", "Bench Press", 40); weeklyTracker.AddWorkout(user, "Friday", "Deadlifts", 60); // Display the weekly workouts for the user weeklyTracker.DisplayWeeklyWorkouts(); } } public class WeeklyWorkoutTracker { private List<Workout> weeklyWorkouts; public WeeklyWorkoutTracker() { weeklyWorkouts = new List<Workout>(); } public void AddWorkout(User user, string day, string exercise, double weight) { Workout workout = new Workout { User = user, Day = day, Exercise = exercise, Weight = weight }; weeklyWorkouts.Add(workout); } public void DisplayWeeklyWorkouts() { foreach (var workout in weeklyWorkouts) { Console.WriteLine(workout); } } } public class Workout { public string? Day { get; set; } public string? Exercise { get; set; } public double Weight { get; set; } public User? User { get; set; } public override string ToString() { return $"On {Day}, {User?.UserName} performed {Exercise} with a weight of {Weight} lbs.\n"; } } public class User { public string UserName { get; set; } public User(string userName) { UserName = userName; } }
3 Replies
RebornJcush
RebornJcushOP11mo ago
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Connection string for your database
string connectionString = $"Data Source={orkout};Version=3;";

// Create a SqlConnection using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

// Insert user
int userId;
using (SqlCommand insertUserCommand = new SqlCommand("INSERT INTO Users (UserName) VALUES (@User); SELECT SCOPE_IDENTITY();", connection))
{
insertUserCommand.Parameters.AddWithValue("@User", "Justyn Cushing");

// Execute the command and get the UserId
userId = Convert.ToInt32(insertUserCommand.ExecuteScalar());
}

// Insert workouts
using (SqlCommand insertWorkoutCommand = new SqlCommand("INSERT INTO Workouts (UserName, Day, Exercise, Weight) VALUES (@User, @Day, @Exercise, @Weight);", connection))
{
insertWorkoutCommand.Parameters.AddWithValue("@User", userId);
insertWorkoutCommand.Parameters.AddWithValue("@Day", "Monday");
insertWorkoutCommand.Parameters.AddWithValue("@Exercise", "Squats");
insertWorkoutCommand.Parameters.AddWithValue("@Weight", 50);

// Execute the command for each workout
insertWorkoutCommand.ExecuteNonQuery();

// Repeat for other workouts...
}
}
WeeklyWorkoutTracker weeklyTracker = new WeeklyWorkoutTracker();

// Create a user
User user = new User("Justyn Cushing");

// Add workouts for the user
weeklyTracker.AddWorkout(user, "Monday", "Squats", 50);
weeklyTracker.AddWorkout(user, "Wednesday", "Bench Press", 40);
weeklyTracker.AddWorkout(user, "Friday", "Deadlifts", 60);

// Display the weekly workouts for the user
weeklyTracker.DisplayWeeklyWorkouts();
}
}
public class WeeklyWorkoutTracker
{
private List<Workout> weeklyWorkouts;

public WeeklyWorkoutTracker()
{
weeklyWorkouts = new List<Workout>();
}

public void AddWorkout(User user, string day, string exercise, double weight)
{
Workout workout = new Workout
{
User = user,
Day = day,
Exercise = exercise,
Weight = weight
};

weeklyWorkouts.Add(workout);
}

public void DisplayWeeklyWorkouts()
{
foreach (var workout in weeklyWorkouts)
{
Console.WriteLine(workout);
}
}
}

public class Workout
{
public string? Day { get; set; }
public string? Exercise { get; set; }
public double Weight { get; set; }
public User? User { get; set; }

public override string ToString()
{
return $"On {Day}, {User?.UserName} performed {Exercise} with a weight of {Weight} lbs.\n";
}
}

public class User
{
public string UserName { get; set; }

public User(string userName)
{
UserName = userName;
}
}
using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Connection string for your database
string connectionString = $"Data Source={orkout};Version=3;";

// Create a SqlConnection using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

// Insert user
int userId;
using (SqlCommand insertUserCommand = new SqlCommand("INSERT INTO Users (UserName) VALUES (@User); SELECT SCOPE_IDENTITY();", connection))
{
insertUserCommand.Parameters.AddWithValue("@User", "Justyn Cushing");

// Execute the command and get the UserId
userId = Convert.ToInt32(insertUserCommand.ExecuteScalar());
}

// Insert workouts
using (SqlCommand insertWorkoutCommand = new SqlCommand("INSERT INTO Workouts (UserName, Day, Exercise, Weight) VALUES (@User, @Day, @Exercise, @Weight);", connection))
{
insertWorkoutCommand.Parameters.AddWithValue("@User", userId);
insertWorkoutCommand.Parameters.AddWithValue("@Day", "Monday");
insertWorkoutCommand.Parameters.AddWithValue("@Exercise", "Squats");
insertWorkoutCommand.Parameters.AddWithValue("@Weight", 50);

// Execute the command for each workout
insertWorkoutCommand.ExecuteNonQuery();

// Repeat for other workouts...
}
}
WeeklyWorkoutTracker weeklyTracker = new WeeklyWorkoutTracker();

// Create a user
User user = new User("Justyn Cushing");

// Add workouts for the user
weeklyTracker.AddWorkout(user, "Monday", "Squats", 50);
weeklyTracker.AddWorkout(user, "Wednesday", "Bench Press", 40);
weeklyTracker.AddWorkout(user, "Friday", "Deadlifts", 60);

// Display the weekly workouts for the user
weeklyTracker.DisplayWeeklyWorkouts();
}
}
public class WeeklyWorkoutTracker
{
private List<Workout> weeklyWorkouts;

public WeeklyWorkoutTracker()
{
weeklyWorkouts = new List<Workout>();
}

public void AddWorkout(User user, string day, string exercise, double weight)
{
Workout workout = new Workout
{
User = user,
Day = day,
Exercise = exercise,
Weight = weight
};

weeklyWorkouts.Add(workout);
}

public void DisplayWeeklyWorkouts()
{
foreach (var workout in weeklyWorkouts)
{
Console.WriteLine(workout);
}
}
}

public class Workout
{
public string? Day { get; set; }
public string? Exercise { get; set; }
public double Weight { get; set; }
public User? User { get; set; }

public override string ToString()
{
return $"On {Day}, {User?.UserName} performed {Exercise} with a weight of {Weight} lbs.\n";
}
}

public class User
{
public string UserName { get; set; }

public User(string userName)
{
UserName = userName;
}
}
This is the instructions for the assignment: Your task for this week is to implement the classes and functionality needed to support the data storage you've defined. and these are what i declared in my mock up a few weeks back Tables, fields/data types: -Workouts WorkoutID Int (primary key) UserID int Day varchar(50) Exercise varchar(50) Weight int foreign key (userID) references Users(UserID) – allows a relationship between the 2 tables for the the association of workouts to a specific user -Users UserID Int (Primary key) UserName varchar(50) I tried adding in some code into program class for the database but like i said, i dont understand it at all
Joschi
Joschi11mo ago
Is it a requirement to not use external dependencies, like an ORM? If not you could just use EFCore and a SQLite database. There are a lot of tutorials on how to do that.
RebornJcush
RebornJcushOP11mo ago
not specified.. they dont really teach us more so "heres your assignments and a 15 min 10 year old powerpoint goodluck" & ill check it out. thanks!
Want results from more Discord servers?
Add your server