65.9K
CodeProject is changing. Read more.
Home

Auto Synchonize between DataSet And database

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (3 votes)

Nov 29, 2006

CPOL

3 min read

viewsIcon

39027

downloadIcon

477

This tool allows us to easily load and update a dataset without thinking about the database anymore.

Sample Image - TransacDataSetAdapter.jpg

Introduction

Two weeks ago, I posted a first version of a library which was able to synchronize data between a generated dataset (by CSC) and a database. To do this job, the only requirement for the tool is a DataSet with DataAdapters.

But after using this library, I realized two real problems in the production environment.

First, if there were some errors during the saving process, I didn't know where the problem was. I have fixed this problem using the well known "Log4Net" assembly. Here are two sources which helped me to use it:

Secondly, the transaction was not implemented. Then if an error had occurred, some data was saved... other data was not. In order to do this job efficiently, I propose this good solution:

Using the Code

In order to use the TransacDatasetAdapter, you will have to:

  1. Add a reference to the assembly DataDigest.Datas in your project
  2. Add the configuration requirements if you need the log features. In my sample project, I propose a configuration which will save your logs in a flat text file. You could find more information in the URI proposed in the introduction.

All is done... may be you need to know how to use this library now.

If you have read the previous article about DataSetAdaptor (I had called it Adaptor... now I know it has to be named DataSetAdapter), you won't be lost. In fact in this project, I have made some refactoring in the first version and then I have extended the DataSetAdapter base with TransacDataSetAdapter. Using it is the same philosophy, the only difference is the naming convention, and a connection property which needs to be set (using the adapter associated with the datatable that you give in the parameter).

Now, let's move on to what you are certainly more interested in.

  1. Init the adaptor:

    TransacDatasetAdapter<SAMPLEDBDATASET> dtsAdaptor
        = new TransacDatasetAdapter<SAMPLEDBDATASET>(dataSet);
  2. Set the connection property (this connection will be used for the transaction). As you can see, it is a datatable which has been sent. By reflection, the assembly will find its tableadaptor and get its connection property.

    dtsAdaptor.SetConnection(dataSet.Car);
  3. Loading process, same idea as in the past...
    To download data from database to dataset, you will specify it to the adaptor like that:

    dtsAdaptor.Fill();
  4. Saving process, same idea as in the past...

    dtsAdaptor.SaveChanges();

The Design of the Application

TransacDatasetAdapter Design

Conclusion

Not a lot of work was needed to make this code really more robust... may be not the best yet... But not too far I hope. Thank you for your feedback in order to improve (or not) this tool.

Little Tip

I wanted to refresh the content of my log file during my tests... I remembered an existing tool on the Linux environment... therefore I had a look at "tail" for Windows... I found it here.
May be there are other tools to do it, but this one was clearly enough for me.

History

  • 7th November, 2006
    • Idea of DataSetAdaptor from here
  • 22nd November, 2006
    • A first try at implementing error handler
  • 29th November, 2006
    • DataSetAdaptor renamed to DataSetAdapter (stupid but logical)
    • DataSetAdapter extended by TransacDataSetAdapter
    • Log4Net implemented