Click here to Skip to main content
15,896,421 members
Articles / Programming Languages / C#

C# Relational Database

Rate me:
Please Sign up or sign in to vote.
2.62/5 (59 votes)
8 Feb 20055 min read 85.2K   1.7K   28  
This article describes how to write a relational database using C#.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.Reflection.Emit;

namespace SplitString
{
	// Declare delegate with global scope
	public delegate void MyDelegate();
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmSplitString : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.TextBox txtFName;
		private System.Windows.Forms.TextBox txtLName;
		private System.Windows.Forms.TextBox txtDOB;
		private System.Windows.Forms.TextBox txtSocSec;
		private System.Windows.Forms.ListBox lstSplitString;
		
		// Instantiate new string arrays
		private string[] FName = new string[10];
		private string[] LName = new string[10];
		private string[] DOB = new string[10];
		private string[] SocSec = new string[10];

		// Declare new variables
		private int intIndex;
        private string strSplit1 = "", strSplit2 = "", strSplit3 = "",
			strSplit4 = "";
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;

		// Instantiate instance of windows form
		private frmRelationalDatabase relDat;
        /// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.MenuItem miYellow;
		private System.Windows.Forms.MenuItem miAqua;
		private string myFileName = "C:/Program Files/Davco/MyRelationalDatabase/X_File.ocx";
		
