Dynamic Software 생성 linq 관련 DAO

14041 단어 LINQ
1단계:새 사용자 정의 템플릿
<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
    TableHost host = (TableHost)(Host);
    host.Fieldlist.Sort(CodeCommon.CompareByintOrder);    
    string identityKey=host.IdentityKey.ColumnName ;
string ClassName =host.TableName;
string ClassNames =host.TableName+"s";
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class <#= ClassName #>_DAO
    {

        public static string Insert(<#= ClassName #> objEntity)
        {
            try
            {
                HISDataContext db = DatabaseHelper.GetDB();
                db.<#= ClassNames #>.InsertOnSubmit(objEntity);
                db.SubmitChanges();
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
            return string.Empty;
        }

        public static string Update(<#= ClassName #> model)
        {
            try
            {
                HISDataContext db = DatabaseHelper.GetDB();
                <#= ClassName #> oldEntity = db.<#= ClassNames #>.First(c => c.<#= identityKey #> == model.<#= identityKey #>);
                DatabaseHelper.Assign<<#= ClassName #>>(model, oldEntity);
                db.SubmitChanges();
                return "";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static string DeleteById(int ID)
        {
            try
            {
                HISDataContext db = DatabaseHelper.GetDB();
                var rs = db.<#= ClassNames #>.Where(c => c.<#= identityKey #> == ID);
                if (rs.Count() > 0)
                {
                    <#= ClassName #> objKey = rs.First<<#= ClassName #>>();
                    db.<#= ClassNames #>.DeleteOnSubmit(objKey);
                    db.SubmitChanges();
                }
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
            return "";
        }

        public static <#= ClassName #> SelectByID(long ID)
        {
            try
            {
                HISDataContext db = DatabaseHelper.GetDB();
                var rs = (from a in db.<#= ClassNames #>
                          where a.<#= identityKey #> == ID
                          select a).ToList<<#= ClassName #>>();
                if (rs.Count() > 0)
                    return rs.First<<#= ClassName #>>();
                else
                    return null;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static List<<#= ClassName #>> SelectAll()
        {
            try
            {
                HISDataContext db = DatabaseHelper.GetDB();
                var list = (from n in db.<#= ClassNames #>
                            orderby n.<#= identityKey #> descending
                            select n).ToList<<#= ClassName #>>();
                if (list.Count > 0)
                {
                    return list;
                }
                else
                {
                    return null;
                }

            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }  

    }

}

 
만약 당신의dao명칭이 표명이라면 두 번째 단계를 무시합니다
2단계: 대량 수정 생성 파일 이름
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Tools
{
    /*
     * kook      dal                  _dao
     *   :t_user.cs-->t_user_DAO.cs
     */
    class EditFileName
    {
        static void Main(string[] args)
        {
            Console.WriteLine("          dao");
            RenameFile(@"D:\CodematicDemo\linq", "_DAO");
            Console.WriteLine("     ");
            Console.ReadKey();
        }
        public static void RenameFile(string ParentDir, string stringFront)
        {
            string[] files = Directory.GetFiles(ParentDir, "*.cs", SearchOption.TopDirectoryOnly);
            foreach (string file in files)
            {
                string filename = Path.GetFileName(file);
                string pathname = Path.GetDirectoryName(file);
                filename = filename.Substring(0,filename.Length-3) + "_DAO"+".cs";
                FileInfo fi = new FileInfo(file);
                fi.MoveTo(Path.Combine(pathname, filename));
            }
            string[] dirs = Directory.GetDirectories(ParentDir);
            foreach (string dir in dirs)
            {
                RenameFile(dir, stringFront);
            }
        }
    }
}

좋은 웹페이지 즐겨찾기