using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Elephant.OPS.Application.Service.SqlHelper
{
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 ;
}
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 - ) * 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 - ) * 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);
}
}
}

Ado.net简单快捷帮助类的更多相关文章

  1. .net下简单快捷的数值高低位切换

    .net下简单快捷的数值高低位切换 做网络通讯中数值传输是很普遍的事情,但数值的存储在不平台和硬件上存储方式都不一样,主要有两大类分别是高位和低位存储:而.net平台下是低位存储,通过.net提供的函 ...

  2. 简单快捷地测试 JPush API

    随着 JPush API v3版本的推出,加上之前开放的 Report API,JPush API 逐渐切换为比较好的符合 REST API 的规范,从而也很容易地使用一般的 HTTP/REST 工具 ...

  3. VC++ 一个简单的Log类

    在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...

  4. 简单快捷好用的vim配置和终端配置推荐

    vim 配置实用spf13-vim,安装方便简单快捷,极力推荐. 另外oh-my-zsh 终端配置很好,与之搭配使用效果更佳. 安装都很简单,一个脚本搞定, 都是在gitHub上开源的,自行搜索,这里 ...

  5. ADO.NET基础巩固-----连接类和非连接类

          最近的一段时间自己的状态还是不错的,早上,跑步,上自习看书,下午宿舍里面编程实战,晚上要么练习代码,要么去打球(在不打就没机会了),生活还是挺丰富的. 关于C#的基础回顾就先到前面哪里,这 ...

  6. C++ 最简单的日志类

    最近搞一个 C++ 项目的二次开发,没玩过 C++,可谓步履维艰.自己写个简单的日志类都被各种坑折磨.终于搞定了. 参考了这篇博客,并且进一步简化:https://www.cnblogs.com/Ds ...

  7. python+selenium之自定义封装一个简单的Log类

    python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...

  8. C++定义一个简单的Computer类

    /*定义一个简单的Computer类 有数据成员芯片(cpu).内存(ram).光驱(cdrom)等等, 有两个公有成员函数run.stop.cpu为CPU类的一个对象, ram为RAM类的一个对象, ...

  9. Python之自定义封装一个简单的Log类

    参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...

随机推荐

  1. PHP使用redis防止大并发下二次写入(如如何防止重复下订单)

    php调用redis进去读写操作,大并发下会出现:读取key1,没有内容则写入内容,但是大并发下会出现同时多个php进程写入的情况,这个时候需要加一个锁,即获取锁的php进程有权限写. $lock_k ...

  2. adb push和adb install区别

    一般的,Android 应用程序有两种安装方法: 1. 将应用程序的apk文件push到手机中,用如下命令: adb push xxxx.apk /system/app. 2. 用adb instal ...

  3. http://www.cnblogs.com/wuyunfei/p/4277226.html

    http://www.cnblogs.com/wuyunfei/p/4277226.html

  4. 利用python操作redis-cluster

    In [8]: def redis_cluster(): ...: redis_nodes = [{'host':'192.168.100.60','port':7000}, ...: {'host' ...

  5. python字典dict的增、删、改、查操作

    ## python字典dict的增.删.改.查操作dict = {'age': 18, 'name': 'jin', 'sex': 'male', }#增# dict['heigh'] = 185 # ...

  6. SPSS-因子分析

    因子分析 有可能用较少的综合指标分析存在于各变量中的各类信息,而各综合指标之间彼此是不相关的,代表各类信息的综合指标称为因子.定义:因子分析就是用少数几个因子来描述许多指标或因素之间的联系,以较少几个 ...

  7. kotlin集合操作

    1.1 总数操作 方法作用: any--判断集合中是否有满足条件 的元素: all--判断集合中的元素是否都满足条件: count--查询集合中满足条件的元素个数: fold--在给定初始值的基础上, ...

  8. php导出excel不知道列数 php26进制函数

    function num2Letter($num) { $num = intval($num); if ($num <= 0) return false; $letterArr = array( ...

  9. linux系统上项目部署

    步骤:(特别注意:虚拟机安装的一般是32位的操作系统,jdk也必须使用32位的)查看虚拟机版本:sudo uname --m i686 //表示是32位 x86_64 // 表示是64位 查看是否已经 ...

  10. 手工命令行 搭建 hadoop 和 spark 环境

    环境准备:3台CentOS7,64位,Hadoop2.7需要64位Linux 192.168.20.161  192.168.20.162  192.168.20.163 三台机器分别叫host01. ...