Click here to Skip to main content
15,891,828 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.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Xml.Schema;

namespace CreateXMLDB
{
	public partial class FormCreateXMLDB : Form
	{
		//	 Current item being edited (-1 = new item)
		private int m_intEditItem = -1;

		//	Data types valid for XML file
		private string[] DataTypes =
		{
			"System.Boolean",
			"System.Byte",
			"System.Char",
			"System.DateTime",
			"System.Decimal",
			"System.Double",
			"System.Int16",
			"System.Int32",
			"System.Int64",
			"System.SByte",
			"System.Single",
			"System.String",
			"System.TimeSpan",
			"System.UInt16",
			"System.UInt32",
			"System.UInt64"
		};

		//	Tooltip for buttons
		private ToolTip m_ToolTip = new ToolTip();

		public FormCreateXMLDB()
		{
			InitializeComponent();
		}

		private void FormCreateXMLDB_Load(object sender, EventArgs e)
		{
			FormCreateXMLDBInitialise();
		}

		private void FormCreateXMLDBInitialise()
		{
			//	Put data types in combo
			foreach (string strType in DataTypes)
				cboFieldType.Items.Add(strType);

			//	ToolTips
			m_ToolTip.SetToolTip(btnBrowseWD, "Browse For Working Directory");
			m_ToolTip.SetToolTip(btnCreateClass, "Create C# Class File");
			m_ToolTip.SetToolTip(btnCreateSchema, "Create XML File With Schema");
			m_ToolTip.SetToolTip(btnReadSchema, "Read Existing XML File");
			m_ToolTip.SetToolTip(btnAddField, "Add or Update Field in List View");
			m_ToolTip.SetToolTip(btnRemoveField, "Remove Selected Field from List View");
			m_ToolTip.SetToolTip(cboKeyField, "Select Key / Index Field");
			m_ToolTip.SetToolTip(cboStringOverride, "Select Field For String Override In Class");
		}

		private void btnAddField_Click(object sender, EventArgs e)
		{
			ListViewItem lvItem;

			if (txtFieldName.Text == "" || cboFieldType.Text == "")
			{
				ShowMessage("Field Name and Field Type Required", true);
				return;
			}

			if (m_intEditItem != -1)
			{
				lvItem = lvDatabase.Items[m_intEditItem];
				lvItem.Text = txtFieldName.Text;
				lvItem.SubItems[1].Text = cboFieldType.Text;
				if (chkAutoIncrement.Enabled && chkAutoIncrement.Checked)
					lvItem.SubItems[2].Text = "Yes";
				else
					lvItem.SubItems[2].Text = "No";
			}
			else
			{
				lvItem = new ListViewItem(txtFieldName.Text);
				lvItem.SubItems.Add(cboFieldType.Text);
				if (chkAutoIncrement.Enabled && chkAutoIncrement.Checked)
					lvItem.SubItems.Add("Yes");
				else
					lvItem.SubItems.Add("No");
				lvDatabase.Items.Add(lvItem);
			}

			ClearCurrentField();
			UpdateKeyFieldCombo();
		}

		private void btnBrowseWD_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			if (fbd.ShowDialog() == DialogResult.OK)
				txtWorkingDirectory.Text = fbd.SelectedPath;
		}

		private void btnCreateClass_Click(object sender, EventArgs e)
		{
			if (!IsDataComplete(false))
				return;

			DialogResult dlgResult = DialogResult.Yes;

			string strInitialDirectory = txtWorkingDirectory.Text;
			string strFilter = "C# Source File (*.cs)|*.cs";
			string strFileName = Path.Combine(txtWorkingDirectory.Text, txtClassName.Text);
			if (!strFileName.EndsWith(".cs"))
				strFileName += ".cs";
			bool blnAddExtension = true;

			dlgResult = SaveFileName(strInitialDirectory, strFilter, ref strFileName, blnAddExtension);
			if (dlgResult == DialogResult.OK)
				CreateClass(strFileName);
			else
				ShowMessage("C# Source File NOT Created", true);
		}

