动软 生成 linq相关DAO

第一步:新建自定义模板

<#@ 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名称就是表名的话 那忽略第二步

第二步:批量修改生成文件名

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:CodematicDemolinq", "_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);
            }
        }
    }
}
原文地址:https://www.cnblogs.com/0banana0/p/3230571.html