Skip to content

Commit

Permalink
add - sql server database integration
Browse files Browse the repository at this point in the history
  • Loading branch information
AqibChattha committed Nov 14, 2022
1 parent 2594ac9 commit a1fbfa6
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 277 deletions.
47 changes: 47 additions & 0 deletions SeaAnimalQuiz/SeaAnimalQuiz/Classes/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace SchoolManagementSystem.Classes
{
public class Config
{
private string ConnectionString = @"Data Source=(local);Initial Catalog=SeaAnimalQuiz;Integrated Security=True";
private SqlConnection _connection;
private static Config _instance;

private Config()
{
_connection = new SqlConnection(ConnectionString);
try
{
_connection.Open();
}
catch (Exception)
{
MessageBox.Show("Connection failed, Please try again later.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static Config Instance
{
get
{
if (_instance == null)
_instance = new Config();
return _instance;
}
}
public SqlConnection Connection
{
get
{
return _connection;
}
}

}
}
12 changes: 6 additions & 6 deletions SeaAnimalQuiz/SeaAnimalQuiz/Classes/MCQ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class MCQ : Question
// The fourth possible answer.
string answer4;

// image file name
string imageFileName;
// image file
byte[] image;

/// <summary>
/// constructor for the MCQ class.
Expand All @@ -36,14 +36,14 @@ public class MCQ : Question
/// <param name="answer2"></param>
/// <param name="answer3"></param>
/// <param name="correctAnswer"></param>
public MCQ(string questionText, string correctAnswer, string answer1, string answer2, string answer3, string answer4, string imageFileName)
public MCQ(string questionText, string correctAnswer, string answer1, string answer2, string answer3, string answer4, byte[] image)
: base(questionText, correctAnswer, QuestionType.MultipleChoice)
{
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.answer4 = answer4;
this.imageFileName = imageFileName;
this.image = image;
}

/// <summary>
Expand Down Expand Up @@ -103,11 +103,11 @@ public string Answer4
/// <summary>
/// this will return the image file name
/// </summary>
public string ImageFileName
public byte[] Image
{
get
{
return imageFileName;
return image;
}
}

Expand Down
95 changes: 31 additions & 64 deletions SeaAnimalQuiz/SeaAnimalQuiz/Classes/Quiz.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using SchoolManagementSystem.Classes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Windows.Forms;
Expand Down Expand Up @@ -52,60 +56,44 @@ public Quiz()
/// <summary>
/// load the questions from the text file.
/// </summary>
public void LoadQuestions(QuizCategory category)
public bool LoadQuestions(QuizCategory category)
{
// load the questions from the FishQuestions.txt file in the Data directory.
string file = "";
if (category == QuizCategory.Fish)
{
file = "../../Data/FishQuestions.txt";
}
else if (category == QuizCategory.Birds)
{
file = "../../Data/BirdQuestions.txt";
}
else if (category == QuizCategory.Reptiles)
{
file = "../../Data/ReptileQuestions.txt";
}

try
{
if (File.Exists(file))
var con = Config.Instance.Connection;
SqlCommand cmd = new SqlCommand("dbo.[Get_QuestionsForQuiz]", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Category", category.ToString()));
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// Store each line in array of strings
string[] lines = File.ReadAllLines(file);


foreach (string ln in lines)
if (reader["qType"].ToString().Equals(QuestionType.MultipleChoice.ToString()))
{
MCQ mcq = new MCQ(reader["qText"].ToString()
, reader["correctAnswer"].ToString()
, reader["option1"].ToString()
, reader["option2"].ToString()
, reader["option3"].ToString()
, reader["option4"].ToString()
, reader["image"] == DBNull.Value ? null : (byte[])reader["image"]);
questions.Add(mcq);
}
else if (reader["qType"].ToString().Equals(QuestionType.TrueFalse.ToString()))
{
if (!(ln == null || ln == ""))
{
string[] data = ln.Split(new string[] { ",;," }, StringSplitOptions.None);
if (data[0].Equals(QuestionType.MultipleChoice.ToString()))
{
// create MCQ and load data
MCQ mcq = new MCQ(data[1], data[2], data[3], data[4], data[5], data[6], data[7]);
questions.Add(mcq);
}
else if (data[0].Equals(QuestionType.TrueFalse.ToString()))
{
// create True False and load data
TrueFalse trueFalse = new TrueFalse(data[1], data[2], data[3], data[4], data[5]);
questions.Add(trueFalse);
}
}
TrueFalse trueFalse = new TrueFalse(reader["qText"].ToString()
, reader["correctAnswer"].ToString()
, reader["image"] == DBNull.Value ? null : (byte[])reader["image"]);
questions.Add(trueFalse);
}
}
reader.Close();
}
catch (Exception)
{
MessageBox.Show("There was an error loading the questions.", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return false;
}

// Shuffle the questions.
ShuffleQuestions();

// Reset the current question number.
currentQuestionNumber = 0;

Expand All @@ -114,29 +102,8 @@ public void LoadQuestions(QuizCategory category)

// Reset the number of incorrect answers.
incorrectAnswers = 0;
}

/// <summary>
/// shuffle the questions in the list and only retain 20 questions.
/// </summary>
private void ShuffleQuestions()
{
// Create a new random number generator.
Random random = new Random();

// Shuffle the questions in the list.
for (int i = 0; i < questions.Count; i++)
{
// Get a random index.
int randomIndex = random.Next(0, questions.Count);

// Swap the questions at the current index and the random index.
Question temp = questions[i];
questions[i] = questions[randomIndex];
questions[randomIndex] = temp;
}

try { questions = questions.GetRange(0, 20); } catch (Exception) { }
return true;
}

/// <summary>
Expand Down
42 changes: 6 additions & 36 deletions SeaAnimalQuiz/SeaAnimalQuiz/Classes/TrueFalse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,23 @@ namespace SeaAnimalQuiz.Classes
/// </summary>
internal class TrueFalse : Question
{
// first expected answer
string firstAnswer;
// image file
byte[] image;

// second expected answer
string secondAnswer;

// image file name
string imageFileName;

public TrueFalse(string questionText, string correctAnswer, string firstAnswer, string secondAnswer, string imageFileName)
public TrueFalse(string questionText, string correctAnswer, byte[] image)
: base(questionText, correctAnswer, QuestionType.TrueFalse)
{
this.firstAnswer = firstAnswer;
this.secondAnswer = secondAnswer;
this.imageFileName = imageFileName;
}

/// <summary>
/// This method will get the first answer.
/// </summary>
public string FirstAnswer
{
get
{
return firstAnswer;
}
}

/// <summary>
/// This method will get the second answer.
/// </summary>
public string SecondAnswer
{
get
{
return secondAnswer;
}
this.image = image;
}

/// <summary>
/// This method will get the image file name.
/// </summary>
public string ImageFileName
public byte[] Image
{
get
{
return imageFileName;
return image;
}
}

Expand Down
1 change: 1 addition & 0 deletions SeaAnimalQuiz/SeaAnimalQuiz/SeaAnimalQuiz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Config.cs" />
<Compile Include="Classes\MCQ.cs" />
<Compile Include="Classes\Question.cs" />
<Compile Include="Classes\Quiz.cs" />
Expand Down
4 changes: 0 additions & 4 deletions SeaAnimalQuiz/SeaAnimalQuiz/UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ public void SetQuizPage(QuizCategory category)
ucQuiz.Instance.RefreshUc();
ucQuiz.Instance.BringToFront();
}
else
{
MessageBox.Show("Not enough questions available for this category. Please add questions to this category.", "No Questions", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btnStart_Click(object sender, EventArgs e)
Expand Down
23 changes: 2 additions & 21 deletions SeaAnimalQuiz/SeaAnimalQuiz/UI/ucAddQuestions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,13 @@ private void rbtnTrueFalse_CheckedChanged(object sender, EventArgs e)

private void btnAdd_Click(object sender, EventArgs e)
{
string fileToAppend = "";
if (cbCategories.SelectedItem.ToString().Equals("Fish"))
{
fileToAppend = "../../Data/FishQuestions.txt";
}
else if (cbCategories.SelectedItem.ToString().Equals("Birds"))
{
fileToAppend = "../../Data/BirdQuestions.txt";
}
else if (cbCategories.SelectedItem.ToString().Equals("Reptiles"))
{
fileToAppend = "../../Data/ReptileQuestions.txt";
}
else
{
MessageBox.Show("Please select the question category.", "Select category", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

if (rbtnMcq.Checked)
{
ucAdd_MCQ.Instance.saveQuestion(fileToAppend);
ucAdd_MCQ.Instance.saveQuestion(cbCategories.SelectedItem.ToString());
}
else if (rbtnTrueFalse.Checked)
{
ucAdd_TrueFalse.Instance.saveQuestion(fileToAppend);
ucAdd_TrueFalse.Instance.saveQuestion(cbCategories.SelectedItem.ToString());
}
else
{
Expand Down
Loading

0 comments on commit a1fbfa6

Please sign in to comment.