数据库
首页 > 数据库> > SqlSugar创建对象SqlSugarBase

SqlSugar创建对象SqlSugarBase

作者:互联网

 sqlsugar安装:

官网Nuget安装 - SqlSugar 5x - .NET果糖网 (donet5.com)


SqlSugar打包:

public class SqlSugarBase
    {
        private static string MyConn { get => ConfigurationManager.ConnectionStrings["myconn"].ConnectionString; }
        private static SqlSugarClient Client
        {
            get => new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = MyConn,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,//自动释放
                InitKeyType = InitKeyType.SystemTable
            });

        }
        /// <summary>
        /// 打印SQL语句
        /// </summary>
        /// <param name="db"></param>
        private static void WriteSQL(SqlSugarClient db)
        {
            db.Aop.OnLogExecuting = (sql, pars) =>
             {
                 Log.log(sql,new System.Diagnostics.StackTrace(true));
                 if(pars!=null&&pars.Length>0) Log.log(string.Join(",", pars?.Select(it => it.ParameterName + "=" + it.Value)), new System.Diagnostics.StackTrace(true));
             };
        }
        public static SqlSugarClient DB
        {
            get
            {
                SqlSugarClient db = Client;
                WriteSQL(db);
                return db;
            }
        }
    }

在DAL中继承或者直接使用

例如:

 public class GuardDAL:SqlSugarBase
    {
        public static DataTable GetInfo()
        {
            return DB.Queryable<Guard>().ToDataTable();
        }
        public static DataTable GetInfo(int id)
        {
            return DB.Queryable<Guard>().Where(it=>it.Id==id).ToDataTable();
        }
    }

以上是继承SqlSugarBase,sqlsugar的简单使用。

注:Log是日志文件,将sql语句以及参数打印。

/// <summary>
    /// 日志文件
    /// </summary>
    public class Log
    {
        // 定义单例模式的变量
        private static Log instance = null;
        //log输出文件
        private const string TOFILE_NAME = "\\log.txt";
        log输出路径
        //private const string TOFILE_PATH = "D:\\LOG\\";

        // 单例模式私有的构造方法
        private Log()
        {

        }


        public static Log GetInstance()
        {
            if (instance == null)
            {
                instance = new Log();
            }
            return instance;
        }


        public static string PHYSICAL_PATH { get; set; }
        public static string LAST_MSG { get; set; }

        //public static string ReadSystemLog()
        //{
        //    return File.ReadAllText(LogReadonly.LOG_FILE_PATH + LogReadonly.LOG_FILE_NAME_SYSTEM, Encoding.UTF8);
        //}


        /// 创建日志
        /// <param name="message">日志信息</param>
        /// <param name="from">来自哪儿</param>
        /// <param name="number">行数</param>
        public static void log(string message, System.Diagnostics.StackTrace st)
        {
            string cont = "";
            //当前路径
            //string path = "../log/";
            string logFolderPath = AppDomain.CurrentDomain.BaseDirectory;
            logFolderPath = logFolderPath.Remove(logFolderPath.Length - 1, 1);
            logFolderPath = logFolderPath.Replace("/", "\\");
            logFolderPath = logFolderPath + "\\log";
            //file
            string fileName = logFolderPath + TOFILE_NAME;

            DirectoryInfo dir = new DirectoryInfo(logFolderPath);
            dir.Create();//自行判断一下是否存在。

            FileInfo fileInf = new FileInfo(fileName);
            if (File.Exists(fileName))//如何文件存在 则在文件后面累加
            {
                FileStream logFss = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamReader r = new StreamReader(logFss);
                cont = r.ReadToEnd();
                r.Close();
                logFss.Close();
            }
            FileStream logFs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamWriter n = new StreamWriter(logFs);
            n.Write(cont);
            n.Write("时间:" + DateTime.Now.ToString() + "\t" + " 信息:" + message +"\t" + method(st) + "\r\n");
            n.Close();
            logFs.Close();
            if (fileInf.Length >= 1024 * 1024 * 200)
            {
                string NewName = logFolderPath + "Log" + DateTime.Now.ToString().Trim().Replace("/", "").Replace(":", "").ToString() + ".txt";
                File.Move(fileName, NewName);
            }
        }
        private static string method(System.Diagnostics.StackTrace st)
        {
            System.Diagnostics.StackFrame sf = st.GetFrame(0);
            string mes = System.IO.Path.GetFileNameWithoutExtension(sf.GetFileName().ToString()) + "." + sf.GetMethod().Name + "\t" + sf.GetFileLineNumber() + "行" + sf.GetFileColumnNumber() + "列";
            return mes;
        }
    }

标签:string,创建对象,logFolderPath,static,new,SqlSugarBase,public,SqlSugar,Log
来源: https://blog.csdn.net/ftfmatlab/article/details/120738672