Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Create Data Classes

0.00/5 (No votes)
21 Nov 2007 1  
An application that creates an XML database and a C# class to access it
Screenshot - JGCreateDataClasses.jpg

Introduction

I originally wrote this article/application in December 2006 to create an XML database -- and the associated C# classes to use it -- from data entered by the user. My thinking at the time was that I was going to move my data from Access 97 to XML. Since then, I have upgraded to Access 2007 and started to use ADO.NET instead of classic ADO. I decided that I would extend the original application, so it now allows you to do the following:

  • You can save the data you have entered, so you can re-load it at a later stage to extend/change it.
  • You can create C# classes from it for classic ADO (ADODB), ADO.NET and XML databases.
  • You can read in a table from an existing Access database to populate the List View.
  • You can read in an existing XML schema to populate the List View.
  • You can create an Access database from the data you enter.
  • You can create an XML database (and schema) from the data you enter.

The objective of this application is the same as the previous one. It allows me to design the data that I need, i.e. to decide what variables I need in the database, what types those variables should be and whether they should auto-increment. Then it saves this information in a List View. It has a database and a C# class produced and saved for me at the press of a button. I have re-designed the classes it produces. There is now one file containing a class for the data and a separate updater class in the same file.

Using the Code

I hope the code is fairly self-explanatory. It uses a List View to keep the data and allows that data to be added/edited/deleted. When you are happy that it represents what you want in your database, you give it some additional information: where you want the files saved, the namespace, the class name, etc. You then press a couple of buttons and it will produce the files. It's worth saving it at this point. I often discover that I have missed a vital field and so the ability to re-load the original data and just alter it saves a lot of time!

When you first open the C# class in Visual Studio, it won't be formatted very well. Press Ctrl+End; delete the final closing brace; type it in again and it should be formatted in the way Visual Studio normally formats your files. There will be a few "TODO" comments in the C# source. Just check these and make sure they represent what you want.

It allows the recovery of data by an index key or by name. You will need to tweak it if you want to use a different name or key. It also writes a lightweight class consisting of an Index and a Name. You can thus get an array to show in a List View or Combo Box, allowing users to select records easily. The time-saving is because of simple code like this:

//    Public accessors

sw.WriteLine("#region Public Properties");
sw.WriteLine("");
sw.WriteLine("// TODO Check if some should be read only");
sw.WriteLine("");
foreach (JDataRecord dataRecord3 in m_FieldArray)
{    
    sw.WriteLine("public " + 
        dataRecord3.CDataType + " " + dataRecord3.PublicName);
    sw.WriteLine("{");
    sw.WriteLine("get { return this." + dataRecord3.PrivateName + "; }");
    sw.WriteLine("set { this." + dataRecord3.PrivateName + " = value;}");
    sw.WriteLine("}");
    sw.WriteLine("");
}
sw.WriteLine("#endregion");

Instead of repeating the variable names half a dozen times when you write the C# class, you can leave it to the computer to keep churning through it, inserting the names in the right places. It's what computers do well! It uses this simple technique to write functions to add data, update data, delete data, etc.: all the functions I use in accessing data. You may need to add some additional functions to extract data, but the basic functions will be in the C# class.

Points of Interest

One of the classes, JDataTypes, contains a large string array that attempts to map the different names used for data types by Access, C# and XML. I doubt I have used all of these, so there may be some mismatches. Let me know if you find any and I will update the source. I would be interested in any comments you have. I use the program now for all my data based programs, as it saves a lot of repetitive work. However, you never know what could be corrected/improved until other people look at your code!

History

  • 21st November, 2007 - First update
  • 19th December, 2006 - First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here