How to perform the CRUD operations in WPF Scheduler using offline SQL database?
In the WPF scheduler, perform CRUD operation using SQL local data base in Scheduler by using OnAppointmentEditorClosing event.
Refer the following documentation to configure the SQL database in the application.
STEP 1: Create a local database and add the required code snippets for DB connections.
public static string ConnectionString { get { string currentDir = System.Environment.CurrentDirectory; currentDir = currentDir.Substring(0, currentDir.Length - 10); return @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + currentDir + @"\MeetingsDB.mdf;Integrated Security=True"; } } public static SqlConnection GetDBConnection() { SqlConnection connection = new SqlConnection(ConnectionString); if (connection.State != ConnectionState.Open) { connection.Open(); } return connection; }
STEP 2: Create a table in the database and add the required fields like the below code snippet.
STEP 3: Gets the data from the table and populate the Meetings collection in SchedulerViewModel.
class SchedulerViewModel { public List<Meetings> Meetings { get; set; } public SchedulerViewModel() { try { var dataTable = ConnectDB.GetDataTable("SELECT * FROM Meetings"); Meetings = new List<Meetings>(); Meetings = (from DataRow dr in dataTable.Rows select new Meetings() { Subject = dr["Subject"].ToString(), StartTime = dr["StartTime"] as DateTime? ?? DateTime.Now, EndTime = dr["EndTime"] as DateTime? ?? DateTime.Now }).ToList(); } catch { // Handle exceptions } } }
STEP 4: Set the SchedulerViewModel as the DataContext and ItemSource as Meetings.
<Window.DataContext> <local:SchedulerViewModel/> </Window.DataContext> <Grid> <scheduler:SfScheduler x:Name="scheduler" ViewType="Week" ItemsSource="{Binding Meetings}"> <scheduler:SfScheduler.AppointmentMapping> <scheduler:AppointmentMapping Subject="Subject" StartTime="StartTime" EndTime="EndTime"/> </scheduler:SfScheduler.AppointmentMapping> </scheduler:SfScheduler> </Grid>
STEP 5: Using OnAppointmentEditorClosing event and SQL query update the DB for require actions.
private void OnSchedulerAppointmentEditorClosing(object sender, AppointmentEditorClosingEventArgs e) { if (e.Action == AppointmentEditorAction.Add) { string sqlAdd = "INSERT INTO Meetings ([Subject],[StartTime],[EndTime]) VALUES('" + e.Appointment.Subject + "', '" + e.Appointment.StartTime.ToString("yyyy-MM-dd HH:mm:ss") + "' , '" + e.Appointment.EndTime.ToString("yyyy-MM-dd HH:mm:ss") + "')"; ConnectDB.ExecuteSQLQuery(sqlAdd); } else if (e.Action == AppointmentEditorAction.Delete) { string sqlDelete = "DELETE from Meetings where Subject ='" + e.Appointment.Subject + "';"; ConnectDB.ExecuteSQLQuery(sqlDelete); } else if (e.Action == AppointmentEditorAction.Edit) { string sqlUpdate = "UPDATE Meetings set StartTime='" + e.Appointment.StartTime + "',EndTime='" + e.Appointment.EndTime + "' where Subject='" + e.Appointment.Subject + "';"; ConnectDB.ExecuteSQLQuery(sqlUpdate); } }
Conclusion
I hope you enjoyed learning about how to perform the CRUD operations in WPF Scheduler (Calendar).
You can refer to our WPF Scheduler feature
tour
page to know about its
other groundbreaking feature representations. You can also explore
documentation
to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!