PSI.DAL/CreateSql.cs

(开头部分) 6KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using PSI.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PSI.DAL
{
        public class CreateSql
        {
                /// <summary>
                /// 生成Insert语句
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="t"></param>
                /// <param name="cols"></param>
                /// <returns></returns>
                public static SqlModel GetInsertSqlAndParas<T>(T t, string cols, int isReturn)
                {
                        Type type = typeof(T);
                        PropertyInfo[] properties = PropertyHelper.GetTypeProperties<T>(cols);
                        string priName = type.GetPrimary();
                        //生成要插入的列 {1}
                        string columns = string.Join(",", properties.Where(p => p.Name != priName).Select(p => $"[{p.GetColName()}]"));
                        //生成插入的参数{2}
                        string paraColumns = string.Join(",", properties.Where(p => p.Name != priName).Select(p => $"@{p.GetColName()}"));
                        //参数数组的生成
                        SqlParameter[] arrParas = CreateParameters<T>(properties, t);

                        //sql语句
                        string sql = $"INSERT INTO [{type.GetTName()}] ({columns}) VALUES ({paraColumns}) ";
                        if (isReturn == 1)
                                sql += ";select @@identity";
                        return new SqlModel() { Sql = sql, SqlParaArray = arrParas };

                }

                /// <summary>
                /// 生成Update语句
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="t"></param>
                /// <param name="cols"></param>
                /// <returns></returns>
                public static SqlModel GetUpdateSqlAndParas<T>(T t, string cols, string strWhere)
                {
                        Type type = typeof(T);
                        //获取所有属性
                        PropertyInfo[] properties = PropertyHelper.GetTypeProperties<T>(cols);
                        string priName = type.GetPrimary();
                        //生成要更新的列 {1}
                        string columns = string.Join(",", properties.Where(p => p.Name != priName).Select(p => string.Format("[{0}]=@{0}", p.GetColName())));
                        ;            //参数数组的生成
                        SqlParameter[] arrParas = CreateParameters<T>(properties, t);

                        if (string.IsNullOrEmpty(strWhere))
                        {
后面还有 90 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。