首页 > 编程 > .NET > 正文

用ASP.NET写你自己的代码生成器

2024-07-10 13:06:45
字体:
来源:转载
供稿:网友

frmmain.aspx

<%@ page language="c#" codebehind="frmmain.aspx.cs" autoeventwireup="false" inherits="web_test.frmmain" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
    <head>
        <title>frmmain</title>
        <meta name="generator" content="microsoft visual studio 7.0">
        <meta name="code_language" content="c#">
        <meta name="vs_defaultclientscript" content="javascript">
        <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
    </head>
    <body ms_positioning="gridlayout">
        <form id="frmmain" method="post" runat="server">
            <asp:textbox id="txtservername" runat="server" font-size="10pt" design_time_lock="true">192.168.0.220</asp:textbox>
            <asp:label id="label7" runat="server" width="79px" height="20px" font-size="10pt" design_time_lock="true">数据库列表</asp:label>
            <asp:label id="label6" runat="server" width="53px" height="20px" font-size="10pt" design_time_lock="true" visible="false">字段名</asp:label>
            <asp:label id="label5" runat="server" width="53px" height="20px" font-size="10pt" design_time_lock="true">表名</asp:label>
            <asp:button id="button1" runat="server" width="102px" text="gettable" font-size="10pt" design_time_lock="true"></asp:button>
            <asp:label id="label4" runat="server" width="79px" height="20px" font-size="10pt" design_time_lock="true">密码</asp:label>
            <asp:label id="label2" runat="server" width="79px" height="20px" font-size="10pt" design_time_lock="true">用户名</asp:label>
            <asp:label id="label1" runat="server" width="79px" height="20px" font-size="10pt" design_time_lock="true">服务器名</asp:label>
            <asp:textbox id="txtpassword" runat="server" font-size="10pt" backcolor="transparent" forecolor="black" bordercolor="white" design_time_lock="true"></asp:textbox>
            <asp:textbox id="txtusername" runat="server" font-size="10pt" design_time_lock="true">sa</asp:textbox>
            <asp:dropdownlist id="dropdownlist1" runat="server" width="208px" autopostback="true" font-size="10pt" design_time_lock="true"></asp:dropdownlist>
            <asp:dropdownlist id="dropdownlist2" runat="server" width="208px" height="28px" font-size="10pt" design_time_lock="true" visible="false"></asp:dropdownlist>
            <asp:button id="button2" runat="server" width="107px" text="getdatabase" design_time_lock="true"></asp:button>
            <asp:dropdownlist id="dropdownlist3" runat="server" width="207px" height="25px" design_time_lock="true"></asp:dropdownlist>
            <asp:button id="button3" runat="server" width="110px" text="生 成" design_time_lock="true" visible="false"></asp:button>
            <asp:textbox id="txtlayout" runat="server" width="619px" height="367px" textmode="multiline" backcolor="ivory" design_time_lock="true"></asp:textbox>
            <asp:button id="button4" runat="server" height="25px" width="105px" text="实体类" design_time_lock="true"></asp:button>
            <asp:button id="button5" runat="server" width="107px" text="test" height="25px" design_time_lock="true" visible="false"></asp:button>
            <asp:button id="button6" runat="server" height="23px" width="105px" text="集合类" design_time_lock="true" visible="false"></asp:button>
        </form>
    </body>
