zoukankan      html  css  js  c++  java
  • C#封装SqlHelper

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace Ytulab.DAL
    {
        /// <summary>
        /// 功能:SQL数据库访问类
        /// </summary>
        public class SqlHelper
        {
            //连接字符串
            private static readonly string str = ConfigurationManager.ConnectionStrings["ytulab"].ConnectionString;
            /// <summary>
            /// 增删改
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="param">sql参数</param>
            /// <returns>受影响的函数</returns>
            public static int ExecuteNonQuery(string sql, params SqlParameter[] param)
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        if (param != null)
                        {
                            cmd.Parameters.AddRange(param);
                        }
                        return cmd.ExecuteNonQuery();
                    }
                }
            }
    
            /// <summary>
            /// 查询
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="param">sql参数</param>
            /// <returns>首行首列</returns>
            public static object ExecuteScalar(string sql, params SqlParameter[] param)
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        if (param != null)
                        {
                            cmd.Parameters.AddRange(param);
                        }
                        return cmd.ExecuteScalar();
                    }
                }
            }
    
            /// <summary>
            /// 多行查询
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="param">sql参数</param>
            /// <returns>SqlDataReader</returns>
            public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] param)
            {
                SqlConnection con = new SqlConnection();
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (param != null)
                    {
                        cmd.Parameters.AddRange(param);
    
                    }
                    try
                    {
                        con.Open();
                        return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                    }
                    catch (Exception ex)
                    {
                        con.Close();
                        con.Dispose();
                        throw ex;
                    }
                }
            }
    
            /// <summary>
            /// 查询多行数据
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="param">sql参数</param>
            /// <returns>一个表</returns>
            public static DataTable ExecuteTable(string sql, params SqlParameter[] param)
            {
                DataTable dt = new DataTable();
                using (SqlDataAdapter sda = new SqlDataAdapter(sql, str))
                {
    
                    if (param != null)
                    {
                        sda.SelectCommand.Parameters.AddRange(param);
                    }
                    sda.Fill(dt);
                }
                return dt;
            }
        
        }
    }

  • 相关阅读:
    JUC回顾之-可重入的互斥锁ReentrantLock
    java基础知识回顾之java Thread类学习(十二)-- 线程中断
    mknod命令
    一个公益水塘引发的纠纷
    对cgic的理解——name选项
    linux的fwrite()使用方法,当前时间写入文本的程序
    /etc/resolv.conf文件详解
    关于函数strtok和strtok_r的使用要点和实现原理
    shell视频
    进程一些命令pstree,ps,pstack,top
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/6416700.html
Copyright © 2011-2022 走看看