Click here to Skip to main content
15,891,789 members
Articles / Programming Languages / XML

Create Data Classes

Rate me:
Please Sign up or sign in to vote.
4.88/5 (31 votes)
4 Mar 2011CPOL10 min read 135.7K   2.5K   167  
An application that creates a C# class to read/write data to/from an Access, SQLite, or XML database.
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;


namespace JGClassGenerator
{
	internal class JTestADODBData
	{
		private String m_JAnsiString = "";
		private String m_JAnsiStringFixedLength = "";
		private Object m_JBinary = null;
		private Boolean m_JBoolean = false;
		private Byte m_JByte = 0;
		private Decimal m_JCurrency = 0;
		private DateTime m_JDate;
		private DateTime m_JDateTime;
		private DateTime m_JDateTime2;
		private DateTime m_JDateTimeOffset;
		private Decimal m_JDecimal = 0;
		private Double m_JDouble = 0;
		private Guid m_JGuid = new Guid();
		private Int16 m_JInt16 = 0;
		private Int32 m_JInt32 = 0;
		private Int64 m_JInt64 = 0;
		private Object m_JObject = null;
		private SByte m_JSByte = 0;
		private Single m_JSingle = 0;
		private String m_JString = "None";
		private String m_JStringFixedLength = "";
		private DateTime m_JTime;
		private UInt16 m_JUInt16 = 0;
		private UInt32 m_JUInt32 = 0;
		private UInt64 m_JUInt64 = 0;
		private Double m_JVarNumeric = 0;
		private String m_JXml = "";
		private JCommon.ClassDataTypes m_JUserDefined = JCommon.ClassDataTypes.NONE;

		//	Bare constructor
		internal JTestADODBData()
		{
		}

		//	Constructor from key field number
		internal JTestADODBData(Int32 jInt32)
		{
			JTestADODBUpdater.GetRecord(jInt32, this);
		}

		//	Constructor from String Override
		internal JTestADODBData(String jString)
		{
			JTestADODBUpdater.GetRecord(jString, this);
		}

		//	String Override for main class - must be public
		public override string ToString()
		{
			return this.m_JString;
		}

		//	Lightweight class for adding to ListView, Combo etc
		internal class JTestADODBIndexData
		{
			internal Int32 JInt32 = 0;
			internal String JString = "None";

			internal JTestADODBIndexData()
			{
			}

			internal JTestADODBIndexData(JTestADODBData testADODBData)
			{
				this.JInt32 = testADODBData.JInt32;
				this.JString = testADODBData.JString;
			}

			//	String Override for index data - must be public
			public override string ToString()
			{
				return this.JString;
			}
		}

		#region Public Properties

		//	TODO Check if some should be read only

		internal String JAnsiString
		{
			get { return this.m_JAnsiString; }
			set { this.m_JAnsiString = value; }
		}

		internal String JAnsiStringFixedLength
		{
			get { return this.m_JAnsiStringFixedLength; }
			set { this.m_JAnsiStringFixedLength = value; }
		}

		internal Object JBinary
		{
			get { return this.m_JBinary; }
			set { this.m_JBinary = value; }
		}

		internal Boolean JBoolean
		{
			get { return this.m_JBoolean; }
			set { this.m_JBoolean = value; }
		}

		internal Byte JByte
		{
			get { return this.m_JByte; }
			set { this.m_JByte = value; }
		}

		internal Decimal JCurrency
		{
			get { return this.m_JCurrency; }
			set { this.m_JCurrency = value; }
		}

		internal DateTime JDate
		{
			get { return this.m_JDate; }
			set { this.m_JDate = value; }
		}

		internal DateTime JDateTime
		{
			get { return this.m_JDateTime; }
			set { this.m_JDateTime = value; }
		}

		internal DateTime JDateTime2
		{
			get { return this.m_JDateTime2; }
			set { this.m_JDateTime2 = value; }
		}

		internal DateTime JDateTimeOffset
		{
			get { return this.m_JDateTimeOffset; }
			set { this.m_JDateTimeOffset = value; }
		}

		internal Decimal JDecimal
		{
			get { return this.m_JDecimal; }
			set { this.m_JDecimal = value; }
		}

