Click here to Skip to main content
15,897,518 members
Articles / Operating Systems / Windows
Article

Auto Synchonize between DataSet And database

Rate me:
Please Sign up or sign in to vote.
3.50/5 (3 votes)
2 Jan 2007CPOL3 min read 38.6K   475   28   8
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:

    C#
    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.

    C#
    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:

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

    C#
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other UMH
Belgium Belgium
* Bachelor of Information Management at ISAT Charleroi in Belgium.
* Master II of Information science at UMH Mons.

I spend my time in small projects which can help me or small enterprises and
I am very interested in designing projects
with specifications of UML or in applying usual design patterns.

Comments and Discussions

 
GeneralDifference between DataSet and DataTable in asp.net Pin
Lalit24rocks15-May-13 19:46
Lalit24rocks15-May-13 19:46 
QuestionFiles corrupted? Pin
duffy24829-Dec-06 5:36
duffy24829-Dec-06 5:36 
AnswerRe: Files corrupted? Pin
BlaiseBraye29-Dec-06 6:16
BlaiseBraye29-Dec-06 6:16 
GeneralRe: Files corrupted? Pin
duffy2481-Jan-07 23:40
duffy2481-Jan-07 23:40 
GeneralRe: Files corrupted? Pin
BlaiseBraye2-Jan-07 11:32
BlaiseBraye2-Jan-07 11:32 
GeneralNow it looks good... Pin
duffy2483-Jan-07 23:58
duffy2483-Jan-07 23:58 
GeneralReflection is not spelled with an x Pin
robrich29-Nov-06 5:10
robrich29-Nov-06 5:10 
GeneralRe: Reflection is not spelled with an x Pin
BlaiseBraye29-Nov-06 6:23
BlaiseBraye29-Nov-06 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.