zoukankan      html  css  js  c++  java
  • SqlHelper.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace _001
    {
        public static class SqlHelper
        {
            //定义一个连接字符串,
            //readonly,只能在初始化和构造函数中赋值
            private static readonly string conStr=ConfigurationManager.ConnectionStrings["mssqlserver"].ConnectionString;

            //1.执行增insert 删delete 改update的方法;
            public static int ExecuteNonQuery(string sql,params SqlParameter[] pms)
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        conn.Open();
                        return cmd.ExecuteNonQuery();
                    }
                }
            }

            //2.执行查询,返回单个值的方法
            //ExecuteScalar()
            public static object ExecuteScalar(string sql, params SqlParameter[] pms)
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        conn.Open();
                        return cmd.ExecuteScalar();
                    }
                }
            }

            //3.执行查询,返回多行,多列的方法
            //ExecuteReader
            public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
            {
                SqlConnection conn = new SqlConnection(conStr);
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        if (pms != null)
                        {
                            cmd.Parameters.AddRange(pms);
                        }
                        try
                        {
                            conn.Open();
                            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        }
                        catch
                        {
                            conn.Close();
                            conn.Dispose();
                            throw;
                        }
                    }
            }
        }
    }

  • 相关阅读:
    linux查看端口被占用等常用命令
    python批量修改文件内容及文件编码方式的处理
    为什么在Python里推荐使用多进程而不是多线程
    python小练习之读取文件写入excel
    python 小练习之生成手机号码
    python 小练习之删除文件夹下的所有文件,包括子文件夹中的文件
    python中 字符 字典 列表之间的转换
    统计表中 重复出现 XX次以上的数据
    java数据类型
    字符编码和python使用encode,decode转换utf-8, gbk, gb2312
  • 原文地址:https://www.cnblogs.com/HarryChis/p/10368742.html
Copyright © 2011-2022 走看看