SQLHELPER

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQLHELPER相关的知识,希望对你有一定的参考价值。

 

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

namespace Jifang.Dal
{
    public class SqlHelper
    {
        private static readonly string connString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;

        #region GetTable
        /// <summary>
        /// 获取表格
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type">sql类型,sql语句或存储过程</param>
        /// <param name="pars">参数集</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlDataAdapter myda = new SqlDataAdapter(sql, conn))
                {
                    myda.SelectCommand.CommandType = type;  //如果执行的是sql语句,则type为字符串,如果执行的是存储过程,则type为存储过程
                    if (pars != null)
                    {
                        myda.SelectCommand.Parameters.AddRange(pars);
                    }
                    DataTable dt = new DataTable();
                    myda.Fill(dt);
                    return dt;
                }
            }
        }
        #endregion

        #region Insert
        public static int ExcuteNonquery(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region Update
        public static int ExcuteUpdate(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
    
        #endregion

        #region Delete
        public static int ExcuteDel(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region 返回结果集第一行第一列的值
        /// <summary>
        /// 返回结果集第一行第一列的值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="pars"></param>
        /// <returns></returns>
        public static Object ExcuteScalare(string sql, CommandType type, params SqlParameter[] pars)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = type;
                    if (pars != null)
                    {
                        cmd.Parameters.AddRange(pars);
                    }
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
        #endregion
    }
}

 

以上是关于SQLHELPER的主要内容,如果未能解决你的问题,请参考以下文章

C#实现较为实用的SQLhelper

SQLHelper助手

2016-11-2坚持学习Day17微软 推出的SQLHelper

单步执行SqlHelper.ExecuteNonQuery();弹出"没有可用于当前位置的源代码",请问是啥原因啊

复习SqlHelper

SqlHelper