zoukankan      html  css  js  c++  java
  • 动软 生成 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);
                }
            }
        }
    }
  • 相关阅读:
    NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(3)
    zend studio快捷键
    Guid的生成和数据修整(去除空格和小写字符)
    工作中常用Windows快捷键整理(1)-快速关闭网页
    PHP学习笔记(3)-Zend Studio安装和汉化
    PHP学习笔记(2)
    PHP学习笔记(1)
    判断本机ip是电信还是网通
    作业 4/1
    logging 模块
  • 原文地址:https://www.cnblogs.com/0banana0/p/3230571.html
Copyright © 2011-2022 走看看