Click here to Skip to main content
Email Password   helpLost your password?
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:

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralFantastic Job
Rajesh B Jain
4:11 2 Jul '08  
Really, you have created a very useful program. It saved my lot of time. Thanks for that!!!
GeneralThis comes in handy
Bert delaVega
8:17 15 Apr '08  
Thanks for posting this. It's helping in what I'm working on right now.
GeneralGreat
Danielsue
19:53 3 Nov '07  
Great!Thank you very much!
GeneralDatabase
jarell31
12:03 20 Jun '07  
I am need to create a database that will automatically translate error codes. where do i start? I program good in C or C++. i have all of the error codes on a document in Excel. will i be able to use that document?

Thanks
Jarell31
Louisiana State University- Student
AnswerRe: Database
eddyep
22:03 27 Nov '07  
one possibility:
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties= \"Excel 8.0;HDR=YES;IMEX=3\" ";

read in excel with "SELECT * FROM [Tabelle1$] WHERE errorclm = " + errorcode "
errorclm is the column-name in excel
errorcode is the errorcode

next:
give the excel table in a Database (SQL or access or everything and read also

(access) strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path ;

"SELECT * FROM dataname WHERE errorclm = " + errorcode "

errorclm is the field-name in database
errorcode is the errorcode
JokeBest
Recep GUVEN
10:46 13 Jun '07  
Very good..
Thank You Wink
Generaleasier shortcut for reformatting
FrankD
3:08 25 Dec '06  
> ... C# class won't be formatted very well -
> press Ctrl+End, delete the final closing brace,
> and type it in again, and it should be formatted in
> the way Visual Studio normally formats your files.

easier is: [Strg-KD] Wink

ciao Frank
GeneralWhat about msde/sql server 2005 Express edition?
Rob Eckert
15:05 19 Dec '06  
This will work for really small databases, but since it looks like the xml file is read in for each method, larger databases won't be all that efficient. - I would recommend moving to MSDE/SQL Server 2005 Express Edition or some similar (MySQL) database.

Otherwise I would suggest your generated class have a method to load the xml (an 'open' method),
then only write out the xml when records are changed. That will reduce the performance issue of XML being parsed every call to read a record.


GeneralRe: What about msde/sql server 2005 Express edition?
Jeff Gaines
23:32 19 Dec '06  
Thanks for the suggestions Smile
I have never used MSDE or SQL Server so lack the experience to do that, perhaps this could be the basis for somebody with that experience (what are you doing over Christmas...)
I will certainly have a look at ways of speeding it up over time.
GeneralThank YOu!
asflowlaapp
13:20 19 Dec '06  
This is so very very GREAT.... Thanks!

Poke tongue Laugh Wink
GeneralRe: Thank YOu!
Identity.Classified
21:36 18 Feb '07  
Another thanks from me, now i can develop lightweight apps that doesn't require the user to buy MS Office, or Oracle, or set up Paradox in ODBC.

Good job.
GeneralVisual 2003
andre12345
7:56 19 Dec '06  
Would you have a Visual 2003 Version?
GeneralRe: Visual 2003
Jeff Gaines
8:22 19 Dec '06  
I haven't but it doesn't use any special features of VS2005. If you start a VS2003 project and use the name CreateXMLDB you should be able to import the form file and you'll be in business!
GeneralGreat Post!
dbherron
7:47 19 Dec '06  
Thanks for posting this. I've been thinking about creating a similar project but haven't had the time. Wink

Hey, It's C#, how hard can it be?
GeneralRe: Great Post!
Jeff Gaines
8:23 19 Dec '06  
I agree, C# is absolutely brill!


Last Updated 21 Nov 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010