• SQLHelper


    SQLHelper帮助类。

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DataBaseUility
    {
        /// <summary>
        /// 数据库使用公共类
        /// </summary>
        public class SQLHelper
        {
            public static string Connection
            {
                get
                {
                    return "Data Source=.;Database=ShortMessage;Integrated Security=true";
                }
            }
    
            /// <summary>
            /// 增删改数据
            /// </summary>
            /// <param name="strSql"></param>
            /// <returns></returns>
            public static bool Processing(string strSql)
            {
                using (SqlConnection sqlCon = new SqlConnection(Connection))
                {
                    sqlCon.Open();
                    SqlCommand sqlCom = new SqlCommand(strSql, sqlCon);
                    if (sqlCom.ExecuteNonQuery() > 0)
                    {
                        return true;
                    }
                    return false;
                }
            }
    
            /// <summary>
            /// 指定条件查询,返回实体集合
            /// </summary>
            /// <returns></returns>
            public static List<T> GetTabaleValue<T>(string strSql)
                where T : class,new()
            {
                using (SqlConnection sqlCon = new SqlConnection(DatabaseCommon.Connection))
                {
                    sqlCon.Open();
                    SqlDataAdapter sqlDA = new SqlDataAdapter(strSql, sqlCon);
                    DataTable dt = new DataTable();
                    sqlDA.Fill(dt);
                    List<T> config = DTToModel<T>(dt);
                    return config;
                }
            }
    
            /// <summary>
            /// 数据表转换为实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<T> DTToModel<T>(DataTable dt) where T : class,new()
            {
                Type type = typeof(T);
                PropertyInfo[] files = type.GetProperties();
                List<T> list = new List<T>();
                foreach (DataRow dr in dt.Rows)
                {
                    T model = new T();
                    for (int i = 0; i < files.Count(); i++)
                    {
                        files[i].SetValue(model, dr[i]);
                    }
                    list.Add(model);
                }
                return list;
            }
        }
    }
  • 相关阅读:
    【转贴】Cookie + Session + OAuth + SSO
    zz淘宝商品库MySQL优化实践
    HIVE 数据倾斜调优总结zz
    数据挖掘笔记(一)
    hive函数参考手册
    hive QL(HQL)简明指南zz
    数据挖掘笔记(二)
    python format string (转)
    hive 中转义符使用问题
    关于文档管理
  • 原文地址:https://www.cnblogs.com/cang12138/p/5939576.html
Copyright © 2020-2023  润新知