		public frmSplitString()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.miYellow = new System.Windows.Forms.MenuItem();
			this.miAqua = new System.Windows.Forms.MenuItem();
			this.txtFName = new System.Windows.Forms.TextBox();
			this.txtLName = new System.Windows.Forms.TextBox();
			this.txtDOB = new System.Windows.Forms.TextBox();
			this.txtSocSec = new System.Windows.Forms.TextBox();
			this.lstSplitString = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.miYellow,
																					  this.miAqua});
			this.menuItem1.Text = "File";
			// 
			// miYellow
			// 
			this.miYellow.Index = 0;
			this.miYellow.Text = "&BackColor( Yellow )";
			this.miYellow.Click += new System.EventHandler(this.miYellow_Click);
			// 
			// miAqua
			// 
			this.miAqua.Index = 1;
			this.miAqua.Text = "BackColor( Aqua )";
			this.miAqua.Click += new System.EventHandler(this.miAqua_Click);
			// 
			// txtFName
			// 
			this.txtFName.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtFName.Location = new System.Drawing.Point(8, 16);
			this.txtFName.MaxLength = 10;
			this.txtFName.Name = "txtFName";
			this.txtFName.Size = new System.Drawing.Size(112, 26);
			this.txtFName.TabIndex = 0;
			this.txtFName.Text = "";
			this.txtFName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtFName_KeyDown);
			// 
			// txtLName
			// 
			this.txtLName.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtLName.Location = new System.Drawing.Point(128, 16);
			this.txtLName.MaxLength = 12;
			this.txtLName.Name = "txtLName";
			this.txtLName.Size = new System.Drawing.Size(112, 26);
			this.txtLName.TabIndex = 1;
			this.txtLName.Text = "";
			this.txtLName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtLName_KeyDown);
			// 
			// txtDOB
			// 
			this.txtDOB.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtDOB.Location = new System.Drawing.Point(248, 16);
			this.txtDOB.MaxLength = 12;
			this.txtDOB.Name = "txtDOB";
			this.txtDOB.Size = new System.Drawing.Size(112, 26);
			this.txtDOB.TabIndex = 2;
			this.txtDOB.Text = "";
			this.txtDOB.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtDOB_KeyDown);
			// 
			// txtSocSec
			// 
			this.txtSocSec.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtSocSec.Location = new System.Drawing.Point(368, 16);
			this.txtSocSec.MaxLength = 12;
			this.txtSocSec.Name = "txtSocSec";
			this.txtSocSec.Size = new System.Drawing.Size(112, 26);
			this.txtSocSec.TabIndex = 3;
			this.txtSocSec.Text = "";
			this.txtSocSec.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSocSec_KeyDown);
			// 
			// lstSplitString
			// 
			this.lstSplitString.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lstSplitString.ItemHeight = 18;
			this.lstSplitString.Location = new System.Drawing.Point(8, 48);
			this.lstSplitString.Name = "lstSplitString";
			this.lstSplitString.Size = new System.Drawing.Size(472, 202);
			this.lstSplitString.TabIndex = 4;
			this.lstSplitString.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lstSplitString_KeyDown);
			this.lstSplitString.DoubleClick += new System.EventHandler(this.lstSplitString_DoubleClick);
			this.lstSplitString.KeyUp += new System.Windows.Forms.KeyEventHandler(this.lstSplitString_KeyUp);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(0, 264);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(480, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "1. Double click item to split string to textBoxes for edit.";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(24, 288);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(408, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "Make necessary change to field, and then press F1.";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(0, 312);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(448, 16);
			this.label3.TabIndex = 7;
			this.label3.Text = "2. To bring up Relational Database Record, single click press \"enter\"";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(24, 336);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(216, 16);
			this.label4.TabIndex = 8;
			this.label4.Text = "record, and press \"Enter\"";
			// 
			// frmSplitString
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(10, 19);
			this.BackColor = System.Drawing.Color.Aqua;
			this.ClientSize = new System.Drawing.Size(488, 361);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1,
																		  this.lstSplitString,
																		  this.txtSocSec,
																		  this.txtDOB,
																		  this.txtLName,
																		  this.txtFName});
			this.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Menu = this.mainMenu1;
			this.Name = "frmSplitString";
			this.Text = "SplitString";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.frmSplitString_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ParentChanged += new System.EventHandler(this.frmSplitString_ParentChanged);
			this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new frmSplitString());
		}
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		

		private void Form1_Load(object sender, System.EventArgs e)
		{
			// instantiate dialog box for creating records
			relDat = new frmRelationalDatabase( new MyDelegate(ShowSplash));
			
			// Declare Arrays
			string[] FName = {"Dave", "Lilly", "Bob", 
                              "Sandra", "Dick", "Ted",
							  "Kathy", "Angela", "Nick", 
                              "Juan"};
			string[] LName = {"Brighton", "Tomlin", "TheFather", 
                              "Murphy",  "Holiday", "Heathrow",
                              "Auburn", "Thomas", "TheGreek", 
                               "Valdez"};
			string[] DOB = {"05/23/1956", "02/03/1957", "01/01/1967", 
                            "02/02/1967", "08/09/1967", "04/05/1976", 
                            "06/06/1956", "07/02/1945", "05/05/1923", 
                            "07/01/1948"};
			string[] SocSec = {"123-23-3456", "234-23-2345", "345-23-1234", 
                               "456-23-2345", "567-34-5678", "567-23-4567", 
                               "234-34-2345", "456-12-7897", "456-23-6543", 
                               "654-43-6543"};
			
			// Format String
			string strFormatString = "";
			strFormatString = "{0, -10}{1, -12}{2, -12}{3, -12}";

			// Loop through arrays and load formatted string into listbox
			for( int i = 0; i < FName.Length; i++ )
			{
				// Instantiate new Stringbuilder class
			    // When we instantiate instance of new class here the class is 
				// recreated and therefore the memory is erased. This is easier 
				// than using StringBuilder.Remove
			    StringBuilder sb = new StringBuilder();
				sb.AppendFormat(strFormatString, FName[i], LName[i], 
					DOB[i], SocSec[i]);
				lstSplitString.Items.Add( sb.ToString() );
			}
		}

		private void lstSplitString_DoubleClick(object sender, 
			System.EventArgs e)
		{
			string strSplit = lstSplitString.SelectedItem.ToString();
			intIndex = lstSplitString.SelectedIndex;
			
			strSplit1 = strSplit.Substring(0, 9);
			strSplit2 = strSplit.Substring(10, 11);
			strSplit3 = strSplit.Substring(22, 11);
			strSplit4 = strSplit.Substring(34, 12);

		    txtFName.Text = strSplit1.Trim();
			txtLName.Text = strSplit2.Trim();
			txtDOB.Text = strSplit3.Trim();
			txtSocSec.Text = strSplit4.Trim();
        }

		private void txtFName_KeyDown(object sender, 
			System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyCode == Keys.F1)
			{
				ChangeText( strSplit1, strSplit2, strSplit3, strSplit4 );
			}
		}

		private void txtLName_KeyDown(object sender, 
			System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyCode == Keys.F1 )
			{
				ChangeText( strSplit1, strSplit2, strSplit3, strSplit4 );
			}
		}

		private void txtDOB_KeyDown(object sender, 
			System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyCode == Keys.F1 )
			{
				ChangeText( strSplit1, strSplit2, strSplit3, strSplit4 );
			}
		}

		private void txtSocSec_KeyDown(object sender, 
			System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyCode == Keys.F1 )
			{
				ChangeText( strSplit1, strSplit2, strSplit3, strSplit4 );
			}
		}

		public void ChangeText( string strSplit1, string strSplit2, string strSplit3, 
			                    string strSplit4 )
		{
			// Instantiate new StringBuilder. We'll do this locally
			// so the Stringbuilder is cleared each time.
			StringBuilder sd = new StringBuilder();
			
			// Format String
			string strFormat = "";
			strFormat = "{0, -10}{1, -12}{2, -12}{3, -12}";

			// Pass values from textBoxes to string variables
			strSplit1 = txtFName.Text;
			strSplit2 = txtLName.Text;
			strSplit3 = txtDOB.Text;
			strSplit4 = txtSocSec.Text;

			// Rebuild StringBuilder Class, and replace old listBox item 
			// with new information
			sd.AppendFormat(strFormat, strSplit1.Trim(), strSplit2.Trim(), 
				strSplit3.Trim(), strSplit4.Trim());
		    lstSplitString.Items.RemoveAt(intIndex);
			lstSplitString.Items.Insert(intIndex, sd.ToString());
			
			// Clear textBoxes
			txtFName.Clear();
			txtLName.Clear();
			txtDOB.Clear();
			txtSocSec.Clear();
			
			// Deselects listbox item
			lstSplitString.Refresh();
		}

        private void lstSplitString_KeyDown(object sender, 
			System.Windows.Forms.KeyEventArgs e)
		{
			if( e.KeyCode == Keys.Return )
			{
				if( File.Exists( myFileName ) )
				{
					// don't do nothing
				}
				else
				{
                    // Create File
					try
					{
						// Create File
						File.CreateText( myFileName );
					    MessageBox.Show("New support file has been\r\n" +
							"created, so restart program.",
							"Important", MessageBoxButtons.OK,
							MessageBoxIcon.Exclamation);
						Application.Exit();
					}
					catch( IOException )
					{
						Application.Exit();
					}
				}
			}
		}
        
		private void lstSplitString_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			
			try
			{
				// Pass listBox Selected Index to variable
				intIndex = lstSplitString.SelectedIndex;
				
				// Instantiate StreamWriter
				StreamWriter streamWriter = new StreamWriter( myFileName );

				// Writes to external file
				streamWriter.Write( intIndex.ToString() );
			
				// Closes StreamWriter
				streamWriter.Close();
			}
			catch( Exception g )
			{
				MessageBox.Show( g.ToString(), "Important",
					MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
			finally
			{
				// Show Relational database Form
				relDat.Show();
			}
		}

		protected void ShowSplash()
		{
			Show();
		}

		// In the event the operator clicks on "Close Form"
		// in Task Bar.
		private void frmSplitString_Closing(object sender, 
			System.ComponentModel.CancelEventArgs e)
		{
			Application.Exit();
		}

		private void frmSplitString_ParentChanged(object sender, System.EventArgs e)
		{
			lstSplitString.Refresh();
		}

		private void miYellow_Click(object sender, System.EventArgs e)
		{
			this.BackColor = Color.Yellow;
		}

		private void miAqua_Click(object sender, System.EventArgs e)
		{
			this.BackColor = Color.Aqua;
		}

		
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I studied Fortran IV in HighSchool where we had 2 keypunch machines, and access to an IBM 1100 at the Community College. We ran our programs batch, and compiled our programs on paper tape.

Years later when PC's became affordable, I gave programming another shot. This time I studied RPG with the IBM AS-400 computer. I could access the College Computer with Emulator Software( Mocha Soft MW 5250 ) and my home PC.

C++ came later, then VB-6, C#.Net, and Managed C++. I am currently studying VB.Net

Comments and Discussions