What is the best solution to save the changes to the sql server database

T

Tony Johansson

Hello!

I have used a data reader to read the database table and load into a Class
objects and then add all these object into a generic list and then bind this
to a DataGridView.

Now if I change something and want to update the database with the changes
which could be a new row in the DataGridView
or a row has been changed or a row has been deleted what is the best
solution ?

The requirement is that I must use a data reader to load into a class so I
can't for example the DataAdapter.Fill method.

//Tony
 
A

Arne Vajhøj

I have used a data reader to read the database table and load into a Class
objects and then add all these object into a generic list and then bind this
to a DataGridView.

Now if I change something and want to update the database with the changes
which could be a new row in the DataGridView
or a row has been changed or a row has been deleted what is the best
solution ?

The requirement is that I must use a data reader to load into a class so I
can't for example the DataAdapter.Fill method.

If the change was done by another app or thread, then you will have
yo re-query.

If the change was done by your app and thread, then you should be
able to refresh based on the in memory data structure without
hitting the database.

Arne
 
R

RayLopez99

Hello!

I have used a data reader to read the database table and load into a Class
objects and then add all these object into a generic list and then bind this
to a DataGridView.

Now if I change something and want to update the database with the changes
which could be a new row in the DataGridView
or a row has been changed or a row has been deleted what is the best
solution ?

The requirement is that I must use a data reader to load into a class so I
can't for example the DataAdapter.Fill method.

//Tony

In .NET, you can achieve what you want without fear by making all your
classes depend on these two parent: INotifyPropertyChanged and
IEditableObject, like this:

: IEditableObject,INotifyPropertyChanged

Then you must implement the three classes as shown below:

public class myclass : IEditableObject, INotifyPropertyChanged
{

public void BeginEdit()
{
}

public void CancelEdit()
{
}

public void EndEdit()
{
}

//other stuff here.

public event PropertyChangedEventHandler PropertyChanged;

private void onPropertyChanged(object sender, string
propertyName)
{
//Google this to get the right format
see here: http://msdn.microsoft.com/en-us/lib...l.inotifypropertychanged.propertychanged.aspx

If you do the above you will never go wrong.

The only design issue you will face is this: should I update the
server everytime a change is made client side, or wait a bit? Biggest
example: a list box that changes. If you don't update, the list box
is not complete. But if you update, the screen refresh takes time.
Most times I don't update because most times the user does not really
need the most current list box contents. Or you can put a button and
tell the user "if you need to refresh, for xyz reason, click this
button".

RL
 
O

Osamede.Zhang

Hello!

I have used a data reader to read the database table and load into a Class
objects and then add all these object into a generic list and then bind this
to a DataGridView.

Now if I change something and want to update the database with the changes
which could be a new row in the DataGridView
or a row has been changed or a row has been deleted what is the best
solution ?

The requirement is that I must use a data reader to load into a class so I
can't for example the DataAdapter.Fill method.

//Tony

you must know which object is changed,and update one by one
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top