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

SqlHelper

时间:2018-02-10 14:59:00      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:sql语句   esc   ret   cti   int   range   manager   nbsp   语句   

抄至传智播客的教学视频,做个记录

 public static class SqlHelper
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
       // private static readonly string conrt = ConfigurationManager.ConnectionStrings [ "MySql" ].ConnectionString;
        public static string conrt { get; set; }

       

        /// <summary>
        /// 增删改返回整数,其它为-1
        /// </summary>
        /// <param name="sqltext">sql语句</param>
        /// <param name="cmdType">存储过程</param>
        /// <param name="pms">参数</param>
        /// <returns></returns>
        public static int ExecuteNoQuery(string sqltext,CommandType cmdType,params SqlParameter[] pms )
        {
            //建立sql连接
            using ( SqlConnection con=new SqlConnection (conrt) )
            {
                using ( SqlCommand cmd=new SqlCommand (sqltext,con) )
                {
                    cmd.CommandType = cmdType;
                    if(pms!=null)
                    {
                        cmd.Parameters.AddRange ( pms );
                    }
                    con.Open ( );
                    return cmd.ExecuteNonQuery ( );
                }
            }
        }

        /// <summary>
        /// 返回查找的数据
        /// </summary>
        /// <param name="sqltext">传入sql字符串</param>
        /// <param name="cmdType">传入的类型是存储过程还是普通字符串</param>
        /// <param name="pms">参数</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sqltext,CommandType cmdType,params SqlParameter[] pms)
        {
            //建立sql连接
            using ( SqlConnection con = new SqlConnection ( conrt ) )
            {
                using ( SqlCommand cmd = new SqlCommand ( sqltext , con ) )
                {
                    cmd.CommandType = cmdType;
                    if ( pms != null )
                    {
                        cmd.Parameters.AddRange ( pms );
                    }
                    con.Open ( );
                    return cmd.ExecuteScalar ( );
                }
            }
        }

        public static SqlDataReader ExecuteReader(string sqltext,CommandType cmdType,params SqlParameter[] pms)
        {
            SqlConnection con = new SqlConnection ( conrt );
            using ( SqlCommand cmd=new SqlCommand (sqltext,con) )
            {
                cmd.CommandType = cmdType;
                if(pms!=null)
                {
                    cmd.Parameters.AddRange ( pms );
                }
                try
                {
                    if ( con.State == ConnectionState.Closed )
                    {
                        con.Open ( );
                    }
                    return cmd.ExecuteReader ( CommandBehavior.CloseConnection );
                }
                catch
                {

                    con.Close ( );
                    con.Dispose ( );
                   throw;
                }
            }
        }

        public static DataTable ExecuteDataTable(string sqltext,CommandType cmdType,params SqlParameter[] pms)
        {
            DataTable table = new DataTable ( );
            using ( SqlDataAdapter adapter=new SqlDataAdapter (sqltext,conrt) )
            {
                adapter.SelectCommand.CommandType = cmdType;
                if ( pms != null )
                {
                    adapter.SelectCommand.Parameters.AddRange ( pms );
                }
                adapter.Fill ( table );
            }
            return table;
        }
    }

 

SqlHelper

标签:sql语句   esc   ret   cti   int   range   manager   nbsp   语句   

原文地址:https://www.cnblogs.com/rbzz/p/8438621.html

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