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

万能的SQLHelper帮助类

时间:2015-08-06 21:49:04      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
    /// 数据库帮助类
    /// </summary>
    public class SQLHelper
    {
        private static string connStr = "server=.;database=***;uid=**;pwd=***";

        //定义对象
        private static SqlConnection conn = null;
        private static SqlCommand cmd = null;
        private static SqlDataAdapter sda = null;
        private static DataSet ds = null;

        /// <summary>
        /// 查询多行多列
        /// </summary>
        public static DataSet Search(string sql, params SqlParameter[] parameters)
        {
            try
            {
                using (conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    cmd = new SqlCommand(sql, conn);
                    foreach (SqlParameter p in parameters)
                    {
                        cmd.Parameters.Add(p);
                    }
                    sda = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    sda.Fill(ds);

                    return ds;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// 查询单行单列
        /// </summary>
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            try
            {
                using (conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    cmd = new SqlCommand(sql, conn);
                    foreach (SqlParameter p in parameters)
                    {
                        cmd.Parameters.Add(p);
                    }
                    object obj = cmd.ExecuteScalar();

                    return obj;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// 查询单行单列(增删改查)
        /// </summary>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            try
            {
                using (conn = new SqlConnection(connStr))
                {
                    conn.Open();
                    cmd = new SqlCommand(sql, conn);
                    foreach (SqlParameter p in parameters)
                    {
                        cmd.Parameters.Add(p);
                    }
                    int row = cmd.ExecuteNonQuery();

                    return row;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }

 

万能的SQLHelper帮助类

标签:

原文地址:http://www.cnblogs.com/xibianriluo/p/4709125.html

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