zoukankan      html  css  js  c++  java
  • C# DBHelper 数据库连接帮助类

    DBHelper 帮助类

    1.作用:将C#应用程序与SQL SERVER数据库交互的公共方法(增删改查)提取出来,方便调用。

    2.要求:C#入门级程序员必须熟练掌握编写

    3.组成:三个公用方法。

    ①ExecuteNonQuery   增删改

    ②ExecuteScalar  查询返回单行单列

    ③ExecuteQuery  查询返回多行多列

    4.代码明细:

    public class DBHelper
        {
            public static string strConn = "Server=01D023101509880;uid=CG;pwd=abc.1234;Database=QQChat";
            /// <summary>
            /// 增、删、改通用方法
            /// </summary>
            /// <param name="Sql">SQL 语句</param>
            /// <param name="paras">可选参数</param>
            /// <returns></returns>
            public static int ExecuteNonQuery(string Sql, params SqlParameter[] paras)
            {
                int result = 0;
                //using 自动关闭资源,自带异常处理机制
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();//打开连接
                    SqlCommand command = new SqlCommand(Sql, conn);//执行命令
                    command.Parameters.AddRange(paras);//加入参数
                    result = command.ExecuteNonQuery(); //执行 
                }
                return result;
            }
            /// <summary>
            /// 查询单行当列
            /// </summary>
            /// <param name="Sql">SQL 语句</param>
            /// <param name="paras">可选参数</param>
            /// <returns></returns>
            public static Object ExecuteScalar(string Sql, params SqlParameter[] paras)
            {
                Object result;
                //using 自动关闭资源,自带异常处理机制
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();//打开连接
                    SqlCommand command = new SqlCommand(Sql, conn);//执行命令
                    command.Parameters.AddRange(paras);//加入参数
                    result = command.ExecuteScalar(); //执行 
                }
                return result;
            }
            /// <summary>
            /// 查询返回多行多列;泛型升级未知对象类型;泛型类,泛型方法
            /// </summary>
            /// <param name="Sql">SQL 语句</param>
            /// <param name="paras">可选参数</param>
            /// <returns></returns>
            public static List<T> ExecuteQuery<T>(string Sql, params SqlParameter[] paras)
            {
                List<T> list = new List<T>();//创建泛型集合对象
                using (SqlConnection conn = new SqlConnection(strConn))
                {
                    conn.Open();//打开连接
                    SqlCommand command = new SqlCommand(Sql, conn);//执行命令
                    command.Parameters.AddRange(paras);//加入参数
                    using (SqlDataReader reader = command.ExecuteReader())//执行游标
                    {
                        Type type = typeof(T);//检测类型
                        //next 往下执行
                        while (reader.Read())
                        {
                            //创建对象
                            T t=(T)Activator.CreateInstance(type);
                            foreach(PropertyInfo pi in type.GetProperties()) //反射 PropertyInfo类获取基类的属性
                            {
                                pi.SetValue(t, reader[pi.Name] is DBNull ? null:reader[pi.Name]); //ORM 将游标的值赋给对象
                            }
                            list.Add(t);
                        }
                    }
                }
    
                return list;
            }
    
        }
    }
  • 相关阅读:
    《编译原理》-用例题理解-自顶向下语法分析及 FIRST,FOLLOW,SELECT集,LL(1)文法
    8 张脑图入门 JavaScript
    Navicat Premium 12连接Oracle时提示oracle library is not loaded的问题解决
    Spring boot 多模块项目 + Swagger 让你的API可视化
    Spring Boot -05- 多模块结构项目构建与测试(详细图文教程)IDEA 版
    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)
    SSM 项目从搭建爬坑到 CentOS 服务器部署
    LeetCode
    有趣的位运算
    记一次向maven中央仓库提交依赖包
  • 原文地址:https://www.cnblogs.com/cuig/p/12593753.html
Copyright © 2011-2022 走看看