		private void btnCreateSchema_Click(object sender, EventArgs e)
		{
			if (!IsDataComplete(true))
				return;

			DialogResult dlgResult = DialogResult.Yes;

			string strInitialDirectory = txtWorkingDirectory.Text;
			string strFilter = "XML Files (*.xml)|*.xml";
			string strFileName = txtXMLDataSetName.Text + ".xml";
			bool blnAddExtension = true;

			dlgResult = SaveFileName(strInitialDirectory, strFilter, ref strFileName, blnAddExtension);
			if (dlgResult == DialogResult.OK)
				CreateSchema(strFileName);
			else
				ShowMessage("XML File NOT Created", true);
		}

		private DialogResult SaveFileName(string strInitialDirectory, string strFilter, ref string strFileName, bool blnAddExtension)
		{
			DialogResult dlgResult = DialogResult.No;
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.InitialDirectory = strInitialDirectory;
			sfd.Filter = strFilter;
			sfd.FileName = strFileName;
			sfd.AddExtension = blnAddExtension;

			dlgResult = sfd.ShowDialog();

			if (dlgResult == DialogResult.OK)
				strFileName = sfd.FileName;
			else
				strFileName = "";

			return dlgResult;
		}

		private void btnReadSchema_Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = "XML Files (*.xml)|*.xml";
			if (ofd.ShowDialog() != DialogResult.OK)
				return;