		internal Double JDouble
		{
			get { return this.m_JDouble; }
			set { this.m_JDouble = value; }
		}

		internal Guid JGuid
		{
			get { return this.m_JGuid; }
			set { this.m_JGuid = value; }
		}

		internal Int16 JInt16
		{
			get { return this.m_JInt16; }
			set { this.m_JInt16 = value; }
		}

		internal Int32 JInt32
		{
			get { return this.m_JInt32; }
			set { this.m_JInt32 = value; }
		}

		internal Int64 JInt64
		{
			get { return this.m_JInt64; }
			set { this.m_JInt64 = value; }
		}

		internal Object JObject
		{
			get { return this.m_JObject; }
			set { this.m_JObject = value; }
		}

		internal SByte JSByte
		{
			get { return this.m_JSByte; }
			set { this.m_JSByte = value; }
		}

		internal Single JSingle
		{
			get { return this.m_JSingle; }
			set { this.m_JSingle = value; }
		}

		internal String JString
		{
			get { return this.m_JString; }
			set { this.m_JString = value; }
		}

		internal String JStringFixedLength
		{
			get { return this.m_JStringFixedLength; }
			set { this.m_JStringFixedLength = value; }
		}

		internal DateTime JTime
		{
			get { return this.m_JTime; }
			set { this.m_JTime = value; }
		}

		internal UInt16 JUInt16
		{
			get { return this.m_JUInt16; }
			set { this.m_JUInt16 = value; }
		}

		internal UInt32 JUInt32
		{
			get { return this.m_JUInt32; }
			set { this.m_JUInt32 = value; }
		}

		internal UInt64 JUInt64
		{
			get { return this.m_JUInt64; }
			set { this.m_JUInt64 = value; }
		}

		internal Double JVarNumeric
		{
			get { return this.m_JVarNumeric; }
			set { this.m_JVarNumeric = value; }
		}

		internal String JXml
		{
			get { return this.m_JXml; }
			set { this.m_JXml = value; }
		}

		internal JCommon.ClassDataTypes JUserDefined
		{
			get { return this.m_JUserDefined; }
			set { this.m_JUserDefined = value; }
		}


		#endregion

		#region Additional Fields

		//	Derived/Calculated Fields


		internal bool IsADONET
		{
			get { return this.JUserDefined == JCommon.ClassDataTypes.ADONET; }
		}

		#endregion

	}


	#region Updater Class


	static class JTestADODBUpdater
	{

		private static string m_DBPath = @"D:\Data\Visual Studio 2008\Projects\JGClassGenerator\Tests\JTypes.mdb";
		private static string m_TableName = "tblTest";

		internal static string DatabasePath
		{
			get { return m_DBPath; }
			set { m_DBPath = value; }
		}

		internal static string TableName
		{
			get { return m_TableName; }
			set { m_TableName = value; }
		}

		//	Returns all records in a List
		internal static List<JTestADODBData> AllRecords()
		{
			List<JTestADODBData> listRecords = new List<JTestADODBData>();
			JTestADODBData testADODBData;

			if (!DBFileExists())
				return null;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();
			//	Create the SelectCommand.
			string selectString = "SELECT *  FROM " + m_TableName;
			//	TODO Check Ordering
			selectString += " ORDER BY JString";
			//	Get Recordset
			adRS = GetRecordset(ref adCON, selectString);

			// Loop through the Dataset and add each Row to the List
			if (adRS != null)
			{
				while (!adRS.EOF)
				{
					testADODBData = new JTestADODBData();
					GetRecordDetails(adRS, ref testADODBData);
					listRecords.Add(testADODBData);
					adRS.MoveNext();
				}
			}

			return listRecords;
		}

