This is my first StackOverflow post, so please be careful ...
I have some questions regarding the scope of objects for ADO.NET.
When I connect to the database, I usually use this code:
OleDbConnection conn = new OleDbConnection("my_connection_string"); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * from Employees", conn); OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter); DataTable dt = new DataTable(); adapter.Fill(dt); conn.Close(); conn.Dispose();
Let's say that I bind the resulting DataTable to the grid control and allow my users to edit the contents of the grid. Now, when my users click the "Save" button, I need to call this code:
adapter.Update(dt);
Here are my questions:
1) Do I need to save the adapter object that I created when I originally uploaded the data, or can I create another adapter object in the "Click" Save "event to update?
2) If I need to save the source adapter object, do I also need to save the available connection object and open it?
I understand the unrelated ADO.NET model - I just got confused in the area of ββobjects when it came time to update the database. If someone could give me some guidance on best practices for this scenario, I would really appreciate it!
Thanks in advance...
Mark lansdown
source share