IT开发知识库 - 立志做成最实用的开发手册!
数据库 SQL MySQL Oracle技术 Oracle管理 Oracle开发 Oracle面试 Oracle Exception Sql Server Informix Sybase DB2 Access VFP 数据仓库 高性能数据库开发 其他数据库
当前位置: 开发知识库 > 数据库 > SQL > 容易的SqlHelper

容易的SqlHelper

时间:2017-04-18来源:未知 作者:admin点击:
简单的SqlHelper public class SqlHelper { public static string connstr= ConfigurationManager.ConnectionStrings[ " Connstr " ].ConnectionString; /// summary /// 执行增删改 /// /summary /// param name="sql"/param /// param name="paramet
简单的SqlHelper
public class SqlHelper
    {
        public static string connstr= ConfigurationManager.ConnectionStrings["Connstr"].ConnectionString;

        /// <summary>
        /// 执行增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 返回之后一行一列的数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }

        /// <summary>
        /// 返回查询的表
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter ada = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    ada.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }

        /// <summary>
        /// 从数据库查询的数据,如果是DBNull就转换成null
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object FromDBNull(object value)
        {
            if (value == DBNull.Value)
            {
                return null;
            }
            else
            {
                return value;
            }
        }

        /// <summary>
        /// 把数据插入数据库中,如果是null就转换成DBNull
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object ToDBNull(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }
View Code

这是简单的sqlHelper,保存一下。

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
相关内容
推荐内容