用ASP.NET写你自己的代码生成器(3)。

80酷酷网    80kuku.com

  asp.net#region Make Form
        /// <summary>
        /// 生成表单项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, System.EventArgs e)
        {
            if(this.DropDownList1.SelectedIndex!=-1)
            {
                m_Scon.ConnectionString = "user id="+this.txtUserName.Text+
                    ";password="+this.txtPassword.Text+
                    ";initial catalog="+this.DropDownList3.SelectedItem.Text+
                    ";data source="+this.txtServerName.Text;
                SqlCommand m_Scmd = new SqlCommand("sp_columns",m_Scon);
                m_Scmd.CommandType = CommandType.StoredProcedure;
                SqlParameter myParm = m_Scmd.Parameters.Add("table_name",SqlDbType.VarChar,100);
                myParm.Value = this.DropDownList1.SelectedItem.Value;
                m_Scon.Open();
                SqlDataReader m_Sdr = m_Scmd.ExecuteReader();
                string ColName = "";
                string ColType = "";
                while(m_Sdr.Read())
                {
                    ColName = m_Sdr["COLUMN_NAME"].ToString();
                    ColType = m_Sdr["TYPE_NAME"].ToString();
                }
                m_Sdr.Close();
                m_Scon.Close();
            }
        }
        #endregion

        #region Make object
        /// <summary>
        /// 生成实体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, System.EventArgs e)
        {
            if(this.DropDownList1.SelectedIndex!=-1)
            {
                m_Scon.ConnectionString = "user id="+this.txtUserName.Text+
                    ";password="+this.txtPassword.Text+
                    ";initial catalog="+this.DropDownList3.SelectedItem.Text+
                    ";data source="+this.txtServerName.Text;
                SqlCommand m_Scmd = new SqlCommand("sp_columns",m_Scon);
                m_Scmd.CommandType = CommandType.StoredProcedure;
                SqlParameter myParm = m_Scmd.Parameters.Add("table_name",SqlDbType.VarChar,100);
                myParm.Value = this.DropDownList1.SelectedItem.Value;
                m_Scon.Open();
                SqlDataReader m_Sdr = m_Scmd.ExecuteReader();
                string TableName = this.DropDownList1.SelectedItem.Value;
                string m_LayOut = "";

                m_LayOut = m_LayOut + "using System;\r\n";
                m_LayOut = m_LayOut + "using System.Data;\r\n" ;
                m_LayOut = m_LayOut + "using System.Data.SqlClient;\r\n";
                m_LayOut = m_LayOut + "\r\n";
                m_LayOut = m_LayOut + "namespace OceanSoft\r\n";
                m_LayOut = m_LayOut + "{\r\n";

                m_LayOut = m_LayOut + BL(4) + "///<comment>\r\n";
                m_LayOut = m_LayOut + BL(4) + "///\r\n";
                m_LayOut = m_LayOut + BL(4) + "///</comment>\r\n";

                string ColName = "";     //列名
                string ColType = "";     //列的数据类型
                string ColDefine = "";   //列定义
                string ColProperty = ""; //列属性
                while(m_Sdr.Read())
                {
                    ColName = m_Sdr["COLUMN_NAME"].ToString();
                    ColType = GT(m_Sdr["TYPE_NAME"].ToString());
                    
                    ColDefine = ColDefine + BL(8) + "private " + ColType + " m_" + ColName + ";\r\n";

                    ColProperty = ColProperty + BL(8) + "\r\n";
                    ColProperty = ColProperty + BL(8) + "public " + ColType + " " + ColName + "\r\n";
                    ColProperty = ColProperty + BL(8) + "{\r\n";
                    ColProperty = ColProperty + BL(12) + "get\r\n";
                    ColProperty = ColProperty + BL(12) + "{\r\n";
                    ColProperty = ColProperty + BL(16) + "return m_" + ColName + " ;\r\n";
                    ColProperty = ColProperty + BL(12) + "}\r\n";
                    ColProperty = ColProperty + BL(12) + "set\r\n";
                    ColProperty = ColProperty + BL(12) + "{\r\n";
                    ColProperty = ColProperty + BL(16) + "m_" + ColName + " = value ;\r\n";
                    ColProperty = ColProperty + BL(12) + "}\r\n";
                    ColProperty = ColProperty + BL(8) + "}\r\n";
                    
                }
                m_Sdr.Close();
                m_Scon.Close();

                m_LayOut = m_LayOut + BL(4) + "public class " + TableName + "\r\n";
                m_LayOut = m_LayOut + BL(4) + "{\r\n";

                m_LayOut = m_LayOut + BL(4) + "\r\n";
                m_LayOut = m_LayOut + ColDefine;  //定义

                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "///构造函数\r\n";
                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "public " + TableName + "()\r\n";  //构造函数
                m_LayOut = m_LayOut + BL(8) + "{\r\n";
                m_LayOut = m_LayOut + BL(8) + "}\r\n\r\n";

                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "///析构函数\r\n";
                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "~" + TableName + "()\r\n";  //析构函数
                m_LayOut = m_LayOut + BL(8) + "{\r\n";
                m_LayOut = m_LayOut + BL(8) + "}\r\n";

                m_LayOut = m_LayOut + ColProperty;  //实体属性

                m_LayOut = m_LayOut + BL(4) + "}\r\n";

                m_LayOut = m_LayOut + "}\r\n";

                this.txtLayOut.Text = m_LayOut;
            }
        }
        #endregion

        #region Test
        private void Button5_Click(object sender, System.EventArgs e)
        {
            //
        }
        #endregion

        #region Functions
        private string BL(int values)
        {
            switch(values)
            {
                case 4:
                    return "\t";
                case 8:
                    return "\t\t";
                case 12:
                    return "\t\t\t";
                case 16:
                    return "\t\t\t\t";
                case 20:
                    return "\t\t\t\t\t";
            }
            return "";
        }

        private string GT(string Values)
        {
            switch(Values.ToUpper())
            {
                case "INT":
                    return "int";
                case "NVARCHAR":
                    return "string";
                case "TINYINT":
                    return "int";
                case "INT IDENTITY":
                    return "int";
                case "BIT":
                    return "int";
                case "UNIQUEIDENTIFIER":
                    return "string";
                case "DATETIME":
                    return "string";
                case "VARCHAR":
                    return "string";
                default:
                    return "string";
            }
        }
        #endregion

        #region Make Object Collection Class
        private void Button6_Click(object sender, System.EventArgs e)
        {
            if(this.DropDownList1.SelectedIndex!=-1)
            {
                string TableName = this.DropDownList1.SelectedItem.Value;
                string m_LayOut = "";

                m_LayOut = m_LayOut + "using System;\r\n";
                m_LayOut = m_LayOut + "using System.Data;\r\n" ;
                m_LayOut = m_LayOut + "using System.Data.SqlClient;\r\n";
                m_LayOut = m_LayOut + "using System.Collections;\r\n";
                m_LayOut = m_LayOut + "\r\n";

                m_LayOut = m_LayOut + "namespace e3.Pantheon.WorkFlow.Structure\r\n";
                m_LayOut = m_LayOut + "{\r\n";  

                m_LayOut = m_LayOut + BL(4) + "///<comment>\r\n";
                m_LayOut = m_LayOut + BL(4) + "///公司名称:苏州中软公司\r\n";
                m_LayOut = m_LayOut + BL(4) + "///作者:\r\n";
                m_LayOut = m_LayOut + BL(4) + "///创建日期:"+System.DateTime.Now.ToShortDateString()+"\r\n";
                m_LayOut = m_LayOut + BL(4) + "///用途说明:\r\n";
                m_LayOut = m_LayOut + BL(4) + "///修改记录:\r\n";
                m_LayOut = m_LayOut + BL(4) + "///</comment>\r\n";

                m_LayOut = m_LayOut + BL(4) + "public class " + TableName + "s : System.Collections.CollectionBase\r\n";
                m_LayOut = m_LayOut + BL(4) + "{\r\n";

                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "///构造函数\r\n";
                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "public " + TableName + "s()\r\n";  //构造函数
                m_LayOut = m_LayOut + BL(8) + "{\r\n";
                m_LayOut = m_LayOut + BL(12) + "//TODO:在这里增加构造函数逻辑\r\n";
                m_LayOut = m_LayOut + BL(8) + "}\r\n\r\n";

                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "///析构函数\r\n";
                m_LayOut = m_LayOut + BL(8) + "///<summary>\r\n";
                m_LayOut = m_LayOut + BL(8) + "~" + TableName + "s()\r\n";  //析构函数
                m_LayOut = m_LayOut + BL(8) + "{\r\n";
                m_LayOut = m_LayOut + BL(12) + "//TODO:在这里增加析构函数逻辑\r\n";
                m_LayOut = m_LayOut + BL(8) + "}\r\n\r\n";

                m_LayOut = m_LayOut + BL(8) + "public void Remove(int index)\r\n";   //删除
                m_LayOut = m_LayOut + BL(8) + "{\r\n";
                m_LayOut = m_LayOut + BL(12) + "if(index>Counter-1||Counter<0)\r\n";
                m_LayOut = m_LayOut + BL(12) + "{}\r\n";
                m_LayOut = m_LayOut + BL(12) + "else\r\n";
                m_LayOut = m_LayOut + BL(12) + "{\r\n";
                m_LayOut = m_LayOut + BL(16) + "List.RemoveAt(index);\r\n";
                m_LayOut = m_LayOut + BL(12) + "}\r\n";
                m_LayOut = m_LayOut + BL(8) + "}\r\n\r\n";

                m_LayOut = m_LayOut + BL(8) + "public void Add("+TableName+" m_"+TableName+")\r\n";  //增加
                m_LayOut = m_LayOut + BL(8) + "{\r\n";
                m_LayOut = m_LayOut + BL(12) + "List.Add("+" m_"+TableName+");\r\n";
                m_LayOut = m_LayOut + BL(8) + "}\r\n\r\n";

                m_LayOut = m_LayOut + BL(8) + "public "+TableName+" this[int index]\r\n";  //集合的元素
                m_LayOut = m_LayOut + BL(8) + "{\r\n";

                m_LayOut = m_LayOut + BL(12) + "get\r\n";  
                m_LayOut = m_LayOut + BL(12) + "{\r\n";
                m_LayOut = m_LayOut + BL(16) + "if(index <List.Count)\r\n";
                m_LayOut = m_LayOut + BL(16) + "{\r\n";
                m_LayOut = m_LayOut + BL(20) + "return ("+TableName+")List[Index];\r\n";
                m_LayOut = m_LayOut + BL(16) + "}\r\n";
                m_LayOut = m_LayOut + BL(16) + "else\r\n";
                m_LayOut = m_LayOut + BL(16) + "{\r\n";
                m_LayOut = m_LayOut + BL(16) + "return null;\r\n";
                m_LayOut = m_LayOut + BL(16) + "}\r\n";
                m_LayOut = m_LayOut + BL(12) + "}\r\n";

                m_LayOut = m_LayOut + BL(12) + "set\r\n";
                m_LayOut = m_LayOut + BL(12) + "{\r\n";
                m_LayOut = m_LayOut + BL(16) + "if(iIndex<List.Count)\r\n";
                m_LayOut = m_LayOut + BL(16) + "{\r\n";
                m_LayOut = m_LayOut + BL(20) + "List[index]=value;\r\n";
                m_LayOut = m_LayOut + BL(16) + "}\r\n";
                m_LayOut = m_LayOut + BL(12) + "}\r\n";

                m_LayOut = m_LayOut + BL(8) + "}\r\n";

                m_LayOut = m_LayOut + BL(4) + "}\r\n";  //class

                m_LayOut = m_LayOut + "}\r\n";  //namespace

                this.txtLayOut.Text = m_LayOut;
            }
        }
        #endregion

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: