Click here to Skip to main content
15,885,278 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.4K   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.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

/*
 * Copyright Jeff Gaines 2007 - 2011 (jeff@jgaines.co.uk)
 * Free licence for private use
 * Please contact the author if you wish to use the code commercially
*/

namespace JGClassGenerator
{
	internal partial class FormSettings : FormBaseDialog
	{
		internal bool SettingsSaved = false;

		internal FormSettings()
		{
			InitializeComponent();
			ShowButtons = true;
		}

		private void FormSettings_Load(object sender, EventArgs e)
		{
			ButtonCancelText = "Close";
			ButtonOKTooltip = "Save Settings";
			ButtonCancelTooltip = "Discard Changes";
			lblProgDBPath.Text = "";
			lblProjectDBPath.Text = "";
			SetHelpString();
			LoadSettings();

			string appPath = Application.StartupPath;
			string dbPath = Path.GetDirectoryName(appPath);
			dbPath = Path.GetDirectoryName(dbPath);
			dbPath = Path.Combine(dbPath, "Database");

			pickProgramDBPath.InitialDirectory = dbPath;
			pickSavedProjectsDBPath.InitialDirectory = dbPath;
		}


		internal override void ButtonOK_Click(object sender, EventArgs e)
		{
			SaveSettings();
			base.ButtonOK_Click(sender, e);
		}

		private void LoadSettings()
		{
			try
			{
				JCommon.ClassDataTypes dataType = (JCommon.ClassDataTypes)Properties.Settings.Default.SavedProjectDataType;

				switch (dataType)
				{
					case JCommon.ClassDataTypes.ADONET:
						rbAccess12.Checked = true;
						break;

					case JCommon.ClassDataTypes.SQLITE:
						rbSQLite.Checked = true;
						break;
				}

				pickProgramDBPath.SelectedPath = Properties.Settings.Default.ProgramDatabasePath;
				pickSavedProjectsDBPath.SelectedPath = Properties.Settings.Default.SavedProjectsDatabasePath;
			}
			catch
			{
			}
			chkAddDefaultFields.Checked = Properties.Settings.Default.AddDefaultFields;
		}

		private void rbAccess12_CheckedChanged(object sender, EventArgs e)
		{
			pickProgramDBPath.SelectedPath = "";
			pickSavedProjectsDBPath.SelectedPath = "";
			SetFileFilter();
			lblProgDBPath.Text = "JGClassGenerator.accdb";
			lblProjectDBPath.Text = "JGSavedProjects.accdb";
		}

		private void rbSQLite_CheckedChanged(object sender, EventArgs e)
		{
			pickProgramDBPath.SelectedPath = "";
			pickSavedProjectsDBPath.SelectedPath = "";
			SetFileFilter();
			lblProgDBPath.Text = "JGClassGenerator.db3";
			lblProjectDBPath.Text = "JGSavedProjects.db3";
		}

		private void SaveSettings()
		{
			if (!rbAccess12.Checked && !rbSQLite.Checked)
			{
				ShowMessage("Please select data type for saving projects", true);
				return;
			}

			string programDBPath = pickProgramDBPath.SelectedPath;

			if (string.IsNullOrEmpty(programDBPath))
			{
				ShowMessage("Please Set Program Database Path", true);
				SettingsSaved = false;
				return;
			}

			try
			{
				FileInfo fInfo = new FileInfo(programDBPath);
				if (!fInfo.Exists)
				{
					ShowMessage("Cannot Find Program Database File", true);
					SettingsSaved = false;
					return;
				}
			}
			catch
			{
				ShowMessage("Cannot Find Program Database File", true);
				SettingsSaved = false;
				return;
			}

			string projectDBPath = pickSavedProjectsDBPath.SelectedPath;

			if (string.IsNullOrEmpty(projectDBPath))
			{
				ShowMessage("Please Set Saved Projects Database Path", true);
				SettingsSaved = false;
				return;
			}

			try
			{
				FileInfo fInfo = new FileInfo(projectDBPath);
				if (!fInfo.Exists)
				{
					ShowMessage("Cannot Find Saved Projects Database File", true);
					SettingsSaved = false;
					return;
				}
			}
			catch
			{
				ShowMessage("Cannot Find Saved Projects Database File", true);
				SettingsSaved = false;
				return;
			}

			JCommon.ClassDataTypes dataType = JCommon.ClassDataTypes.ADONET;

			if (rbAccess12.Checked)
				dataType = JCommon.ClassDataTypes.ADONET;
			else if (rbSQLite.Checked)
				dataType = JCommon.ClassDataTypes.SQLITE;

			Properties.Settings.Default.SavedProjectDataType = (int)dataType;
			Properties.Settings.Default.ProgramDatabasePath = programDBPath;
			Properties.Settings.Default.AddDefaultFields = chkAddDefaultFields.Checked;
			Properties.Settings.Default.SavedProjectsDatabasePath = projectDBPath;

			Properties.Settings.Default.Save();

			JCommon.ProjectDataType = (JCommon.ClassDataTypes)Properties.Settings.Default.SavedProjectDataType;
			JCommon.ProgramDatabasePath = Properties.Settings.Default.ProgramDatabasePath;
			JCommon.SavedProjectsDatabasePath = Properties.Settings.Default.SavedProjectsDatabasePath;

			SettingsSaved = true;
		}

		private void SetFileFilter()
		{
			string fileFilter = "";

			if (rbAccess12.Checked)
			{
				fileFilter = "Access DB Files (*.accdb)|*.accdb";
			}
			else if (rbSQLite.Checked)
			{
				fileFilter = "SQLite DB Files (*.db3)|*.db3";
			}

			if (!string.IsNullOrEmpty(fileFilter))
			{
				pickProgramDBPath.FileFilter = fileFilter;
				pickSavedProjectsDBPath.FileFilter = fileFilter;
			}
		}

		private void SetHelpString()
		{
			string helpMes = Environment.NewLine;
			helpMes += "These settings are for loading/saving Projects in the program" + Environment.NewLine;
			helpMes += Environment.NewLine;
			helpMes += "They do not affect the settings for your individual projects";
			lblHelp.Text = helpMes;
		}


	}
}

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