Reputation:
I'm an old VB programmer trying to build an application in C#. Simple idea, really -- just want to have a cheesy form with 3 textboxes (populated from a database), and two buttons: Move Next and Move Previous.
In the old VB6 days I'd open a static connection on the Form_Load event, then open a recordset and populate my textboxes. My "Move Next" and "Move Previous" buttons would simply have "rst.MoveNext" and "rst.MovePrevious" code behind, along with something like [Textbox1.Text = rst("Field1")]. Then, finally on the Form's closing event, I'd have something like cnn.close.
From what I've read, this is not the way to do things in C#. Static database connections are taboo thanks to connection pooling. But this leaves some room for me to wonder: How do I redesign my little C# application without the static recordset and db connection?
If I open the connection, load the first record onto my form on the Load event (ie: SELECT TOP 1 FROM MYTABLE), then close the connection, how do I manage my MoveNext and MovePrevious buttons? Should I reopen the connection, and somehow try to query for the "Next Lowest" or "Next Highest" primary key in the table?
Or am I simply approaching this the wrong way entirely?
Any help is greatly appreciated!!!
Thanks, Jason
Upvotes: 2
Views: 76
Reputation: 2387
In VB6, the framework was trying to do a lot of the work for you, in a very specific way. With the .NET Framework they realized that the way it was done with the recordset did not fit everyone's need, so they gave the developers more control by using different implementations of data access patterns. As Patrick mentions the DataSet is one of them, but then there is also the LINQ way of doing it.
You could also load a buffer and navigate through it with the next previous and then load more when(if) you reach the end of that buffer.
Upvotes: 1
Reputation: 736
What you want to do is an DataSet, you can insert one from you Database using the DataSourceWindow View->Other Windows -> Data Sources View, with a DataSet you can drag and drop fields and you have the utility BindingSource Navigator Take a look on this link How to Set Up and DataSet
Upvotes: 1