public static int DoRegisterNewTask(TaskInfo TaskDetails) { int returnVal = 0; MySql.Data.MySqlClient.MySqlConnection msqlConnection = OpenDbConnection(); try { //define the command reference MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand(); //define the connection used by the command object msqlCommand.Connection = msqlConnection; msqlCommand.CommandText = "INSERT INTO task(taskId,priority,details) " + "VALUES(@taskId,@priority,@details)"; msqlCommand.Parameters.AddWithValue("@taskId", TaskDetails.id); msqlCommand.Parameters.AddWithValue("@priority", TaskDetails.value); msqlCommand.Parameters.AddWithValue("@details", TaskDetails.taskDetails); msqlCommand.ExecuteNonQuery(); returnVal = 1; } catch (Exception er) { returnVal = 0; } finally { //always close the connection msqlConnection.Close(); } return returnVal; }
private static List<TaskInfo> QueryAllTaskList() { List<TaskInfo> TaskList = new List<TaskInfo>(); MySql.Data.MySqlClient.MySqlConnection msqlConnection = OpenDbConnection(); try { //define the command reference MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand(); msqlCommand.Connection = msqlConnection; msqlCommand.CommandText = "Select * From task;"; MySql.Data.MySqlClient.MySqlDataReader msqlReader = msqlCommand.ExecuteReader(); while (msqlReader.Read()) { TaskInfo Task = new TaskInfo(); Task.id = msqlReader.GetString("taskId"); Task.value = msqlReader.GetString("priority"); Task.taskDetails = msqlReader.GetString("details"); TaskList.Add(Task); } } catch (Exception er) { } finally { //always close the connection msqlConnection.Close(); } return TaskList; }
private void addTaskBtn_Click(object sender, RoutedEventArgs e) { DNBSNData.TaskInfo newTask = new DNBSNData.TaskInfo(); newTask.id = GenerateId(); newTask.value = taskValueCB.Text; newTask.taskDetails = taskDetailsTB.Text; DNBSNDb.DbInteraction.DoRegisterNewTask(newTask); taskValueCB.Text = taskDetailsTB.Text = ""; fetchTaskData(); tskxpndr.IsExpanded = false; }