码迷,mamicode.com
首页 > Windows程序 > 详细

C# ADO.NET 封装的增删改查

时间:2017-02-25 16:01:26      阅读:526      评论:0      收藏:0      [点我收藏+]

标签:sys   sql语句   开发   query   finally   tab   执行方法   set   更新   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace MCO.ADO.NET
{
    class SqlServerHelper
    {
        #region 该类的核心代码
        /// <summary>
        /// 私有化构造器(单例模式开发数据库查询工具类)
        /// </summary>
        private SqlServerHelper() { }

        /// <summary>
        /// 程序执行前实例化一个数据库帮助类
        /// </summary>
        private static SqlServerHelper sqlServer = new SqlServerHelper();

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private string connection;

        /// <summary>
        /// 数据库命令执行方法(SQL语句)
        /// </summary>
        private int Command(string sql)
        {
            SqlConnection conn = new SqlConnection(connection);
            try
            {
                SqlCommand command = new SqlCommand(sql, conn);
                conn.Open();
                return command.ExecuteNonQuery();
            }
            catch
            {
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }

        /// <summary>
        /// 查询(SQL语句)
        /// </summary>
        private DataTable GetList(string sql)
        {
            SqlConnection conn = new SqlConnection(connection);
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                DataTable data = new DataTable();
                adapter.Fill(data);
                return data;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        /// <summary>
        /// 创建该单例类的方法(数据库连接字符串)
        /// </summary>
        public static SqlServerHelper GetSqlServer(string connection)
        {
            sqlServer.connection = connection;
            return sqlServer;
        }

        /// <summary>
        /// 完全查询(表名, 字段) //表名和字段最好加上中括号[Id]
        /// </summary>
        public DataTable GetList(string tableName, string field)
        {
            string sql = string.Format("select {0} from {1}", field, tableName);
            return GetList(sql);
        }

        /// <summary>
        /// 条件查询(表名, 字段, 条件)
        /// </summary>
        public DataTable GetList(string tableName, string field, string where)
        {
            string sql = string.Format("select {0} form {1} where {2}", field, tableName, where);
            return GetList(sql);
        }

        /// <summary>
        /// 分页查询(表名, 字段, 条件, 主键, 页码, 条数)
        /// </summary>
        public DataTable GetList(string tableName, string field, string where, string idField, int page, int size)
        {
            string sql = string.Format("select top {0} {1} from {2} where {3} not in (select top {4} {5} from {6} where {7}) and ({8})",
                                            size, field, tableName, idField, (page - 1) * size, idField, tableName, where, where
                                      );
            return GetList(sql);
        }

        /// <summary>
        /// 排序查询(表名, 字段, 条件, 排序)
        /// </summary>
        public DataTable GetList(string tableName, string field, string where, string order)
        {
            string sql = string.Format("select {0} form {1} where {2} order by {3}", field, tableName, where, order);
            return GetList(sql);
        }

        /// <summary>
        /// 分页排序查询(表名, 字段, 条件, 主键, 页码, 条数, 排序)
        /// </summary>
        public DataTable GetList(string tableName, string field, string where, string idField, int page, int size, string order)
        {
            string sql = string.Format("select top {0} {1} from {2} where {3} not in (select top {4} {5} from {6} where {7}) and ({8}) order by {9}",
                                            size, field, tableName, idField, (page - 1) * size, idField, tableName, where, where, order
                                      );
            return GetList(sql);
        }

        /// <summary>
        /// 条件删除(表名, 条件) //返回受影响的行数, 0 表示失败
        /// </summary>
        public int Delete(string tableName, string where)
        {
            string sql = string.Format("delete from {0} where {1}", tableName, where);
            return Command(sql);
        }

        /// <summary>
        /// 条件修改(表名, 更新的数据, 条件)
        /// </summary>
        public int Update(string tableName, string updateData, string where)
        {
            string sql = string.Format("update {0} set {1} where {2}", tableName, updateData, where);
            return Command(sql);
        }
    }
}

 

C# ADO.NET 封装的增删改查

标签:sys   sql语句   开发   query   finally   tab   执行方法   set   更新   

原文地址:http://www.cnblogs.com/lovling/p/6441855.html

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