Click here to Skip to main content
15,895,370 members
Articles / Web Development / ASP.NET

JohnKenedy Data Access Layer Library

Rate me:
Please Sign up or sign in to vote.
3.42/5 (12 votes)
10 Jul 2008GPL37 min read 52K   1.1K   48  
This is a .NET 3.5 library that acts as Data Access Layer with many automatic features
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;

namespace JohnKenedy.DataAccessModule
{
    public partial class Module : Form
    {
        ModuleManager _Manager = new ModuleManager();

        public Module()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtOutputFolder.Text == "")
            {
                MessageBox.Show("Output folder must be set first");
                return;
            }
            if (txtNamespace.Text == "")
            {
                MessageBox.Show("Namespace must be set first");
                return;
            }

            _Manager.Namespace = txtNamespace.Text;
            _Manager.OutputFolder = txtOutputFolder.Text;

            string _filename = "";
            if (txtFileName.Text == "")
            {
                saveFileDialog1.Filter = "DataAccess Module Files(*.dam) | *.dam";
                saveFileDialog1.ShowDialog();
                _filename = saveFileDialog1.FileName;
            }
            else
            {
                _filename = txtFileName.Text;
            }
            if (Path.GetExtension(_filename) == "") _filename = _filename + ".dam";
            
            Stream _stream = null;
            if (File.Exists(_filename)) File.Delete(_filename);
            _stream = new FileStream(_filename, FileMode.CreateNew);

            BinaryFormatter _formatter = new BinaryFormatter();
            _formatter.Serialize(_stream, _Manager);
            _stream.Close();

            txtFileName.Text = _filename;
            MessageBox.Show("Project file is saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            frmCreateNewModule _frm = new frmCreateNewModule();
            _frm.Manager = _Manager;
            _frm.ShowDialog();
            LoadGrid();
        }

        private void Module_Load(object sender, EventArgs e)
        {
            RefreshAll();
        }

        public void RefreshAll()
        {
            ClearAll();
            LoadGrid();
            LoadData();
        }

        public void ClearAll()
        {
            txtFileName.Text = "";
            txtOutputFolder.Text = "";
            txtNamespace.Text = "";
            dataGridView1.DataSource = null;
        }

        public void LoadGrid()
        {
            dataGridView1.DataSource = _Manager.GetGridData();
        }

        public void LoadData()
        {
            txtOutputFolder.Text = _Manager.OutputFolder;
            txtNamespace.Text = _Manager.Namespace;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "DataAccess Module Files(*.dam) | *.dam";
            openFileDialog1.ShowDialog();
            string _filename = openFileDialog1.FileName;
            if (File.Exists(_filename))
            {
                txtFileName.Text = _filename;
                FileStream _fs = new FileStream(_filename, FileMode.Open);
                BinaryFormatter _formatter = new BinaryFormatter();
                object _object = _formatter.Deserialize(_fs);
                _Manager = (ModuleManager)_object;
            }
            RefreshAll();
            txtFileName.Text = _filename;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (txtOutputFolder.Text == "")
            {
                MessageBox.Show("Output folder must be set first");
                return;
            }
            if (txtNamespace.Text == "")
            {
                MessageBox.Show("Namespace must be set first");
                return;
            }

            _Manager.Namespace = txtNamespace.Text;
            _Manager.OutputFolder = txtOutputFolder.Text;

            IDictionary<string, string> _files = _Manager.GenerateFiles(txtNamespace.Text);
            foreach (KeyValuePair<string, string> _file in _files)
            {
                string _filename = Path.Combine(txtOutputFolder.Text, _file.Key + ".cs");
                if (File.Exists(_filename)) RenameFile(_filename);
                FileStream _fs = new FileStream(_filename, FileMode.CreateNew);
                StreamWriter _sw = new StreamWriter(_fs);
                _sw.Write(_file.Value);
                _sw.Flush();
                _sw.Close();
                _fs.Close();
            }
            MessageBox.Show("File(s) generated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void RenameFile(string _filename)
        {
            for (int i = 0; i <= 1000; i++)
            {
                string _newname = _filename + ".bak" + i.ToString();
                if (File.Exists(_newname) == true) continue;
                File.Copy(_filename, _newname);
                File.Delete(_filename);
                break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath == "" || Directory.Exists(folderBrowserDialog1.SelectedPath) == false) return;
            txtOutputFolder.Text = folderBrowserDialog1.SelectedPath;
            _Manager.OutputFolder = txtOutputFolder.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            _Manager = new ModuleManager();
            RefreshAll();
        }

        private void btnProperties_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0) return;
            object _index = dataGridView1.SelectedRows[0].Cells[0].Value;
            
            ModuleExtendDefinition _module = SearchModuleByIndex((int)_index);
            if (_module == null) return;

            frmProperties _properties = new frmProperties();
            _properties.Module = _module;
            _properties.ShowDialog();
            LoadGrid();
        }

        private ModuleExtendDefinition SearchModuleByIndex(int _index)
        {
            if (_Manager == null || _Manager.ModuleList == null || _Manager.ModuleList.Count <= 0) return null;
            foreach (ModuleExtendDefinition _module in _Manager.ModuleList)
            {
                if (_module.Index == _index) return _module;
            }
            return null;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0) return;
            object _index = dataGridView1.SelectedRows[0].Cells[0].Value;

            ModuleExtendDefinition _module = SearchModuleByIndex((int)_index);
            if (_module == null) return;

            if (_Manager.ModuleList.Contains(_module) == false) return;
            _Manager.ModuleList.Remove(_module);
            LoadGrid();
        }
    }
}

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 GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Singapore Singapore
I write code mostly in C#, VB.NET, PHP and Assembly.

Comments and Discussions