</html>
frmmain.aspx.cs

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace web_test
{
    /// <summary>
    /// 作者:davi
    /// 日期:3003-03-18
    /// </summary>
    public class frmmain : system.web.ui.page
    {
        protected system.web.ui.webcontrols.label label1;
        protected system.web.ui.webcontrols.label label2;
        protected system.web.ui.webcontrols.label label4;
        protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
        protected system.web.ui.webcontrols.label label5;
        protected system.web.ui.webcontrols.textbox txtservername;
        protected system.web.ui.webcontrols.textbox txtusername;
        protected system.web.ui.webcontrols.textbox txtpassword;
        protected system.web.ui.webcontrols.button button1;
        protected system.web.ui.webcontrols.dropdownlist dropdownlist2;
        protected system.web.ui.webcontrols.label label6;
        protected system.web.ui.webcontrols.label label7;
        protected system.web.ui.webcontrols.button button2;
        protected system.web.ui.webcontrols.dropdownlist dropdownlist3;
        protected system.web.ui.webcontrols.button button3;
        protected system.web.ui.webcontrols.textbox txtlayout;
        protected system.web.ui.webcontrols.button button4;
        protected system.web.ui.webcontrols.button button5;
        protected system.web.ui.webcontrols.button button6;
        private sqlconnection m_scon;
        private dataset m_ds;

        private void page_load(object sender, system.eventargs e)
        {
            m_scon = new sqlconnection();
            m_ds = new dataset();
        }


        #region web form designer generated code
        //[stathreadattribute]
        override protected void oninit(eventargs e)
        {
            //
            // codegen:该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
       
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {   
            this.button1.click += new system.eventhandler(this.button1_click);
            this.dropdownlist1.selectedindexchanged += new system.eventhandler(this.dropdownlist1_selectedindexchanged);
            this.button2.click += new system.eventhandler(this.button2_click);
            this.button3.click += new system.eventhandler(this.button3_click);
            this.button4.click += new system.eventhandler(this.button4_click);
            this.button5.click += new system.eventhandler(this.button5_click);
            this.button6.click += new system.eventhandler(this.button6_click);
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion

        #region 显示数据库的表 button1_click
        /// <summary>
        /// 显示数据库的表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_click(object sender, system.eventargs e)
        {
            try
            {
                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_tables",m_scon);
                m_scmd.commandtype = commandtype.storedprocedure;
                sqlparameter myparm = m_scmd.parameters.add("@table_type",sqldbtype.varchar,100);
                myparm.value = "'table'";
                m_scon.open();
                sqldatareader m_sdr = m_scmd.executereader();
                this.dropdownlist1.items.clear();
                while(m_sdr.read())
                {
                    if(m_sdr["table_name"].tostring()!="dtproperties")
                    {
                        listitem m_li = new listitem();
                        m_li.text = m_sdr["table_name"].tostring();
                        m_li.value = m_sdr["table_name"].tostring();
                        this.dropdownlist1.items.add(m_li);
                    }
                }
                m_sdr.close();
                m_scon.close();
            }
            catch
            {
                if(m_scon.state.tostring().toupper()=="open")
                {
                    m_scon.close();
                }
            }
        }

        #endregion

        #region show columns in a table dropdownlist1_selectedindexchanged
        /// <summary>
        /// 显示表的所有列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dropdownlist1_selectedindexchanged(object sender, system.eventargs e)
        {
            try
            {
                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();
                    this.dropdownlist2.items.clear();
                    while(m_sdr.read())
                    {
                        listitem m_li = new listitem();
                        m_li.text = m_sdr["column_name"].tostring();
                        m_li.value = m_sdr["column_name"].tostring();
                        this.dropdownlist2.items.add(m_li);
                    }
                    m_sdr.close();
                    m_scon.close();
                }
            }
            catch
            {
                if(m_scon.state.tostring().toupper()=="open")
                {
                    m_scon.close();
                }
            }
        }

        #endregion

        #region show all database button2_click
        /// <summary>
        /// 显示所有数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_click(object sender, system.eventargs e)
        {
            try
            {
                m_scon.connectionstring = "user id="+this.txtusername.text+
                    ";password="+this.txtpassword.text+
                    ";data source="+this.txtservername.text;
                sqlcommand m_scmd = new sqlcommand("sp_databases",m_scon);
                m_scmd.commandtype = commandtype.storedprocedure;
                m_scon.open();
                sqldatareader m_sdr = m_scmd.executereader();
                this.dropdownlist1.items.clear();
                while(m_sdr.read())
                {
                    listitem m_li = new listitem();
                    m_li.text = m_sdr["database_name"].tostring();
                    m_li.value = m_sdr["database_name"].tostring();
                    this.dropdownlist3.items.add(m_li);
                }
                m_sdr.close();
                m_scon.close();       
            }
            catch
            {
                if(m_scon.state.tostring().toupper()=="open")
                {
                    m_scon.close();
                }
            }
        }

        #endregion
#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) + "///&l, t;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_l..

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表