		//	Returns all index data in a List
		internal static List<JTestADODBData.JTestADODBIndexData> AllIndexData()
		{
			List<JTestADODBData.JTestADODBIndexData> listRecords = new List<JTestADODBData.JTestADODBIndexData>();
			JTestADODBData.JTestADODBIndexData indexData;

			if (!DBFileExists())
				return null;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();
			//	Create the SelectCommand.
			string selectString = "SELECT *  FROM " + m_TableName;
			//	TODO Check Ordering
			selectString += " ORDER BY JString";
			//	Get Recordset
			adRS = GetRecordset(ref adCON, selectString);

			// Loop through the Dataset and add each Row to the List
			if (adRS != null)
			{
				while (!adRS.EOF)
				{
					indexData = new JTestADODBData.JTestADODBIndexData();
					try
					{
						indexData.JInt32 = Convert.ToInt32(GetField(adRS, "JInt32"));
					}
					catch { }
					indexData.JString = Convert.ToString(GetField(adRS, "JString"));
					listRecords.Add(indexData);
					adRS.MoveNext();
				}
			}

			return listRecords;
		}

		private static bool DBFileExists()
		{
			FileInfo fInfo;

			try
			{
				fInfo = new FileInfo(m_DBPath);
				return fInfo.Exists;
			}
			catch
			{
				return false;
			}
		}

		private static ADODB.Recordset GetRecordset(ref ADODB.Connection adCON, string selectString)
		{
			adCON = new ADODB.Connection();
			ADODB.Recordset adRS = new ADODB.Recordset();
			string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
			connectionString += m_DBPath;
			connectionString += ";";
			try
			{
				adCON.Open(connectionString, "", "", 0);
				adRS.Open(selectString, adCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, 0);
				return adRS;
			}
			catch
			{
				return null;
			}
		}

		internal static bool DeleteRecord(Int32 jInt32)
		{
			if (!DBFileExists())
				return false;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			bool result = false;

			string selectString = "SELECT * FROM " + m_TableName + " WHERE JInt32 = ";
			selectString += jInt32.ToString();

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (adRS.EOF)
				{
					result = false;
				}
				else
				{
					adRS.Delete(ADODB.AffectEnum.adAffectCurrent);
					result = true;
				}

				adRS.Close();
				adCON.Close();
			}
			return result;
		}

		//	DeleteRecord From Instance
		internal static bool DeleteRecord(JTestADODBData testADODBData)
		{
			return DeleteRecord(testADODBData.JInt32);
		}