			FileInfo fInfo = new FileInfo(ofd.FileName);
			if (fInfo.Exists)
				txtWorkingDirectory.Text = fInfo.DirectoryName;
			ReadSchema(ofd.FileName);
			UpdateKeyFieldCombo();
		}

		private void btnRemoveField_Click(object sender, EventArgs e)
		{
			if (lvDatabase.SelectedItems.Count != 1)
			{
				ShowMessage("No Field Selected To Remove", true);
				return;
			}

			ListViewItem lvItem = lvDatabase.SelectedItems[0];
			lvDatabase.Items.Remove(lvItem);
			ClearCurrentField();
		}

		private void cboFieldType_SelectedIndexChanged(object sender, EventArgs e)
		{
			switch (cboFieldType.Text)
			{
				case "System.Int16":
				case "System.Int32":
				case "System.Int64":
				case "System.UInt16":
				case "System.UInt32":
				case "System.UInt64":
					chkAutoIncrement.Enabled = true;
					break;
				default:
					chkAutoIncrement.Enabled = false;
					break;
			}
		}

		private void ClearCurrentField()
		{
			txtFieldName.Text = "";
			cboFieldType.SelectedIndex = -1;
			chkAutoIncrement.Checked = false;
			chkAutoIncrement.Enabled = false;
			m_intEditItem = -1;
			btnAddField.Text = "Add Field";
		}

		private void CreateClass(string strClassFile)
		{
			string strClassName = txtClassName.Text;
			string strKeyfield = cboKeyField.Text;
			string strTemp = "public", strName = "", strType = "";
			string strStringOR = cboStringOverride.Text;
			if (strStringOR == "")
				strStringOR = "TODO";
			string strIndexClass = strClassName + "IndexData";

			using (StreamWriter sw = File.CreateText(strClassFile))
			{
				sw.WriteLine("using System;");
				sw.WriteLine("using System.Collections.Generic;");
				sw.WriteLine("using System.Data;");
				sw.WriteLine("using System.IO;");
				sw.WriteLine("using System.Text;");
				sw.WriteLine("using System.Windows.Forms;");
				sw.WriteLine("");
				sw.WriteLine("namespace " + txtClassNamespace.Text);
				sw.WriteLine("{");
				if (rbPrivate.Checked)
					strTemp = "private";
				else if (rbInternal.Checked)
					strTemp = "internal";
				sw.WriteLine(strTemp + " class " + strClassName);
				sw.WriteLine("{");

				//	Class variables
				foreach (ListViewItem lvItem in lvDatabase.Items)
				{
					strName = lvItem.Text;
					strType = lvItem.SubItems[1].Text;
					strTemp = "private " + strType + " m_" + strName + ";";
					sw.WriteLine(strTemp);
				}

				sw.WriteLine("");
				sw.WriteLine(@"//	XML Data File - Edit as needed");
				strTemp = Path.Combine(txtWorkingDirectory.Text, txtXMLDataSetName.Text);
				strTemp += ".xml";
				sw.WriteLine(@"private static string m_DataFile = @" + "\"" + strTemp + "\";");
				sw.WriteLine("");

				//	Class for indexing
				sw.WriteLine("//	Lightweight class for adding to ListView, Combo etc");
				sw.WriteLine("public class " + strIndexClass);
				sw.WriteLine("{");
				strType = GetTypeFromLV(strKeyfield);
				strType = strType.Replace("System.", "");
				sw.WriteLine("public " + strType + " " + strKeyfield + ";");
				sw.WriteLine("public string " + strStringOR + ";");
				sw.WriteLine("");
				sw.WriteLine("public override string ToString()");
				sw.WriteLine("{");
				sw.WriteLine("return this." + strStringOR + ";");
				sw.WriteLine("}");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	Main class
				strTemp = "public";
				if (rbPrivate.Checked)
					strTemp = "private";
				else if (rbInternal.Checked)
					strTemp = "internal";
				//	Bare constructor
				sw.WriteLine(strTemp + " " + strClassName + "()");
				sw.WriteLine("{");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	Constructor from String Override (on basis it is an important field
				sw.WriteLine(strTemp + " " + strClassName + "(string str" + strStringOR + ")");
				sw.WriteLine("{");
				sw.WriteLine("GetRecord(str" + strStringOR + ");");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	Constructor from Index field
				sw.WriteLine(strTemp + " " + strClassName + "(int int" + strKeyfield + ")");
				sw.WriteLine("{");
				sw.WriteLine("GetRecord(int" + strKeyfield + ");");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	Lightweight array for ListView etc.
				sw.WriteLine("//	Lightweight array for ListView etc.");
				sw.WriteLine("public static " + strClassName + "IndexData[] AllRecords()");
				sw.WriteLine("{");
				sw.WriteLine("if (!DBFileExists(m_DataFile))");
				sw.WriteLine("return null;");
				sw.WriteLine("");
				sw.WriteLine(strIndexClass + "[] recTemp;");
				sw.WriteLine("DataSet dsTemp = new DataSet();");
				sw.WriteLine("DataTable dtTemp;");
				sw.WriteLine("dsTemp.ReadXml(m_DataFile, XmlReadMode.ReadSchema);");
				sw.WriteLine("");
				sw.WriteLine("dtTemp = dsTemp.Tables[\"dataRecord\"];");
				sw.WriteLine("");
				sw.WriteLine("// Select all rows");
				sw.WriteLine("DataRow[] foundRows = dtTemp.Select();");
				sw.WriteLine("");
				sw.WriteLine("if (foundRows.Length > 0)");
				sw.WriteLine("{");
				sw.WriteLine("recTemp = new " + strClassName + "IndexData[foundRows.Length];");
				sw.WriteLine("for (int intRow = 0; intRow < foundRows.Length; intRow++)");
				sw.WriteLine("{");
				sw.WriteLine("recTemp[intRow] = new " + strIndexClass + "();");
				sw.WriteLine("recTemp[intRow]." + strKeyfield + " = Convert.ToInt32(foundRows[intRow][\"" + strKeyfield + "\"]);");
				sw.WriteLine("recTemp[intRow]." + strStringOR + " = Convert.ToString(foundRows[intRow][\"" + strStringOR + "\"]);");
				sw.WriteLine("}");
				sw.WriteLine("return recTemp;");
				sw.WriteLine("}");
				sw.WriteLine("else");
				sw.WriteLine("return null;");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	CreateSchema
				sw.WriteLine("private void CreateSchema(string strFileName)");
				sw.WriteLine("{");

				sw.WriteLine("// Create the DataSet & save the schema");
				sw.WriteLine("DataColumn DataColumnNew;");
				sw.WriteLine("DataSet DataSetNew = new DataSet(\"" + txtXMLDataSetName.Text + "\");");
				sw.WriteLine("DataSetNew.Namespace = \"" + txtXMLNamespace.Text + "\";");
				sw.WriteLine("DataTable DataTableNew = new DataTable(\"dataRecord\");");

				//	Create Columns
				foreach (ListViewItem lvItem in lvDatabase.Items)
				{
					strName = lvItem.Text;
					strType = "\"" + lvItem.SubItems[1].Text + "\"";
					sw.WriteLine("DataColumnNew = new DataColumn(\"" + strName + "\", Type.GetType(" + strType + "));");
					if (lvItem.SubItems[2].Text == "Yes")
						sw.WriteLine("DataColumnNew.AutoIncrement = true;");
					sw.WriteLine("DataTableNew.Columns.Add(DataColumnNew);");
				}

				sw.WriteLine("");
				sw.WriteLine("DataSetNew.Tables.Add(DataTableNew);");
				sw.WriteLine("");
				sw.WriteLine("// Write the schema to an XML file.");
				sw.WriteLine("string xmlFilename = strFileName;");
				sw.WriteLine("");
				sw.WriteLine("// Use WriteXml to write the document.");
				sw.WriteLine("DataSetNew.WriteXml(xmlFilename, XmlWriteMode.WriteSchema);");
				sw.WriteLine("");
				sw.WriteLine("// Dispose of the original DataSet.");
				sw.WriteLine("DataSetNew.Dispose();");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	DBFileExists
				sw.WriteLine("private static bool DBFileExists(string strFilePath)");
				sw.WriteLine("{");
				sw.WriteLine("FileInfo fInfo = new FileInfo(strFilePath);");
				sw.WriteLine("if (fInfo.Exists)");
				sw.WriteLine("return true;");
				sw.WriteLine("else");
				sw.WriteLine("return false;");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	DeleteRecord()
				sw.WriteLine("public bool DeleteRecord()");
				sw.WriteLine("{");
				sw.WriteLine("if (!DBFileExists(m_DataFile))");
				sw.WriteLine("return false;");
				sw.WriteLine("");
				sw.WriteLine("string strQuery;");
				sw.WriteLine("");
				sw.WriteLine("DataSet dsTemp;");
				sw.WriteLine("DataTable dtTemp;");
				sw.WriteLine("");
				sw.WriteLine("dsTemp = new DataSet();");
				sw.WriteLine("dsTemp.ReadXml(m_DataFile, XmlReadMode.ReadSchema);");
				sw.WriteLine("");
				sw.WriteLine("dtTemp = dsTemp.Tables[\"dataRecord\"];");
				sw.WriteLine("strQuery = \"" + strKeyfield + " = '\"" + " + this.m_" + strKeyfield + " +\"'\";");
				sw.WriteLine("");
				sw.WriteLine("// Use the Select method to find all rows matching the filter.");
				sw.WriteLine("DataRow[] foundRows = dtTemp.Select(strQuery);");
				sw.WriteLine("");
				sw.WriteLine("if (foundRows.Length > 0)				//	Record Found");
				sw.WriteLine("{");
				sw.WriteLine("foundRows[0].Delete();");
				sw.WriteLine("if (!dtTemp.HasErrors)");
				sw.WriteLine("dtTemp.AcceptChanges();");
				sw.WriteLine("dsTemp.WriteXml(m_DataFile, XmlWriteMode.WriteSchema);");
				sw.WriteLine("return true;");
				sw.WriteLine("}");
				sw.WriteLine("else");
				sw.WriteLine("{");
				sw.WriteLine("return false;");
				sw.WriteLine("}");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	GetRecord(string strTODO)
				sw.WriteLine("// TODO Edit to reflect appropriate field");
				sw.WriteLine("public bool GetRecord(string strTODO)");
				sw.WriteLine("{");
				sw.WriteLine("if (!DBFileExists(m_DataFile))");
				sw.WriteLine("return false;");
				sw.WriteLine("");
				sw.WriteLine("string strQuery;");
				sw.WriteLine("");
				sw.WriteLine("DataSet dsTemp;");
				sw.WriteLine("DataTable dtTemp;");
				sw.WriteLine("");
				sw.WriteLine("dsTemp = new DataSet();");
				sw.WriteLine("dsTemp.ReadXml(m_DataFile, XmlReadMode.ReadSchema);");
				sw.WriteLine("");
				sw.WriteLine("dtTemp = dsTemp.Tables[\"dataRecord\"];");
				sw.WriteLine("");
				sw.WriteLine("strQuery = \"TODO = '\" + strTODO + \"'\";");
				sw.WriteLine("");
				sw.WriteLine("// Use the Select method to find all rows matching the filter.");
				sw.WriteLine("DataRow[] foundRows = dtTemp.Select(strQuery);");
				sw.WriteLine("");
				sw.WriteLine("if (foundRows.Length > 0)");
				sw.WriteLine("{");
				sw.WriteLine("return GetRecordDetails(foundRows, 0);");
				sw.WriteLine("}");
				sw.WriteLine("return false;");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	GetRecord(int intIndex)
				sw.WriteLine("// TODO Edit to reflect appropriate field");
				sw.WriteLine("public bool GetRecord(int intIndex)");
				sw.WriteLine("{");
				sw.WriteLine("if (!DBFileExists(m_DataFile))");
				sw.WriteLine("return false;");
				sw.WriteLine("");
				sw.WriteLine("string strQuery;");
				sw.WriteLine("");
				sw.WriteLine("DataSet dsTemp;");
				sw.WriteLine("DataTable dtTemp;");
				sw.WriteLine("");
				sw.WriteLine("dsTemp = new DataSet();");
				sw.WriteLine("dsTemp.ReadXml(m_DataFile, XmlReadMode.ReadSchema);");
				sw.WriteLine("");
				sw.WriteLine("dtTemp = dsTemp.Tables[\"dataRecord\"];");
				sw.WriteLine("");
				sw.WriteLine("strQuery = \"" + strKeyfield + " = '\" + intIndex.ToString() + \"'\";");
				sw.WriteLine("");
				sw.WriteLine("// Use the Select method to find all rows matching the filter.");
				sw.WriteLine("DataRow[] foundRows = dtTemp.Select(strQuery);");
				sw.WriteLine("");
				sw.WriteLine("if (foundRows.Length > 0)");
				sw.WriteLine("{");
				sw.WriteLine("return GetRecordDetails(foundRows, 0);");
				sw.WriteLine("}");

				sw.WriteLine("else");
				sw.WriteLine("{");
				sw.WriteLine("this." + "m_" + strKeyfield + " = -1;");
				sw.WriteLine("}");
				sw.WriteLine("return false;");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	GetRecordDetails
				sw.WriteLine("private bool GetRecordDetails(DataRow[] foundRows, int intRow)");
				sw.WriteLine("{");
				sw.WriteLine("bool blnOK = true;");
				sw.WriteLine("try");
				sw.WriteLine("{");

				//	Loop through variables
				foreach (ListViewItem lvItem in lvDatabase.Items)
				{
					strName = lvItem.Text;
					strType = lvItem.SubItems[1].Text;
					strType = TypePrefix(strType);
					strTemp = "this." + "m_" + strName + " = " + strType + "foundRows[intRow][\"" + strName + "\"]);";
					sw.WriteLine(strTemp);
				}

				sw.WriteLine("}");
				sw.WriteLine("catch");
				sw.WriteLine("{");
				sw.WriteLine("blnOK = false;");
				sw.WriteLine("}");
				sw.WriteLine("");
				sw.WriteLine("return blnOK;");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	UpdateRecord()
				sw.WriteLine("public bool UpdateRecord()");
				sw.WriteLine("{");
				sw.WriteLine("bool blnOK = false;");
				sw.WriteLine("string strQuery;");
				sw.WriteLine("DataSet dsTemp;");
				sw.WriteLine("DataTable dtTemp;");
				sw.WriteLine("");
				sw.WriteLine("if (!DBFileExists(m_DataFile))");
				sw.WriteLine("CreateSchema(m_DataFile);");
				sw.WriteLine("");
				sw.WriteLine("try");
				sw.WriteLine("{");
				sw.WriteLine("dsTemp = new DataSet();");
				sw.WriteLine("dsTemp.ReadXml(m_DataFile, XmlReadMode.ReadSchema);");
				sw.WriteLine("dtTemp = dsTemp.Tables[\"dataRecord\"];");
				sw.WriteLine("strQuery = \"" + strKeyfield + " = '\"" + " + this.m_" + strKeyfield + " +\"'\";");
				sw.WriteLine("");
				sw.WriteLine("// Use the Select method to find all rows matching the filter.");
				sw.WriteLine("DataRow[] foundRows = dtTemp.Select(strQuery);");
				sw.WriteLine("");
				sw.WriteLine("if (foundRows.Length > 0)				//	Existing Record");
				sw.WriteLine("{");
				sw.WriteLine("UpdateRecordDetails(ref foundRows[0]);");
				sw.WriteLine("}");
				sw.WriteLine("else");
				sw.WriteLine("{");
				sw.WriteLine("DataRow drNew = dtTemp.NewRow();");
				sw.WriteLine("UpdateRecordDetails(ref drNew);");
				sw.WriteLine("dtTemp.Rows.Add(drNew);");
				sw.WriteLine("}");
				sw.WriteLine("if (!dtTemp.HasErrors)");
				sw.WriteLine("dtTemp.AcceptChanges();");
				sw.WriteLine("dsTemp.WriteXml(m_DataFile, XmlWriteMode.WriteSchema);");
				sw.WriteLine("blnOK = true;");
				sw.WriteLine("}");
				sw.WriteLine("catch");
				sw.WriteLine("{");
				sw.WriteLine("blnOK = false;");
				sw.WriteLine("}");
				sw.WriteLine("return blnOK;");
				sw.WriteLine("}");
				sw.WriteLine("");

				//	UpdateRecordDetails
				sw.WriteLine("private void UpdateRecordDetails(ref DataRow dataRow)");
				sw.WriteLine("{");
				sw.WriteLine("//  Any auto increment fields should be excluded");

				//	Loop through variables - but omit auto increment fields
				foreach (ListViewItem lvItem in lvDatabase.Items)
				{
					if (lvItem.SubItems[2].Text != "Yes")
					{
						strName = lvItem.Text;
						strType = lvItem.SubItems[1].Text;
						strTemp = "dataRow[\"";
						strTemp += strName + "\"] = this.";
						strTemp += "m_" + strName;
						sw.WriteLine(strTemp + ";");
					}
				}
				sw.WriteLine("}");
				sw.WriteLine("");

				//	Public accessors
				foreach (ListViewItem lvItem in lvDatabase.Items)
				{
					strName = lvItem.Text;
					strType = lvItem.SubItems[1].Text;
					strType = strType.Replace("System.", "");
					strTemp = "m_" + strName;
					sw.WriteLine("public " + strType + " " + strName);
					sw.WriteLine("{");
					sw.WriteLine("get { return this." + strTemp + "; }");
					sw.WriteLine("set { this." + strTemp + " = value;}");
					sw.WriteLine("}");
					sw.WriteLine("");
				}


				//	Close class
				sw.WriteLine("}");
				//	Close namespace
				sw.WriteLine("}");

				sw.Close();
			}
			ShowMessage("C# Source File Created", false);
		}

		private void CreateSchema(string strFileName)
		{
			// Create a Schema
			DataColumn DataColumnNew;
			DataSet DataSetNew = new DataSet(txtXMLDataSetName.Text);
			string strName, strType;
			Type dataType;

			DataSetNew.Namespace = txtXMLNamespace.Text;
			DataTable DataTableNew = new DataTable("dataRecord");

			foreach (ListViewItem lvItem in lvDatabase.Items)
			{
				strName = lvItem.Text;
				strType = lvItem.SubItems[1].Text;
				dataType = Type.GetType(strType);
				DataColumnNew = new DataColumn(strName, dataType);
				DataColumnNew.AutoIncrement = (lvItem.SubItems[2].Text == "Yes");

				DataTableNew.Columns.Add(DataColumnNew);
			}

			DataSetNew.Tables.Add(DataTableNew);

			// Write the schema and data to an XML file.
			string xmlFilename = strFileName;

			// Use WriteXml to write the document.
			DataSetNew.WriteXml(xmlFilename, XmlWriteMode.WriteSchema);

			// Dispose of the original DataSet.
			DataSetNew.Dispose();

			ShowMessage("XML File Saved", false);
		}

		private string GetTypeFromLV(string strName)
		{
			string strType = "";

			foreach(ListViewItem lvItem in lvDatabase.Items)
			{
				if(lvItem.Text == strName)
				{
					strType = lvItem.SubItems[1].Text;
					break;
				}
			}
			return strType;
		}

		private bool IsDataComplete(bool blnSchema)
		{
			bool blnOK = true;
			string strMissing = "";

			//	These are needed for the schema
			if (txtWorkingDirectory.Text == "")
			{
				strMissing += "Working Directory, ";
				blnOK = false;
			}
			if(txtXMLNamespace.Text == "")
			{
				strMissing += "XML Namespace, ";
				blnOK = false;
			}
			if (txtXMLDataSetName.Text == "")
			{
				strMissing += "XML Data Set Name, ";
				blnOK = false;
			}
			//	These are onl needed for the class
			if (!blnSchema)
			{
				if (cboKeyField.Text == "")
				{
					strMissing += "Key Field, ";
					blnOK = false;
				}
				if (txtClassNamespace.Text == "")
				{
					strMissing += "Class Namespace, ";
					blnOK = false;
				}
				if (txtClassName.Text == "")
				{
					strMissing += "Class Name, ";
					blnOK = false;
				}
			}
			if (!blnOK)
				ShowMessage("These Fields Must Be Completed: " + strMissing, true);

			return blnOK;
		}

		private void lvDatabase_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lvDatabase.SelectedItems.Count != 1)
				return;

			ListViewItem lvItem = lvDatabase.SelectedItems[0];
			m_intEditItem = lvItem.Index;
			txtFieldName.Text = lvItem.Text;
			cboFieldType.Text = lvItem.SubItems[1].Text;
			chkAutoIncrement.Checked = (lvItem.SubItems[2].Text == "Yes");
			btnAddField.Text = "Update Field";
		}

		[DllImport("User32")]
		public static extern bool MessageBeep(int SoundType);

		private DialogResult OverWriteExisting(string strFileName)
		{
			FileInfo fInfo = new FileInfo(strFileName);
			if(fInfo.Exists)
			{
				return MessageBox.Show(this, "Over write existing file?", "File Exists", MessageBoxButtons.YesNoCancel);
			}
			else
			{
				return DialogResult.Yes;
			}
		}

		private void ReadSchema(string strFileName)
		{
			ListViewItem lvItem;
			lvDatabase.Items.Clear();
			ShowMessage("", false);

			try
			{
				DataSet dsFileNotes = new DataSet();
				dsFileNotes.ReadXmlSchema(strFileName);
				txtXMLNamespace.Text = dsFileNotes.Namespace;
				txtXMLDataSetName.Text = dsFileNotes.DataSetName;
				DataTableCollection dtCollection = dsFileNotes.Tables;
				DataTable dTable = dtCollection[0];
				DataColumnCollection dcCollection = dTable.Columns;

				foreach (DataColumn column in dcCollection)
				{
					lvItem = new ListViewItem(column.ColumnName);
					lvItem.SubItems.Add(column.DataType.ToString());
					if (column.AutoIncrement)
						lvItem.SubItems.Add("Yes");
					else
						lvItem.SubItems.Add("No");
					lvDatabase.Items.Add(lvItem);
				}
			}
			catch
			{
				ShowMessage("Unable To Read Schema", true);
			}
		}

		private void ShowMessage(string strMes, bool blnBeep)
		{
			tslblMessage.Text = strMes;
			if (blnBeep)
				MessageBeep(-1);
		}

		private string TypePrefix(string strType)
		{
			string strPrefix = strType.Replace("System.", "Convert.To");
			return strPrefix + "(";
		}

		private void UpdateKeyFieldCombo()
		{
			bool blnContainsKey = false;
			bool blnContainsSO = false;
			string strCurrentKey = cboKeyField.Text;
			string strCurrentSO = cboStringOverride.Text;

			cboKeyField.Items.Clear();
			cboStringOverride.Items.Clear();

			foreach (ListViewItem lvItem in lvDatabase.Items)
			{
				cboKeyField.Items.Add(lvItem.Text);
				if (lvItem.Text == strCurrentKey)
					blnContainsKey = true;

				if (lvItem.SubItems[1].Text == "System.String")
				{
					cboStringOverride.Items.Add(lvItem.Text);
					if (lvItem.Text == strCurrentSO)
						blnContainsSO = true;
				}
			}
			if (blnContainsKey)
				cboKeyField.Text = strCurrentKey;

			if (blnContainsSO)
				cboStringOverride.Text = strCurrentSO;			
		}

	}
}




















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