How can SfSchedule's SQL database be used to conduct CRUD operations?
This article explains how to connect the database with WPF Schedule(SfSchedule) control and how to add, update, and delete data in database using SQL server.
Steps:
4. Drag Meeting_Table table into design view of MeetingDB.dbml. The Entity model diagram for meeting table is generated after it has been dropped into design view. You can enter the values in the Meeting_Table using the Show Table Data option. The meeting details are maintained as string in the database.
You can load the database created in previous step into SfSchedule using MeetingsDBDataContext, and it can be mapped to the custom appointments of schedule. Refer to this documentation to know more about schedule and custom appointments and mapping method.
C#
MeetingsDBDataContext = new MeetingsDBDataContext(); var MeetingsList = (from data in MeetingsDBDataContext.Meeting_Tables select data).ToList(); for (int i = 0; i < MeetingsList.Count; i++) { Meetings.Add(new ScheduleMeeting() { MappedSubject = MeetingsList[i].Subject, MappedStartTime = Convert.ToDateTime(MeetingsList[i].StartTime), MappedEndTime = Convert.ToDateTime(MeetingsList[i].EndTime), MappedColor = newSolidColorBrush((Color)ColorConverter.ConvertFromString(MeetingsList[i].Color)), MappedIsAllDay = Convert.ToBoolean(MeetingsList[i].IsAllDay), AppointmentID = MeetingsList[i].ID.ToString() }); }
You can also add, update, and delete the data in database using MeetingsDBDataContext and Meeting_Table in AppointmentEditorClosed event, where you can get the edited appointment details.
private void Schedule_AppointmentEditorClosed(object sender, AppointmentEditorClosedEventArgs e) { MeetingsDBDataContext scheduleMeetings = new MeetingsDBDataContext(); Meeting_Table meeting; if (e.Action == EditorClosedAction.Save) { var editedAppointment = (e.EditedAppointment as ScheduleMeeting); var appointmentID = Convert.ToInt32(editedAppointment.AppointmentID); if (!e.IsNew) { meeting = (from data in scheduleMeetings.Meeting_Tables where data.ID == appointmentID select data).First() as Meeting_Table; meeting.Subject = editedAppointment.MappedSubject; meeting.StartTime = editedAppointment.MappedStartTime.ToString(); meeting.EndTime = editedAppointment.MappedEndTime.ToString(); meeting.Color = editedAppointment.MappedColor.ToString(); meeting.IsAllDay = editedAppointment.MappedIsAllDay.ToString(); meeting.ID = appointmentID; } else { meeting = new Meeting_Table(); meeting.Subject = editedAppointment.MappedSubject; meeting.StartTime = editedAppointment.MappedStartTime.ToString(); meeting.EndTime = editedAppointment.MappedEndTime.ToString(); meeting.Color = editedAppointment.MappedColor.ToString(); meeting.IsAllDay = editedAppointment.MappedIsAllDay.ToString(); meeting.ID = appointmentID; scheduleMeetings.Meeting_Tables.InsertOnSubmit(meeting); } scheduleMeetings.SubmitChanges(); } if (e.Action == EditorClosedAction.Delete) { var editedAppointment = (e.OriginalAppointment as ScheduleMeeting); var appointmentID = Convert.ToInt32(editedAppointment.AppointmentID); meeting = (from data in scheduleMeetings.Meeting_Tables where data.ID == appointmentID select data).First() as Meeting_Table; scheduleMeetings.Meeting_Tables.DeleteOnSubmit(meeting); scheduleMeetings.SubmitChanges(); } } }
Sample
You can find the sample in the following link: Sample