public Student RegisterStudent(AddStudentOptions options) { if (options == null) { return(null); } if (string.IsNullOrEmpty(options.Firstname)) { return(null); } if (string.IsNullOrEmpty(options.Lastname)) { return(null); } if (string.IsNullOrEmpty(options.Email)) { return(null); } if (string.IsNullOrEmpty(options.Phone)) { return(null); } var newStudent = new Student() { Firstname = options.Firstname, Lastname = options.Lastname, Email = options.Email, Phone = options.Phone }; return(newStudent); }
private static int AddStudent(AddStudentOptions options) { Console.WriteLine("Adding student to database..."); if (!File.Exists($"{AppDomain.CurrentDomain.BaseDirectory}StudentAttendance.mdf")) { Console.WriteLine("Database is not created. Type 'sc -init'."); return(1); } var sqlConnection = new SqlConnection(ConnectionString); try { sqlConnection.Open(); var command = new SqlCommand { Connection = sqlConnection, CommandText = "INSERT INTO StudentAttendance.dbo.Students VALUES (@StudentName)" }; var studentName = command.CreateParameter(); studentName.ParameterName = "@StudentName"; studentName.SqlDbType = SqlDbType.VarChar; studentName.Direction = ParameterDirection.Input; studentName.Value = options.StudentName; command.Parameters.Add(studentName); var affected = command.ExecuteNonQuery(); Console.WriteLine(affected > 0 ? "Student added successfully." : "Student is not added."); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { if (sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); } } return(1); }