string.Join一个容易被忽视的函数

join方法可以将数组通过指定的分隔符组合起来:

 public bool ExcuteCommand<T>(T t) where T : new()
        {
            string sqlColumn = string.Join(",", t.GetType().GetProperties().Select(m => m.Name).ToList());
            string sqlParams = string.Join(",", t.GetType().GetProperties().Select(m => string.Format($"@{m.Name}")));
            //定义参数
            SqlParameter[] paras = new SqlParameter[t.GetType().GetProperties().Length];
            for (int i = 0; i < paras.Length; i++)
            {
                PropertyInfo pi = t.GetType().GetProperties()[i];
                paras[i] = new SqlParameter($"@{pi.Name}", pi.GetValue(t));
            }
            string sql = string.Format($"INSERT INTO {t.GetType().Name} ({sqlColumn.Trim(',')}) VALUES ({sqlParams.Trim(',')})");
            return new SqlHelper().ExecuteCommand(sql, paras);
        }
 bool flag = db.ExcuteCommand<Sys_DisableInfo>
                (new Sys_DisableInfo
                {
                    Data = "14751959991",
                    JoinMan = "陈世盛的测试"
                });

猜你喜欢

转载自blog.csdn.net/qq_31975127/article/details/86364546