שמירת נתונים לSQLlite מפקד DataGrid בWPF
-
אני משתמש בSQLlite עבור תוכנה מסוג WPF בC#, ואני טוען את הנתונים של טבלה מסוימת לפקד DataGrid עם הפונקציה LoadData, ואני רוצה לאפשר למשתמש לערוך את הנתונים שבפקד בטופס, ואז לעשות כפתור שמירה של הנתונים שנערכו חזרה לדאטהבייס באמצעות הפונקציה SaveChangesToDatabase, אבל אני מקבל שגיאה "שלא ניתן לגשת לקובץ שנזרק" היכן הטעות שלי?
private SQLiteDataAdapter adapter; private DataTable dataTable; private DataView dataView; public static string softwareLibrary = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); public static string dbFilePath = Path.Combine(softwareLibrary, "calls.db"); public static string connectionString = $"Data Source={dbFilePath}"; private void LoadData() { using (SQLiteConnection connection = new SQLiteConnection(Globals.connectionString)) { connection.Open(); string filterId = txtId.Text.Trim(); string filterTime = txtTime.Text.Trim(); string selectSql = "SELECT * FROM tCalls WHERE CallId LIKE '%" + filterId + "%' AND CallTime LIKE '%" + filterTime + "%'"; using (SQLiteCommand cmd = new SQLiteCommand(selectSql, connection)) { adapter = new SQLiteDataAdapter(cmd); SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(adapter); dataTable = new DataTable(); adapter.Fill(dataTable); this.dataGridCalls.ItemsSource = dataTable.DefaultView; } connection.Close(); } } private void SaveChangesToDatabase() { try { if (dataTable != null && dataTable.GetChanges() != null) { using (SQLiteConnection connection = new SQLiteConnection(Globals.connectionString)) { connection.Open(); using (SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(adapter)) { adapter.Update(dataTable); } connection.Close(); } dataTable.AcceptChanges(); } } catch (Exception ex) { MessageBox.Show($"An error occurred while saving changes: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
-
הקוד נראה טוב ממבט ראשון.
איזה חבילות/ספריות מותקנות?
תמחק לגמרי את הtry שיישאר רק הקוד, ותריץ ותראה ותציין באיזה שורה השגיאה קורית. -
@dovid כתב בשמירת נתונים לSQLlite מפקד DataGrid בWPF:
ותציין באיזה שורה השגיאה קורית.
כאן
adapter.Update(dataTable) (שורה 46)
איזה חבילות/ספריות מותקנות?
אני התקנתי את החבילה השניה, כנראה שהיא מתקינה אוטומטית את הראשונה
@מוטי-מן תנסה ככה, לפי תיאור השגיאה הבעיה שלך היא לכאורה מול הadapter, כאן אתה פשוט יותר מחדש dataAdapter עם אותו SELECT
private void SaveChangesToDatabase() { try { if (dataTable != null && dataTable.GetChanges() != null) { DataTable dataTableToUpdate = dataTable; using (SQLiteConnection connection = new SQLiteConnection(Globals.connectionString)) { connection.Open(); using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter("SELECT * FROM tCalls", connection)) { using (SQLiteCommandBuilder commandBuilder = new SQLiteCommandBuilder(dataAdapter)) { commandBuilder.ConflictOption = ConflictOption.OverwriteChanges; commandBuilder.SetAllValues = true; dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand(); dataAdapter.Update(dataTableToUpdate); } } } dataTable.AcceptChanges(); } } catch (Exception ex) { MessageBox.Show($"An error occurred while saving changes: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
-
הבעיה פשוטה, כפי שרמז @אביי הadapter מאותחל עם command + connection ששניהם עוברים הריגה בסיום המתודה load.
במתודה save למרות שהadapter קיים, הcommand שלו לא.
אפשר להשמיט במתודה load את הusing ופשוט לשים קונקשיין וקומנד בעלי אורך חיים מלא, או לעשות את הרעיון של אביי של יצירה מחדש.