简体   繁体   中英

Updating SQL Server CE database bound to DataTable

I'm new to SQL and I'm a little unsure about the best way to use the database in the program I'm writing.

My program saves "notes" to an SQL Server CE database. A note row consists of an auto-incrementing ID (int) , a title (string) and the text of the note, which is stored in the database as ntext .

I have a SqlCeDataAdapter loading the ID and title rows into a DataTable which is bound to a DataGrid , so the user can see the list of notes and select one. I don't load the text row because I assume it could be large and so it doesn't need to be in memory all the time.

string ConnectionString = "DataSource = " + @"..\\..\\Notes.sdf";
DatabaseConnection = new SqlCeConnection(ConnectionString);
DatabaseConnection.Open();

SqlCeCommand SelectCommand = new SqlCeCommand("SELECT ID, title FROM notes_id", DatabaseConnection);
DatabaseAdapter = new SqlCeDataAdapter(SelectCommand);

NotesDataTable = new DataTable();
DatabaseAdapter.Fill(NotesDataTable);

MainWindowDataView = NotesDataTable.DefaultView;
dataGridNotes.DataContext = MainWindowDataView;

The problem is when I add a new note. Should I be updating the DataTable and using that to update the SQL database? Or should I be directly updating the SQL database and use that to reload the DataTable to update it?

The problem with updating the DataTable is that I don't have a column there for the text of the note. And if I insert a new row, I'm not sure how to refer to that new row to add the text cell.

Thanks.

You should insert the new row into the database and then reload your DataTable (and your view of the data) with a fresh query from the db. Seems too simple though so you may need to elaborate the problem a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM