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;
}
}
5 replies
Why is my list output not using the method to check true/false
my output is printing like this:
Justyn Cushing, Week 2 Interface PA
Vehicles printed from List
Make: Ford
Model: Festiva
The car is running and heading to Miami, FL
Make: Honda
Model: Accord
The car is running and heading to Atlanta, GA
Make: Volvo
Model: VNL 860
The truck is running and hauling 150 cubic ft of cargo to Richmond, VA
Make: Ford
Model: Transit
The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA
Vehicles printed directly
Make: Ford
Model: Festiva
The car is running and heading to Miami, FL
Make: Honda
Model: Accord
The car is not running and heading to Atlanta, GA
Make: Volvo
Model: VNL 860
The truck is not running and hauling 150 cubic ft of cargo to Richmond, VA
Make: Ford
Model: Transit
The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA
The directly output is correct however, the list will not put anything other than all true or all false. I can upload other classes if needed.
44 replies