Click here to Skip to main content
15,895,011 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.5K   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.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;
using Framework.DACCreator;

namespace DACBuilder
{
	public delegate void GetConnStringEventHandler(object sender, GetConnStringEventArgs e);

	/// <summary>
	/// Summary description for frmNewConnection.
	/// </summary>
	public class frmNewConnection : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtDataSource;
		private System.Windows.Forms.TextBox txtDatabase;
		private System.Windows.Forms.TextBox txtUserID;
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.Label lblDataSource;
		private System.Windows.Forms.Label lblPassword;
		private System.Windows.Forms.Label lblUserName;
		private System.Windows.Forms.Label lblDatabase;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnTestConnection;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button btnBrowse;
		private System.Windows.Forms.Label lblDBType;
		private System.Windows.Forms.ComboBox cboDBTypes;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public event GetConnStringEventHandler GetConnString;

		private string ConnectionString;
		private string DataSource;
		private string Database;
		private string UserID;
		private string Password;
		private DBTypes dbType;


		public frmNewConnection(DBTypes dbType, string ConnectionString)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.dbType = dbType;
			this.ConnectionString = ConnectionString;
		}

		/// <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.txtDataSource = new System.Windows.Forms.TextBox();
			this.lblDataSource = new System.Windows.Forms.Label();
			this.lblPassword = new System.Windows.Forms.Label();
			this.lblUserName = new System.Windows.Forms.Label();
			this.lblDatabase = new System.Windows.Forms.Label();
			this.txtDatabase = new System.Windows.Forms.TextBox();
			this.txtUserID = new System.Windows.Forms.TextBox();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnTestConnection = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnBrowse = new System.Windows.Forms.Button();
			this.lblDBType = new System.Windows.Forms.Label();
			this.cboDBTypes = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// txtDataSource
			// 
			this.txtDataSource.Location = new System.Drawing.Point(104, 40);
			this.txtDataSource.Name = "txtDataSource";
			this.txtDataSource.Size = new System.Drawing.Size(232, 20);
			this.txtDataSource.TabIndex = 0;
			this.txtDataSource.Text = "";
			// 
			// lblDataSource
			// 
			this.lblDataSource.Location = new System.Drawing.Point(16, 40);
			this.lblDataSource.Name = "lblDataSource";
			this.lblDataSource.Size = new System.Drawing.Size(80, 24);
			this.lblDataSource.TabIndex = 1;
			this.lblDataSource.Text = "Data Source:";
			this.lblDataSource.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// lblPassword
			// 
			this.lblPassword.Location = new System.Drawing.Point(32, 112);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new System.Drawing.Size(64, 24);
			this.lblPassword.TabIndex = 2;
			this.lblPassword.Text = "Password:";
			this.lblPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// lblUserName
			// 
			this.lblUserName.Location = new System.Drawing.Point(24, 88);
			this.lblUserName.Name = "lblUserName";
			this.lblUserName.Size = new System.Drawing.Size(72, 24);
			this.lblUserName.TabIndex = 3;
			this.lblUserName.Text = "User Name:";
			this.lblUserName.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// lblDatabase
			// 
			this.lblDatabase.Location = new System.Drawing.Point(24, 64);
			this.lblDatabase.Name = "lblDatabase";
			this.lblDatabase.Size = new System.Drawing.Size(72, 24);
			this.lblDatabase.TabIndex = 4;
			this.lblDatabase.Text = "Database:";
			this.lblDatabase.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// txtDatabase
			// 
			this.txtDatabase.Location = new System.Drawing.Point(104, 64);
			this.txtDatabase.Name = "txtDatabase";
			this.txtDatabase.Size = new System.Drawing.Size(168, 20);
			this.txtDatabase.TabIndex = 5;
			this.txtDatabase.Text = "";
			// 
			// txtUserID
			// 
			this.txtUserID.Location = new System.Drawing.Point(104, 88);
			this.txtUserID.Name = "txtUserID";
			this.txtUserID.Size = new System.Drawing.Size(168, 20);
			this.txtUserID.TabIndex = 6;
			this.txtUserID.Text = "";
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(104, 112);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.Size = new System.Drawing.Size(168, 20);
			this.txtPassword.TabIndex = 7;
			this.txtPassword.Text = "";
			// 
			// btnSave
			// 
			this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnSave.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnSave.Location = new System.Drawing.Point(210, 176);
			this.btnSave.Name = "btnSave";
			this.btnSave.TabIndex = 9;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(290, 176);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 10;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnTestConnection
			// 
			this.btnTestConnection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.btnTestConnection.Location = new System.Drawing.Point(8, 176);
			this.btnTestConnection.Name = "btnTestConnection";
			this.btnTestConnection.Size = new System.Drawing.Size(96, 23);
			this.btnTestConnection.TabIndex = 11;
			this.btnTestConnection.Text = "Test Connection";
			this.btnTestConnection.Click += new System.EventHandler(this.btnTestConnection_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Location = new System.Drawing.Point(13, 160);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(352, 8);
			this.groupBox1.TabIndex = 12;
			this.groupBox1.TabStop = false;
			// 
			// btnBrowse
			// 
			this.btnBrowse.Enabled = false;
			this.btnBrowse.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnBrowse.Location = new System.Drawing.Point(336, 38);
			this.btnBrowse.Name = "btnBrowse";
			this.btnBrowse.Size = new System.Drawing.Size(24, 24);
			this.btnBrowse.TabIndex = 15;
			this.btnBrowse.Text = "...";
			this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
			// 
			// lblDBType
			// 
			this.lblDBType.Location = new System.Drawing.Point(24, 8);
			this.lblDBType.Name = "lblDBType";
			this.lblDBType.Size = new System.Drawing.Size(80, 24);
			this.lblDBType.TabIndex = 16;
			this.lblDBType.Text = "DB Type:";
			this.lblDBType.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// cboDBTypes
			// 
			this.cboDBTypes.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboDBTypes.Location = new System.Drawing.Point(104, 8);
			this.cboDBTypes.Name = "cboDBTypes";
			this.cboDBTypes.Size = new System.Drawing.Size(168, 21);
			this.cboDBTypes.TabIndex = 17;
			this.cboDBTypes.SelectedIndexChanged += new System.EventHandler(this.cboDBTypes_SelectedIndexChanged);
			// 
			// frmNewConnection
			// 
			this.AcceptButton = this.btnSave;
			this.AutoScale = false;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(378, 216);
			this.Controls.Add(this.cboDBTypes);
			this.Controls.Add(this.lblDBType);
			this.Controls.Add(this.btnBrowse);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnTestConnection);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnSave);
			this.Controls.Add(this.txtPassword);
			this.Controls.Add(this.txtUserID);
			this.Controls.Add(this.txtDatabase);
			this.Controls.Add(this.txtDataSource);
			this.Controls.Add(this.lblDatabase);
			this.Controls.Add(this.lblUserName);
			this.Controls.Add(this.lblPassword);
			this.Controls.Add(this.lblDataSource);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmNewConnection";
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "New Connection";
			this.Load += new System.EventHandler(this.frmNewConnection_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void OnGetConnString()
		{
			GetConnStringEventArgs e = new GetConnStringEventArgs(dbType, ConnectionString, txtDataSource.Text, txtDatabase.Text, txtUserID.Text, txtPassword.Text);
			
			if(GetConnString != null)
			{
				GetConnString(this, e);
			}
		}


		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if(TestConnection())
			{
				OnGetConnString();
				//MessageBox.Show("Please restart the application. The changes will occur next time you open the application.");
				Close();
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private bool TestConnection()
		{
			bool bTest = false;
			switch(dbType)
			{
				case DBTypes.SQL:
					SqlConnection sql_cnn = new SqlConnection();
					ConnectionString = "Data Source=" + this.txtDataSource.Text + ";Database=" + this.txtDatabase.Text + ";User ID=" + this.txtUserID.Text + ";Password=" + this.txtPassword.Text;
					sql_cnn.ConnectionString = ConnectionString;
					try
					{
						sql_cnn.Open();
						sql_cnn.Close();
						bTest = true;
					}
					catch(Exception ex)
					{
						MessageBox.Show("Connection failed!\n" + ex.Message.ToString());
						bTest = false;
					}
					break;
				case DBTypes.ORACLE:
					break;
				case DBTypes.MYSQL:
					OdbcConnection mysql_cnn = new OdbcConnection();
					ConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=" + this.txtDataSource.Text + ";Database=" + this.txtDatabase.Text + ";uid=" + this.txtUserID.Text + ";pwd=" + this.txtPassword.Text + ";option=3";
					mysql_cnn.ConnectionString = ConnectionString;
					try
					{
						mysql_cnn.Open();
						mysql_cnn.Close();
						bTest = true;
					}
					catch(Exception ex)
					{
						MessageBox.Show("Connection failed!\n" + ex.Message.ToString());
						bTest = false;
					}
					break;
				case DBTypes.MSACCESS:
					OleDbConnection msaccess_cnn = new OleDbConnection();
					ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=" + this.txtDataSource.Text;
					msaccess_cnn.ConnectionString = ConnectionString;
					try
					{
						msaccess_cnn.Open();
						msaccess_cnn.Close();
						bTest = true;
					}
					catch(Exception ex)
					{
						MessageBox.Show("Connection failed!\n" + ex.Message.ToString());
						bTest = false;
					}
					break;
			}
			return bTest;
		}

		private void btnTestConnection_Click(object sender, System.EventArgs e)
		{
			if(TestConnection())
			{
				MessageBox.Show("Connection tested successfully!");
			}
		}

		private void frmNewConnection_Load(object sender, System.EventArgs e)
		{
			Parse();

			foreach(string s in Enum.GetNames(typeof(DBTypes)))
			{
				cboDBTypes.Items.Add(s);
			}
			cboDBTypes.SelectedIndex = (int)dbType;
			switch(dbType)
			{
				case DBTypes.SQL:
				case DBTypes.MYSQL:
				case DBTypes.ORACLE:
					txtDataSource.Text = DataSource;
					txtDatabase.Text = Database;
					txtUserID.Text = UserID;
					txtPassword.Text = Password;
					btnBrowse.Enabled = false;
					break;
				case DBTypes.MSACCESS:
					txtDataSource.Text = DataSource;
					btnBrowse.Enabled = true;
					txtDatabase.Enabled = txtUserID.Enabled = txtPassword.Enabled = false;
					break;
			}
		}

		private void Parse()
		{
			string[] pairs = ConnectionString.Split(';');
			string name, value;
			string[] vals;
			foreach(string pair in pairs)
			{
				vals = pair.Trim().Split('=');
				if(vals!= null && vals.Length == 2)
				{
					name = vals[0].Trim();
					value = vals[1].Trim();
					switch(name.ToLower())
					{
						case "data source":
						case "server":
							DataSource = value;
							break;
						case "database":
							Database = value;
							break;
						case "user id":
						case "uid":
							UserID = value;
							break;
						case "password":
						case "pwd":
							Password = value;
							break;
						default:
							break;
					}
				}
			}
		}

		private void btnBrowse_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.FileName = DataSource;
			ofd.Filter = "MS Access databases (*.mdb)|*.mdb";
			if(ofd.ShowDialog() == DialogResult.OK)
			{
				txtDataSource.Text = ofd.FileName;
				DataSource = ofd.FileName;
			}
		}

		private void cboDBTypes_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			dbType = (DBTypes)cboDBTypes.SelectedIndex;
			switch(dbType)
			{
				case DBTypes.ORACLE:
					txtDataSource.Enabled = btnBrowse.Enabled = txtDatabase.Enabled = txtUserID.Enabled = txtPassword.Enabled 
						= btnSave.Enabled = btnCancel.Enabled = btnTestConnection.Enabled = false;
					break;
				case DBTypes.SQL:
				case DBTypes.MYSQL:
					btnBrowse.Enabled = false;
					txtDataSource.Enabled = txtDatabase.Enabled = txtUserID.Enabled = txtPassword.Enabled
						= btnSave.Enabled = btnCancel.Enabled = btnTestConnection.Enabled = true;
					break;
				case DBTypes.MSACCESS:
					txtDataSource.Enabled = btnBrowse.Enabled = btnSave.Enabled = btnCancel.Enabled = btnTestConnection.Enabled = true;
					txtDatabase.Enabled = txtUserID.Enabled = txtPassword.Enabled = false;
					break;
				default: 
					break;
			}
		}
	}

	public class GetConnStringEventArgs : EventArgs
	{
		private DBTypes xDBType;
		private string xConnectionString;
		private string xDataSource;
		private string xDatabase;
		private string xUserID;
		private string xPassword;

		public GetConnStringEventArgs(DBTypes xDBType, string xConnectionString, string xDataSource, string xDatabase, string xUserID, string xPassword)
		{
			this.xDBType = xDBType;
			this.xConnectionString = xConnectionString;
			this.xDataSource = xDataSource;
			this.xDatabase = xDatabase;
			this.xUserID = xUserID;
			this.xPassword = xPassword;
		}
		public DBTypes DBType
		{
			get
			{
				return xDBType;
			}
		}
		public string ConnectionString
		{
			get
			{
				return xConnectionString;
			}
		}
		public string DataSource
		{
			get
			{
				return xDataSource;
			}
		}
		public string Database
		{
			get
			{
				return xDatabase;
			}
		}
		public string UserID
		{
			get
			{
				return xUserID;
			}
		}
		public string Password
		{
			get
			{
				return xPassword;
			}
		}

	}


}

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