梅开二度 激情超越

~~常用激扬的句子去激励自己的学生,为什么自己却莫名的沉沦了!~~

自动生成数据层代码!

using System; 
using System.Data; 
using System.IO; 
 
namespace Popo.Web.Tool 

    /**//// <summary> 
    /// DataLayer 的摘要说明。 
    /// </summary> 
    public class DataLayer 
    { 
        public DataLayer() 
        { 
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
        } 
 
        void ExportCSFile(string tableName){ 
            //Popo.Dal.SqlHelp.SqlConnString = "Data Source=localhost;Initial Catalog="+DrpDB.SelectedValue+";User ID=sa;Password=123"; 
            string sql = "select * from "+tableName; 
            DataTable dt = Popo.Dal.SqlHelp.LoadData(sql).Tables[0]; 
 
            string tbName = tableName; 
            string outStr =""; 
            string className = tbName+"Data"; 
            outStr += "namespace Popo.Common.Data{\n"+ 
                "\tusing System;\n"+ 
                "\tusing System.Data;\n\n"+ 
                "\t/// <summary>\n"+ 
                "\t/// "+className+" 的摘要说明。\n"+ 
                "\t/// </summary>\n"+ 
                "\t[System.ComponentModel.DesignerCategory(\"Code\")]\n"+ 
                "\t[SerializableAttribute]\n"+ 
                "\tpublic class "+className+" : DataSet{\n"+ 
                "\t\tpublic "+className+"(){\n"+ 
                //build table 
                "\t\t\tDataTable table = new DataTable(\""+tbName+"\");\n"+ 
                "\t\t\tDataColumnCollection dcc = table.Columns;\n"+ 
                "\t\t\t\n"; 
            foreach(DataColumn dc in dt.Columns){ 
                outStr += "\t\t\tdcc.Add(\""+dc.ColumnName+"\",typeof("+dc.DataType+"));\n"; 
            } 
            outStr += "\t\t\tTables.Add(table);\n"; 
            outStr += "\t\t}\n\n"; 
 
            //property 
            outStr += "\t\t#region 属性\n"; 
            foreach(DataColumn dc in dt.Columns){ 
                outStr += "\t\tpublic "+dc.DataType+" "+dc.ColumnName+"{\n"; 
                outStr += "\t\t\tget{\n"; 
                outStr += "\t\t\t\treturn ("+dc.DataType+")Tables[\""+tbName+"\"].Rows[0][\""+dc.ColumnName+"\"];\n"; 
                outStr += "\t\t\t}\n"; 
                outStr += "\t\t}\n\n"; 
            } 
            outStr += "\t\t#endregion\n"; 
 
            outStr += "\t}\n";//End Class 
            outStr += "}"; //End NameSpace 
 
            System.Web.HttpContext context = System.Web.HttpContext.Current; 
 
            string file = context.Server.MapPath(context.Request.ApplicationPath) + @"\..\Common\Data\"+className+".cs"; 
            if(File.Exists(file)) 
                File.Delete(file); 
             
            FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write); 
            StreamWriter w = new StreamWriter(fs); 
            w.WriteLine(outStr); 
            w.Flush(); 
            w.Close(); 
            fs.Close(); 
        } 
    } 


自动生成代码如下 

namespace Popo.Common.Data{ 
    using System; 
    using System.Data; 
 
    /**//// <summary> 
    /// BoardData 的摘要说明。 
    /// </summary> 
    [System.ComponentModel.DesignerCategory("Code")] 
    [SerializableAttribute] 
    public class BoardData : DataSet{ 
        public BoardData(){ 
            DataTable table = new DataTable("Board"); 
            DataColumnCollection dcc = table.Columns; 
             
            dcc.Add("BoardId",typeof(System.Int32)); 
            dcc.Add("PareId",typeof(System.Int32)); 
            dcc.Add("TreeDepth",typeof(System.Int32)); 
            dcc.Add("BoardName",typeof(System.String)); 
            dcc.Add("BoardAdminUser",typeof(System.String)); 
            dcc.Add("CloseFlag",typeof(System.Boolean)); 
            dcc.Add("BoardMark",typeof(System.String)); 
            Tables.Add(table); 
        } 
 
        属性#region 属性 
        public System.Int32 BoardId{ 
            get{ 
                return (System.Int32)Tables["Board"].Rows[0]["BoardId"]; 
            } 
        } 
 
        public System.Int32 PareId{ 
            get{ 
                return (System.Int32)Tables["Board"].Rows[0]["PareId"]; 
            } 
        } 
 
        public System.Int32 TreeDepth{ 
            get{ 
                return (System.Int32)Tables["Board"].Rows[0]["TreeDepth"]; 
            } 
        } 
 
        public System.String BoardName{ 
            get{ 
                return (System.String)Tables["Board"].Rows[0]["BoardName"]; 
            } 
        } 
 
        public System.String BoardAdminUser{ 
            get{ 
                return (System.String)Tables["Board"].Rows[0]["BoardAdminUser"]; 
            } 
        } 
 
        public System.Boolean CloseFlag{ 
            get{ 
                return (System.Boolean)Tables["Board"].Rows[0]["CloseFlag"]; 
            } 
        } 
 
        public System.String BoardMark{ 
            get{ 
                return (System.String)Tables["Board"].Rows[0]["BoardMark"]; 
            } 
        } 
 
        #endregion 
    } 

posted on 2006-11-13 14:13 梅开二度 激情超越 阅读(89) 评论(0)  编辑 收藏 引用 网摘 所属分类: Web development


只有注册用户登录后才能发表评论。

My Links

Blog Stats

留言簿(3)

随笔分类(22)

随笔档案(21)

文章分类(48)

文章档案(44)

相册

收藏夹(40)

Favorite site

My Friends

搜索

最新评论