码迷,mamicode.com
首页 > 数据库 > 详细

SqlHelper

时间:2014-06-18 14:01:42      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:class   blog   tar   com   string   os   

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,布布扣,bubuko.com

SqlHelper

标签:class   blog   tar   com   string   os   

原文地址:http://www.cnblogs.com/lierjie/p/3791863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!