|
-
Jun 11th, 2020, 07:55 PM
#5
Re: Binding a Data Reader in Windows Forms
 Originally Posted by Delaney
Ok, thank you
If I understand correctly the way : you have some data in a data base, you open it ( connection), you read it (reader), you place what you read in the datasource of the databinding object ( BindingSource1.DataSource = reader) then you use it ( ComboBox1.DataSource = BindingSource1) in you application.
So the datatable was just an intermediary with also methods to play with the data
I suppose this BindingSource1.DataSource = reader format somehow the data else you would do directly ComboBox1.DataSource = reader or it is to read only one time the data to use them with several object (maybe both in fact). I have read the microsoft doc ( link) but I must confess I didn't understand very well. It seems also that the BindingSource object has several methods to play with the data before using them.
I need to jump seriously into database vocabulary, structure, philosophy and tutorials else I will be quickly lost.
Regards
ADO.NET works generally in a disconnected state. Unlike ADO Recordsets, which maintain an open cursor on a database, ADO.NET generally works by opening a connection, retrieving data into your application, closing the connection, then using the local data cache in your application. If you make changes to the data, you then open a connection again and save those changes in a batch. A DataTable is that local data cache.
Where a DataTable gives you random access to the data to get and set values, a data reader is used when you need read-only, forward-only access to the data. A data adapter actually uses a data reader under the hood to populate a DataTable when you call Fill. Calling Load on the DataTable yourself is like doing that without the additional overhead of the data adapetr. You might use a data reader directly if you want to use and discard each record as you read it. Processing each row as you read it means not using the memory required to store the data locally.
When you bind data to your UI in WinForms, you need something to bind. It's generally a good idea to use a BindingSource as it provides one place to access all aspects of your bound data, e.g. navigation and currency. The BindingSource is just a way to access the data though. It still needs data to access and that data can be stored in various forms. You can create a DataTable and bind that, in which case your control will communicate with the BindingSource and the BindingSource will communicate with the DefaultView of the DataTable. As a data reader is read-only, forward-only, a BindingSource can't use it as a data source directly. If you assign a data reader to the DataSource property of a BindingSource, the data will be read from that data reader and used to populate an object that implements the IBindingList(Of T) interface (I haven't checked but that's probably a BindingList(Of T)). That object can then be navigated and accessed much as the DataTable/DataView can. If you're using the BindingSource in code though, the actual type of the underlying list is irrelevant.
Note that, just like the data reader, this IBindingList(Of T) is not intended for editing; just reading. There is less overhead to using a data reader and an IBindingList(Of DataRecordInternal) than there is to using a data adapter and a DataTable so, if you only need to read the data, e.g. a list of items to select from in a ComboBox, binding a data reader is a better option.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|