		internal static bool GetRecord(int keyField, JTestADODBData testADODBData)
		{
			if (!DBFileExists())
				return false;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			bool result = false;

			string selectString = "SELECT * FROM " + m_TableName + " WHERE JInt32 = ";
			selectString += keyField.ToString();

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (!adRS.EOF)
				{
					GetRecordDetails(adRS, ref testADODBData);
				}
				else
				{
					testADODBData.JInt32 = -1;
				}

				adRS.Close();
				adCON.Close();
			}
			return result;
		}

		internal static bool GetRecord(string jString, JTestADODBData testADODBData)
		{
			if (!DBFileExists())
				return false;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			bool result = false;

			string selectString = "SELECT * FROM " + m_TableName + " WHERE JString = ";
			selectString += "\"";
			selectString += jString;
			selectString += "\"";

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (!adRS.EOF)
				{
					GetRecordDetails(adRS, ref testADODBData);
				}
				else
				{
					testADODBData.JInt32 = -1;
				}

				adRS.Close();
				adCON.Close();
			}
			return result;
		}

		//	GetRecord (From selectString)
		internal static JTestADODBData GetSingleRecord(string selectString)
		{
			JTestADODBData testADODBData = new JTestADODBData();
			GetSingleRecord(selectString, ref testADODBData);
			return testADODBData;
		}

		internal static void GetSingleRecord(string selectString, ref JTestADODBData testADODBData)
		{
			if (!DBFileExists())
			{
				testADODBData = null;
				return;
			}

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (!adRS.EOF)
				{
					GetRecordDetails(adRS, ref testADODBData);
				}
				else
				{
					testADODBData.JInt32 = -1;
				}

				adRS.Close();
				adCON.Close();
			}
			return;
		}

		private static bool GetRecordDetails(ADODB.Recordset adRS, ref JTestADODBData testADODBData)
		{
			bool result = true;
			try
			{
				//	Use try catch for numeric fields which may not have been initialised
				testADODBData.JAnsiString = Convert.ToString(GetField(adRS, "JAnsiString"));
				testADODBData.JAnsiStringFixedLength = Convert.ToString(GetField(adRS, "JAnsiStringFixedLength"));
				testADODBData.JBinary = GetField(adRS, "JBinary");
				try
				{
					testADODBData.JBoolean = Convert.ToBoolean(GetField(adRS, "JBoolean"));
				}
				catch { }
				try
				{
					testADODBData.JByte = Convert.ToByte(GetField(adRS, "JByte"));
				}
				catch { }
				try
				{
					testADODBData.JCurrency = Convert.ToDecimal(GetField(adRS, "JCurrency"));
				}
				catch { }
				try
				{
					testADODBData.JDate = Convert.ToDateTime(GetField(adRS, "JDate"));
				}
				catch { }
				try
				{
					testADODBData.JDateTime = Convert.ToDateTime(GetField(adRS, "JDateTime"));
				}
				catch { }
				try
				{
					testADODBData.JDateTime2 = Convert.ToDateTime(GetField(adRS, "JDateTime2"));
				}
				catch { }
				try
				{
					testADODBData.JDateTimeOffset = Convert.ToDateTime(GetField(adRS, "JDateTimeOffset"));
				}
				catch { }
				try
				{
					testADODBData.JDecimal = Convert.ToDecimal(GetField(adRS, "JDecimal"));
				}
				catch { }
				try
				{
					testADODBData.JDouble = Convert.ToDouble(GetField(adRS, "JDouble"));
				}
				catch { }
				testADODBData.JGuid = new Guid(Convert.ToString(GetField(adRS, "JGuid")));
				try
				{
					testADODBData.JInt16 = Convert.ToInt16(GetField(adRS, "JInt16"));
				}
				catch { }
				try
				{
					testADODBData.JInt32 = Convert.ToInt32(GetField(adRS, "JInt32"));
				}
				catch { }
				try
				{
					testADODBData.JInt64 = Convert.ToInt64(GetField(adRS, "JInt64"));
				}
				catch { }
				testADODBData.JObject = GetField(adRS, "JObject");
				try
				{
					testADODBData.JSByte = Convert.ToSByte(GetField(adRS, "JSByte"));
				}
				catch { }
				try
				{
					testADODBData.JSingle = Convert.ToSingle(GetField(adRS, "JSingle"));
				}
				catch { }
				testADODBData.JString = Convert.ToString(GetField(adRS, "JString"));
				testADODBData.JStringFixedLength = Convert.ToString(GetField(adRS, "JStringFixedLength"));
				try
				{
					testADODBData.JTime = Convert.ToDateTime(GetField(adRS, "JTime"));
				}
				catch { }
				try
				{
					testADODBData.JUInt16 = Convert.ToUInt16(GetField(adRS, "JUInt16"));
				}
				catch { }
				try
				{
					testADODBData.JUInt32 = Convert.ToUInt32(GetField(adRS, "JUInt32"));
				}
				catch { }
				try
				{
					testADODBData.JUInt64 = Convert.ToUInt64(GetField(adRS, "JUInt64"));
				}
				catch { }
				try
				{
					testADODBData.JVarNumeric = Convert.ToDouble(GetField(adRS, "JVarNumeric"));
				}
				catch { }
				testADODBData.JXml = Convert.ToString(GetField(adRS, "JXml"));
				try
				{
					testADODBData.JUserDefined = (JCommon.ClassDataTypes)Convert.ToInt32(GetField(adRS, "JUserDefined"));
				}
				catch { }
			}
			catch
			{
				result = false;
			}

			return result;
		}

		internal static bool UpdateRecord(JTestADODBData testADODBData)
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			bool result = false;

			object[] fList = 
				{
					"JAnsiString",
					"JAnsiStringFixedLength",
					"JBinary",
					"JBoolean",
					"JByte",
					"JCurrency",
					"JDate",
					"JDateTime",
					"JDateTime2",
					"JDateTimeOffset",
					"JDecimal",
					"JDouble",
					"JGuid",
					"JInt16",
					"JInt64",
					"JObject",
					"JSByte",
					"JSingle",
					"JString",
					"JStringFixedLength",
					"JTime",
					"JUInt16",
					"JUInt32",
					"JUInt64",
					"JVarNumeric",
					"JXml",
					"JUserDefined"
				};

			object[] fValues = 
				{
					testADODBData.JAnsiString,
					testADODBData.JAnsiStringFixedLength,
					testADODBData.JBinary,
					testADODBData.JBoolean,
					testADODBData.JByte,
					testADODBData.JCurrency,
					testADODBData.JDate,
					testADODBData.JDateTime,
					testADODBData.JDateTime2,
					testADODBData.JDateTimeOffset,
					testADODBData.JDecimal,
					testADODBData.JDouble,
					testADODBData.JGuid.ToString(),
					testADODBData.JInt16,
					testADODBData.JInt64,
					testADODBData.JObject,
					testADODBData.JSByte,
					testADODBData.JSingle,
					testADODBData.JString,
					testADODBData.JStringFixedLength,
					testADODBData.JTime,
					testADODBData.JUInt16,
					testADODBData.JUInt32,
					testADODBData.JUInt64,
					testADODBData.JVarNumeric,
					testADODBData.JXml,
					(Int32)testADODBData.JUserDefined
				};

			string selectString = "SELECT * FROM " + m_TableName + " WHERE JInt32 = ";
			selectString += testADODBData.JInt32.ToString();

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (adRS.EOF)
				{
					adRS.AddNew(fList, fValues);
				}
				else
				{
					adRS.Update(fList, fValues);
				}
				adRS.Close();
				adCON.Close();
				result = true;
			}
			return result;
		}

		internal static object GetJDate(int keyfield)
		{
			object obTemp = null;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			string selectString = "SELECT * FROM " + m_TableName + " WHERE JInt32 = ";
			selectString += keyfield.ToString();

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (!adRS.EOF)
				{
					obTemp = Convert.ToDateTime(GetField(adRS, "JDate"));
				}

				adRS.Close();
				adCON.Close();
			}
			return obTemp;
		}

		internal static bool UpdateJDate(JTestADODBData testADODBData)
		{
			return UpdateField(testADODBData, "JDate", testADODBData.JDate);
		}

		internal static object GetJDouble(int keyfield)
		{
			object obTemp = null;

			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			string selectString = "SELECT * FROM " + m_TableName + " WHERE JInt32 = ";
			selectString += keyfield.ToString();

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (!adRS.EOF)
				{
					obTemp = Convert.ToDouble(GetField(adRS, "JDouble"));
				}

				adRS.Close();
				adCON.Close();
			}
			return obTemp;
		}

		internal static bool UpdateJDouble(JTestADODBData testADODBData)
		{
			return UpdateField(testADODBData, "JDouble", testADODBData.JDouble);
		}

		private static object GetField(ADODB.Recordset adRS, string fieldName)
		{
			object obTemp = null;
			if (adRS.Fields[fieldName] != null)
				obTemp = adRS.Fields[fieldName].Value;
			return obTemp;
		}



		internal static bool UpdateField(JTestADODBData testADODBData, string fieldName, object value)
		{
			ADODB.Connection adCON = new ADODB.Connection();
			ADODB.Recordset adRS;

			bool result = false;

			object[] fList = { fieldName };
			object[] fValues = { value };

			string selectString = "SELECT * FROM " + m_TableName;
			selectString += " WHERE JInt32 = ";
			selectString += testADODBData.JInt32.ToString();

			adRS = GetRecordset(ref adCON, selectString);

			if (adRS != null)
			{
				if (!adRS.EOF)
					adRS.Update(fList, fValues);

				adRS.Close();
				adCON.Close();
				result = true;
			}
			return result;
		}


	#endregion

		#region Additional Functions

		internal static object GetRecordByFLString(string ansiStringFixedLength)
		{
			string selectString = "SELECT * FROM " + m_TableName;
			selectString += " WHERE JAnsiStringFixedLength = '" + ansiStringFixedLength + "'";
			return GetSingleRecord(selectString);
		}

		#endregion
	}


}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Retired
United Kingdom United Kingdom
I have been a keen hobbyist programmer since getting my first computer - a Vic 20 (you had to be able to write programs then since few programs were available and all were expensive).
Retired and now living in Pewsey, Wiltshire, where I spend (far too much of) my time writing computer programs to keep my mind active.

Comments and Discussions