Thursday 3 August 2017

Generate Property and Methods Tool

You can automatically generate the property and method and insert query which help you to build the backed code faster.


Create an window application like below ;




Use below code,

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.Data.SqlClient;

namespace CreateProperty
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=REETPC;Initial Catalog=propertyHelper;Persist Security Info=True;User ID=sa;Password=password1");
        SqlDataReader rd;
        SqlCommand cm = new SqlCommand();
        string fieldName, dataType;
        public Form1()
        {
            InitializeComponent();
        }

        #region Private method for helping to create data
        /// <summary>
        /// Change the string type as the first latter as small lattre
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string getFirstSmall(string str)
        {
            string getStr;
            getStr = str.Substring(0, 1).ToLower();
            getStr = getStr + str.Substring(1, str.Length - 1);
            return getStr;
        }
        /// <summary>
        /// Open the connection
        /// </summary>
        ///
        private void GetConnection()
        {
            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
                rd.Close();
            }

            cn.Open();
            cm.Connection = cn;
            cm.CommandType = CommandType.Text;
            cm.CommandText = "select * from properties";

            rd = cm.ExecuteReader();
        }

        /// <summary>
        /// close the connection
        /// </summary>
        private void CloseConnection()
        {
            cn.Close();
            rd.Close();
        }

        # endregion
        private void btnProperty_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;

            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
                rd.Close();
            }

            cn.Open();
            cm.Connection = cn;
            cm.CommandType = CommandType.Text;
            cm.CommandText = "select * from properties";

            rd = cm.ExecuteReader();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();

                    strProp += "\r\n";

                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        // strProp += "public string " + fieldName + "\n {  get { return _" + getFirstSmall(fieldName) + ";} \n set {  _" + getFirstSmall(fieldName) + " = value; }  }\n";
                        strProp += "public string " + fieldName + "{ get; set; };\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += "public string " + fieldName + "{ get; set; };\n";
                        //strProp += "public string " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; } \n set {  _" + getFirstSmall(fieldName) + " = value; }  }\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += "public int " + fieldName + "{ get; set; };\n";
                        //  strProp += "public int " + fieldName + " \n{  get { return _" + getFirstSmall(fieldName) + ";} \n set {  _" + getFirstSmall(fieldName) + " = value; }  \n}\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += "public bool " + fieldName + "{ get; set; };\n";
                        //  strProp += "public bool " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; } \n set {  _" + getFirstSmall(fieldName) + " = value; }  \n}\n";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += "public DateTime " + fieldName + "{ get; set; };\n";
                        // strProp += "public DateTime " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; }\n set {  _" + getFirstSmall(fieldName) + " = value; \n } \n }";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += "public decimal " + fieldName + "{ get; set; };\n";
                        // strProp += "public decimal " + fieldName + "\n { get { return _" + getFirstSmall(fieldName) + "; }\n set {  _" + getFirstSmall(fieldName) + " = value; \n } \n }";
                    }

                }
            }
            catch { }

            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnAttib_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            GetConnection();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();

                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        strProp += "\n string _" + getFirstSmall(fieldName) + " = string.Empty;";

                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += "\n string _" + getFirstSmall(fieldName) + " = string.Empty;";

                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += "\n int _" + getFirstSmall(fieldName) + " = 0;";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += "\n bool _" + getFirstSmall(fieldName) + " = false;";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += "\n DateTime _" + getFirstSmall(fieldName) + " = DateTime.Now;";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += "\n decimal _" + getFirstSmall(fieldName) + " = 0.0M;";
                    }

                }
            }
            catch { }
            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnConstruct_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            SqlDataReader clonerd = null;
            strProp = "public ClassName(";
            GetConnection();
            clonerd = rd;
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        strProp += ",string " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += ",int " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += ",bool " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += ",DateTime " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += ",decimal " + fieldName;
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += ",string " + fieldName;
                    }

                }
            }
            catch { }
            strProp += ")";
            strProp += getInnerBodyConstructor();
            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnReader_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            strProp = "private void _loadInternalData(SqlDataReader oDataReader,bool bAlreadyPresent) \n {";
            GetConnection();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    if (dataType.Substring(0, 3).ToUpper() == "VAR")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsString(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "TEX")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsString(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "INT")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsInt32(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "BIT")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsBool(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "DAT")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsDateTime(oDataReader[\"" + fieldName + "\"]);";
                    }
                    if (dataType.Substring(0, 3).ToUpper() == "FLO")
                    {
                        strProp += "\n _" + getFirstSmall(fieldName) + " = CheckIsDecimal(oDataReader[\"" + fieldName + "\"]);";
                    }

                }
            }
            catch { }
            strProp += "\n }";
            CloseConnection();
            txtGetValues.Text = strProp;
        }
        /// <summary>
        /// Call the inner body of constructor
        /// </summary>
        /// <param name="rd"></param>
        /// <returns></returns>
        private string getInnerBodyConstructor()
        {
            CloseConnection();
            GetConnection();

            string strProp = string.Empty;
            strProp += "{";
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    strProp += "_" + getFirstSmall(fieldName) + "=" + fieldName + ";\n";
                }
            }
            catch { }
            return strProp;
        }
        private void BTNdal_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            strProp += "internal static  Int16 MethodName(ClassName  objname)";
            strProp += "\n{";
            strProp += " \n Int16 ret = 0;\n DataSet dts = new DataSet(); \n try \n{";
            strProp += "\n SpParamCollection  spParams = new SpParamCollection();";

            GetConnection();
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    strProp += "\n spParams.Add(new SpParam(\"@" + fieldName + "\", objname." + fieldName + "));";
                }
            }
            catch { }
            strProp += "\n ret = ExecProcNonQuery(\" Spname \", spParams);";
            strProp += "\n }";
            strProp += "\n catch (Exception ex)\n { \n  ex.Message.ToString(); \n } \n return ret; \n }";
            CloseConnection();
            txtGetValues.Text = strProp;
        }

        private void btnBAL_Click(object sender, EventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            string strProp = string.Empty;
            SqlDataReader clonerd = null;
            GetConnection();
            clonerd = rd;
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();

                    strProp += "@" + fieldName + " " + dataType + ",\n";


                }
            }
            catch { }
            strProp += "BEGIN \n\nINSERT INTO TableName \n( ";

            strProp += GetFiledOfTable();
            strProp += ")\n Values \n (";

            strProp += GetValuesOfTable();
            strProp += ") \n\nEND";

            txtGetValues.Text = strProp;
        }
        #region To craete insert storeprocedure
        private string GetFiledOfTable()
        {
            CloseConnection();
            GetConnection();

            string strProp = string.Empty;
            while (rd.Read())
            {
                fieldName = rd["fieldName"].ToString();
                dataType = rd["dataType"].ToString();
                strProp += fieldName + ",\n";
            }
            return strProp;
        }
        private string GetValuesOfTable()
        {
            CloseConnection();
            GetConnection();
            string strProp = string.Empty;
            try
            {
                while (rd.Read())
                {
                    fieldName = rd["fieldName"].ToString();
                    dataType = rd["dataType"].ToString();
                    strProp += "@" + fieldName + ",\n";
                }
            }
            catch { }
            return strProp;
        }
        #endregion
    }
}

Database coding.

Need to create table

USE [propertyHelper]
GO

/****** Object:  Table [dbo].[properties]    Script Date: 08/03/2017 14:01:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[properties](
      [fieldName] [varchar](100) NULL,
      [dataType] [varchar](100) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO





No comments:

Post a Comment