using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Data.SqlClient;
using System.Reflection;
using System.Diagnostics;
namespace EvoSoft_Constructor
{
public partial class frmConstructer : Form
{
///
/// Constructor
///
public frmConstructer()
{
InitializeComponent();
BindTableCombo();
}
#region methods
///
/// Method to bind the tables combo
///
private void BindTableCombo()
{
try
{
//fecth table list
cboTables.DisplayMember = "tableName";
cboTables.DataSource = Constructor.GetTables().Tables[0];
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Method to produce the object output
///
private bool CreateObjectOutput(bool custom)
{
ArrayList fieldList = new ArrayList();
string pathName = Application.StartupPath + "\\output\\";
string objectName = txtObjectName.Text + ".cs";
FileInfo file = new FileInfo(pathName + objectName);
bool result = false;
try
{
utils.validateFolder(pathName);
StreamWriter writer = new StreamWriter(pathName + objectName, false);
fieldList = Constructor.GetTableFields(tableName);
//build result string
writer.WriteLine("using System;");
writer.WriteLine("using System.Collections.Generic;");
writer.WriteLine("using System.Linq;");
writer.WriteLine("using System.Text;");
writer.WriteLine();
writer.WriteLine("[Serializable]");
writer.WriteLine("public class " + txtObjectName.Text);
writer.WriteLine("{");
foreach (oTable table in fieldList)
{
string dataType = String.Empty;
string dataResult = String.Empty;
switch (table.dataType.ToLower())
{
case "int":
dataType = "int";
dataResult = "0";
break;
case "bit":
dataType = "bool";
dataResult = "false";
break;
case "money":
dataType = "decimal";
dataResult = "0";
break;
case "decimal":
dataType = "decimal";
dataResult = "0";
break;
case "real":
dataType = "double";
dataResult = "0";
break;
case "float":
dataType = "decimal";
dataResult = "0";
break;
case "datetime":
dataType = "DateTime";
dataResult = "DateTime.MinValue";
break;
case "smalldatetime":
dataType = "DateTime";
dataResult = "DateTime.MinValue";
break;
case "numeric":
dataType = "decimal";
dataResult = "0";
break;
case "image":
dataType = "Byte[]";
dataResult = "new Byte[] { Byte.MinValue }";
break;
case "uniqueidentifier":
dataType = "Guid";
dataResult = "Guid.NewGuid()";
break;
default:
dataType = "string";
dataResult = "String.Empty";
break;
}
writer.WriteLine("private " + dataType + " _" + table.fieldName + " = " + dataResult + ";");
writer.WriteLine();
writer.WriteLine("public " + dataType + " " + table.fieldName);
writer.WriteLine("{");
writer.WriteLine("get {return this._" + table.fieldName + ";}");
writer.WriteLine("set {this._" + table.fieldName + " = value;}");
writer.WriteLine("}");
writer.WriteLine();
}
writer.WriteLine("}");
//dispose writer
writer.Close();
result = true;
Process.Start(pathName);
}
catch (Exception ex)
{
throw ex;
}
return result;
}
#endregion
///
/// Click event to create an Object from a Table
///
///
///
private void btnCreateObject_Click(object sender, EventArgs e)
{
try
{
if (CreateObjectOutput(false))
{
lblResult.Text = "Success";
//MessageBox.Show("Object Constructed Sucessfully");
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Click event for the create custom object
///
///
///
private void btnCreateCustom_Click(object sender, EventArgs e)
{
try
{
if (CreateObjectOutput(true))
{
MessageBox.Show("Object Constructed Sucessfully");
}
}
catch (SqlException ex)
{
MessageBox.Show("Please ensure you use a Select statement only:" + ex.Message);
}
}
private void cboTables_SelectedIndexChanged(object sender, EventArgs e)
{
tableName = cboTables.Text;
txtObjectName.Text = "o" + cboTables.Text.Substring(cboTables.Text.IndexOf("_") + 1);
}
private void btnRefresh_Click(object sender, EventArgs e)
{
BindTableCombo();
lblResult.Text = "";
}
private void frmConstructer_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void btnCreateAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < cboTables.Items.Count; i++)
{
cboTables.SelectedIndex = i;
tableName = cboTables.Text;
txtObjectName.Text = "o" + cboTables.Text.Substring(cboTables.Text.IndexOf("_") + 1);
CreateObjectOutput(false);
}
}
public string tableName { get; set; }
}
}