SqlHelper

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


namespace LZJ_Common
{
    public class SqlHelper
    {
        private static readonly string ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

        /// <summary>
        /// 增删改方法
        /// </summary>
        /// <param name="sqlstr"></param>
        /// <param name="psm"></param>
        public int ExeCuteNonQuery(string sqlstr, params SqlParameter[] psm)
        {
            using (SqlConnection mycon = new SqlConnection(ConStr))
            {
                using (SqlCommand mycom = new SqlCommand(sqlstr, mycon))
                {
                    if (psm != null)
                    {
                        mycom.Parameters.AddRange(psm);
                    }
                    mycon.Open();
                    return mycom.ExecuteNonQuery();
                }
            }
        }

        public object ExeCuteSclar(string sqlstr, params SqlParameter[] psm)
        {
            using (SqlConnection mycon = new SqlConnection(ConStr))
            {
                using (SqlCommand mycom = new SqlCommand(sqlstr, mycon))
                {
                    if (psm != null)
                    {
                        mycom.Parameters.AddRange(psm);
                    }
                    mycon.Open();
                    return mycom.ExecuteScalar();
                }
            }
        }

        public SqlDataReader ExeCuteReader(string sqlstr, params SqlParameter[] psm)
        {
            SqlConnection mycon = new SqlConnection(ConStr);
            using (SqlCommand mycom = new SqlCommand(sqlstr, mycon))
            {
                if (psm != null)
                {
                    mycom.Parameters.AddRange(psm);
                }

                try
                {
                    mycon.Open();
                    return mycom.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch (Exception ex)
                {
                    mycon.Dispose();
                    throw ex;
                }
            }
        }

        public DataTable ExeCuteDataTable(string sqlstr, params SqlParameter[] psm)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter myda = new SqlDataAdapter(sqlstr, ConStr))
            {
                if (psm != null)
                {
                    myda.SelectCommand.Parameters.AddRange(psm);
                }
                myda.Fill(dt);
                return dt;
            }
        }



    }
}

 

SqlHelper,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。