Click here to Skip to main content
15,891,248 members
Articles / Database Development / SQL Server

DACBuilder – Data Access objects generation tool based on XML and XSL templates transformation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
31 Mar 2006CPOL23 min read 76.3K   1.9K   68  
The DACBuilder application provides auto-generation features from multiple database systems in multiple programming languages.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
using System.Xml;
using System.IO;

namespace DACBuilder
{
	/// <summary>
	/// Summary description for frmResources.
	/// </summary>
	public class frmResources : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel pnlGrid;
		private System.Windows.Forms.Panel pnlText;
		private System.Windows.Forms.DataGrid dgResources;
		private System.Windows.Forms.RichTextBox rtfResources;

		private string templatesPath;
		private XmlDataDocument doc;
		private bool bIsDirty = false;
		private string CurrentFile = string.Empty;

		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem mnuResources;
		private System.Windows.Forms.MenuItem mnuResourcesXML;
		private System.Windows.Forms.MenuItem mnuResourcesData;
		private System.Windows.Forms.MenuItem mnuResourcesBar1;
		private System.Windows.Forms.MenuItem mnuResourcesSave;
		private System.Windows.Forms.MenuItem mnuResourcesSaveAs;
		private System.Windows.Forms.MenuItem mnuResourcesOpen;
		private System.Windows.Forms.MenuItem mnuResourcesNew;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmResources()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			templatesPath = Application.StartupPath + @"\Templates\";
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.pnlGrid = new System.Windows.Forms.Panel();
			this.dgResources = new System.Windows.Forms.DataGrid();
			this.pnlText = new System.Windows.Forms.Panel();
			this.rtfResources = new System.Windows.Forms.RichTextBox();
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.mnuResources = new System.Windows.Forms.MenuItem();
			this.mnuResourcesData = new System.Windows.Forms.MenuItem();
			this.mnuResourcesXML = new System.Windows.Forms.MenuItem();
			this.mnuResourcesBar1 = new System.Windows.Forms.MenuItem();
			this.mnuResourcesSave = new System.Windows.Forms.MenuItem();
			this.mnuResourcesSaveAs = new System.Windows.Forms.MenuItem();
			this.mnuResourcesOpen = new System.Windows.Forms.MenuItem();
			this.mnuResourcesNew = new System.Windows.Forms.MenuItem();
			this.pnlGrid.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dgResources)).BeginInit();
			this.pnlText.SuspendLayout();
			this.SuspendLayout();
			// 
			// pnlGrid
			// 
			this.pnlGrid.Controls.Add(this.dgResources);
			this.pnlGrid.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlGrid.Location = new System.Drawing.Point(0, 0);
			this.pnlGrid.Name = "pnlGrid";
			this.pnlGrid.Size = new System.Drawing.Size(824, 266);
			this.pnlGrid.TabIndex = 0;
			// 
			// dgResources
			// 
			this.dgResources.DataMember = "";
			this.dgResources.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dgResources.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dgResources.Location = new System.Drawing.Point(0, 0);
			this.dgResources.Name = "dgResources";
			this.dgResources.Size = new System.Drawing.Size(824, 266);
			this.dgResources.TabIndex = 0;
			// 
			// pnlText
			// 
			this.pnlText.Controls.Add(this.rtfResources);
			this.pnlText.Dock = System.Windows.Forms.DockStyle.Fill;
			this.pnlText.Location = new System.Drawing.Point(0, 0);
			this.pnlText.Name = "pnlText";
			this.pnlText.Size = new System.Drawing.Size(824, 266);
			this.pnlText.TabIndex = 1;
			this.pnlText.Visible = false;
			// 
			// rtfResources
			// 
			this.rtfResources.Dock = System.Windows.Forms.DockStyle.Fill;
			this.rtfResources.Location = new System.Drawing.Point(0, 0);
			this.rtfResources.Name = "rtfResources";
			this.rtfResources.Size = new System.Drawing.Size(824, 266);
			this.rtfResources.TabIndex = 0;
			this.rtfResources.Text = "";
			this.rtfResources.TextChanged += new System.EventHandler(this.rtfResources_TextChanged);
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnuResources});
			// 
			// mnuResources
			// 
			this.mnuResources.Index = 0;
			this.mnuResources.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.mnuResourcesNew,
																						 this.mnuResourcesSave,
																						 this.mnuResourcesSaveAs,
																						 this.mnuResourcesOpen,
																						 this.mnuResourcesBar1,
																						 this.mnuResourcesData,
																						 this.mnuResourcesXML});
			this.mnuResources.Text = "Resources";
			// 
			// mnuResourcesData
			// 
			this.mnuResourcesData.Index = 5;
			this.mnuResourcesData.Text = "Data";
			this.mnuResourcesData.Click += new System.EventHandler(this.mnuResourcesData_Click);
			// 
			// mnuResourcesXML
			// 
			this.mnuResourcesXML.Index = 6;
			this.mnuResourcesXML.Text = "XML";
			this.mnuResourcesXML.Click += new System.EventHandler(this.mnuResourcesXML_Click);
			// 
			// mnuResourcesBar1
			// 
			this.mnuResourcesBar1.Index = 4;
			this.mnuResourcesBar1.Text = "-";
			// 
			// mnuResourcesSave
			// 
			this.mnuResourcesSave.Index = 1;
			this.mnuResourcesSave.Text = "Save";
			this.mnuResourcesSave.Click += new System.EventHandler(this.mnuResourcesSave_Click);
			// 
			// mnuResourcesSaveAs
			// 
			this.mnuResourcesSaveAs.Index = 2;
			this.mnuResourcesSaveAs.Text = "Save as...";
			this.mnuResourcesSaveAs.Click += new System.EventHandler(this.mnuResourcesSaveAs_Click);
			// 
			// mnuResourcesOpen
			// 
			this.mnuResourcesOpen.Index = 3;
			this.mnuResourcesOpen.Text = "Open...";
			this.mnuResourcesOpen.Click += new System.EventHandler(this.mnuResourcesOpen_Click);
			// 
			// mnuResourcesNew
			// 
			this.mnuResourcesNew.Index = 0;
			this.mnuResourcesNew.Text = "New";
			this.mnuResourcesNew.Click += new System.EventHandler(this.mnuResourcesNew_Click);
			// 
			// frmResources
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(824, 266);
			this.Controls.Add(this.pnlText);
			this.Controls.Add(this.pnlGrid);
			this.Menu = this.mainMenu;
			this.Name = "frmResources";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Resources";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmResources_Load);
			this.pnlGrid.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dgResources)).EndInit();
			this.pnlText.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmResources_Load(object sender, System.EventArgs e)
		{
			//pnlText.Size = new Size(0, pnlText.Size.Height);
			pnlText.Visible = false;
			GenerateResource(templatesPath + "template.resx");
			GetXml();
		}

		#region Functions

		private void GenerateResource(string fileName)
		{
			try
			{
				doc = new XmlDataDocument();
				doc.DataSet.ReadXmlSchema(templatesPath + "template.resx");
				doc.Load(fileName);
			
				doc.DataSet.Tables[0].RowChanged += new DataRowChangeEventHandler(frmResources_RowChanged);
				doc.DataSet.Tables[0].RowDeleted += new DataRowChangeEventHandler(frmResources_RowDeleted);
				dgResources.DataSource = doc.DataSet.Tables[0].DefaultView;
			}
			catch(Exception ex)
			{
				while(ex != null)
				{
					MessageBox.Show(ex.Message);
					ex = ex.InnerException;
				}
			}
		}

		private void GetXml()
		{
			rtfResources.Text = doc.OuterXml;
			//XmlTextWriter writer = new XmlTextWriter();
			//doc.Save(writer);
		}

		private void SetXml()
		{
			try
			{
				doc = new XmlDataDocument();
				doc.DataSet.ReadXmlSchema(templatesPath + "template.resx");
				doc.LoadXml(rtfResources.Text);
				doc.DataSet.Tables[0].RowChanged += new DataRowChangeEventHandler(frmResources_RowChanged);
				doc.DataSet.Tables[0].RowDeleted += new DataRowChangeEventHandler(frmResources_RowDeleted);
				dgResources.DataSource = doc.DataSet.Tables[0].DefaultView;
				bIsDirty = false;
			}
			catch(Exception ex)
			{
				while(ex != null)
				{
					MessageBox.Show(ex.Message);
					ex = ex.InnerException;
				}
			}
		}

		private void SaveFile()
		{
			SaveFileDialog saveDlg = new SaveFileDialog();
			saveDlg.Title = "Save ResX file";
			saveDlg.Filter = "ResX files (*.resx)|*.resx";
			if(saveDlg.ShowDialog() == DialogResult.OK)
			{
				CurrentFile = saveDlg.FileName;
				if(CurrentFile != "")
				{
					try
					{
						doc.Save(CurrentFile);
					}
					catch(Exception ex)
					{
						while(ex != null)
						{
							MessageBox.Show(ex.Message);
							ex = ex.InnerException;
						}
					}
				}
			}
		}

		private void OpenFile()
		{
			OpenFileDialog openDlg = new OpenFileDialog();
			openDlg.Title = "Open ResX file";
			openDlg.Filter = "ResX files (*.resx)|*.resx";
			if(openDlg.ShowDialog() == DialogResult.OK)
			{
				if(openDlg.FileName != "")
				{
					GenerateResource(openDlg.FileName);
					CurrentFile = openDlg.FileName;
				}
			}
		}

		#endregion

		private void frmResources_RowChanged(object sender, DataRowChangeEventArgs e)
		{
			GetXml();
		}

		private void frmResources_RowDeleted(object sender, DataRowChangeEventArgs e)
		{
			GetXml();
		}

		private void rtfResources_TextChanged(object sender, System.EventArgs e)
		{
			bIsDirty = true;

		}

		private void mnuResourcesData_Click(object sender, System.EventArgs e)
		{
			pnlText.Visible = false;
			pnlGrid.Visible = true;
			if(bIsDirty)
			{
				SetXml();
			}
		}

		private void mnuResourcesXML_Click(object sender, System.EventArgs e)
		{
			pnlText.Visible = true;
			pnlGrid.Visible = false;
		}

		private void mnuResourcesSave_Click(object sender, System.EventArgs e)
		{
			if(CurrentFile != "")
			{
				try
				{
					doc.Save(CurrentFile);
				}
				catch(Exception ex)
				{
					while(ex != null)
					{
						MessageBox.Show(ex.Message);
						ex = ex.InnerException;
					}
				}
			}
			else
			{
				mnuResourcesSaveAs_Click(sender, e);
			}
		}

		private void mnuResourcesSaveAs_Click(object sender, System.EventArgs e)
		{
			SaveFile();
		}

		private void mnuResourcesOpen_Click(object sender, System.EventArgs e)
		{
			OpenFile();
		}

		private void mnuResourcesNew_Click(object sender, System.EventArgs e)
		{
			GenerateResource(templatesPath + "template.resx");
			CurrentFile = string.Empty;
		}
	}
}

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
Web Developer Telstra Internet
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions