Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. public class Student
  2. {
  3. public int Student_id { get; set; }
  4. public string Student_name { get; set; }
  5. public string Student_mark { get; set; }
  6.  
  7. }
  8.  
  9. public class DataService
  10. {
  11. static string connectionString;
  12. public static String Name = "Data Service.";
  13. private static ObservableCollection<Student> _allStudents = new ObservableCollection<Student>();
  14. public static ObservableCollection<Student> GetStudents()
  15. {
  16. try
  17. {
  18. string server = "127.0.0.1";
  19. string database = "sakila";
  20. string user = "root";
  21. string pswd = "!QAZ2wsx";
  22. connectionString = "Server = " + server + ";database = " + database + ";uid = " + user + ";password = " + pswd + ";SslMode=None;";
  23. using (MySqlConnection connection = new MySqlConnection(connectionString))
  24. {
  25. connection.Open();
  26. MySqlCommand getCommand = connection.CreateCommand();
  27. getCommand.CommandText = "SELECT * FROM student";
  28. using (MySqlDataReader reader = getCommand.ExecuteReader())
  29. {
  30. while (reader.Read())
  31. {
  32. _allStudents.Add(new Student() { Student_id = reader.GetInt32(0), Student_name = reader.GetString(1), Student_mark = reader.GetString(2) });
  33. }
  34. }
  35. }
  36. }
  37. catch (MySqlException sqlex)
  38. {
  39. // Handle it :)
  40. }
  41. return _allStudents;
  42. }
  43.  
  44. public static bool InsertNewStudent(Student newStudent)
  45. {
  46. // Insert to the collection and update DB
  47. try
  48. {
  49. using (MySqlConnection connection = new MySqlConnection(connectionString))
  50. {
  51. connection.Open();
  52. MySqlCommand insertCommand = connection.CreateCommand();
  53. insertCommand.CommandText = "INSERT INTO student(student_id, student_name, student_mark)VALUES(@student_id, @student_name,@student_mark)";
  54. insertCommand.Parameters.AddWithValue("@student_id", newStudent.Student_id);
  55. insertCommand.Parameters.AddWithValue("@student_name", newStudent.Student_name);
  56. insertCommand.Parameters.AddWithValue("@student_mark", newStudent.Student_mark);
  57. insertCommand.ExecuteNonQuery();
  58. return true;
  59. }
  60. }
  61. catch (MySqlException sqlex)
  62. {
  63. return false;
  64. }
  65.  
  66. }
  67. public static bool UpdateStudent(Student Student)
  68. {
  69. try
  70. {
  71. using (MySqlConnection connection = new MySqlConnection(connectionString))
  72. {
  73. connection.Open();
  74. MySqlCommand insertCommand = connection.CreateCommand();
  75. insertCommand.CommandText = "Update student Set student_name= @student_name, student_mark=@student_mark Where student_id =@student_id";
  76. insertCommand.Parameters.AddWithValue("@student_id", Student.Student_id);
  77. insertCommand.Parameters.AddWithValue("@student_name", Student.Student_name);
  78. insertCommand.Parameters.AddWithValue("@student_mark", Student.Student_mark);
  79. insertCommand.ExecuteNonQuery();
  80. return true;
  81. }
  82. }
  83. catch (MySqlException sqlex)
  84. {
  85. // Don't forget to handle it
  86. return false;
  87. }
  88.  
  89. }
  90.  
  91. public static bool Delete(Student Student)
  92. {
  93. try
  94. {
  95. using (MySqlConnection connection = new MySqlConnection(connectionString))
  96. {
  97. connection.Open();
  98. MySqlCommand insertCommand = connection.CreateCommand();
  99. insertCommand.CommandText = "Delete from sakila.student where student_id =@student_id";
  100. insertCommand.Parameters.AddWithValue("@student_id", Student.Student_id);
  101. insertCommand.ExecuteNonQuery();
  102. return true;
  103. }
  104. }
  105. catch (MySqlException sqlex)
  106. {
  107. return false;
  108. }
  109. }
  110. }
  111.  
  112. void Person_OnNotifyPropertyChanged(Object sender, PropertyChangedEventArgs e)
  113. {
  114. organization.Update((StudentViewModel)sender);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement