Ado.net简单快捷帮助类
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简单快捷帮助类的更多相关文章
- .net下简单快捷的数值高低位切换
.net下简单快捷的数值高低位切换 做网络通讯中数值传输是很普遍的事情,但数值的存储在不平台和硬件上存储方式都不一样,主要有两大类分别是高位和低位存储:而.net平台下是低位存储,通过.net提供的函 ...
- 简单快捷地测试 JPush API
随着 JPush API v3版本的推出,加上之前开放的 Report API,JPush API 逐渐切换为比较好的符合 REST API 的规范,从而也很容易地使用一般的 HTTP/REST 工具 ...
- VC++ 一个简单的Log类
在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...
- 简单快捷好用的vim配置和终端配置推荐
vim 配置实用spf13-vim,安装方便简单快捷,极力推荐. 另外oh-my-zsh 终端配置很好,与之搭配使用效果更佳. 安装都很简单,一个脚本搞定, 都是在gitHub上开源的,自行搜索,这里 ...
- ADO.NET基础巩固-----连接类和非连接类
最近的一段时间自己的状态还是不错的,早上,跑步,上自习看书,下午宿舍里面编程实战,晚上要么练习代码,要么去打球(在不打就没机会了),生活还是挺丰富的. 关于C#的基础回顾就先到前面哪里,这 ...
- C++ 最简单的日志类
最近搞一个 C++ 项目的二次开发,没玩过 C++,可谓步履维艰.自己写个简单的日志类都被各种坑折磨.终于搞定了. 参考了这篇博客,并且进一步简化:https://www.cnblogs.com/Ds ...
- python+selenium之自定义封装一个简单的Log类
python+selenium之自定义封装一个简单的Log类 一. 问题分析: 我们需要封装一个简单的日志类,主要有以下内容: 1. 生成的日志文件格式是 年月日时分秒.log 2. 生成的xxx.l ...
- C++定义一个简单的Computer类
/*定义一个简单的Computer类 有数据成员芯片(cpu).内存(ram).光驱(cdrom)等等, 有两个公有成员函数run.stop.cpu为CPU类的一个对象, ram为RAM类的一个对象, ...
- Python之自定义封装一个简单的Log类
参考:http://www.jb51.net/article/42626.htm 参考:http://blog.csdn.net/u011541946/article/details/70198676 ...
随机推荐
- 灵活控制 Hibernate 的日志或 SQL 输出,以便于诊断
我们在使用 Hibernate 时一般只会关注是否显示生成的 SQL 语句,不过有些时候还不够.默认时 Hibernate 执行的 SQL 语句是打印在控制台上的,它也可以配置为输出给 Log4J 或 ...
- [jQ/PHP]再谈使用JS数组储值的运用(提交PHP处理)
--------------------------------------------------------------------------------------------------- ...
- ORACLE数据库 常用命令和Sql常用语句
ORACLE 账号相关 如何获取表及权限 1.COPY表空间backup scottexp登录管理员账号system2.创建用户 create user han identified(认证) by m ...
- 微信小程序-获取当前城市位置及再次授权地理位置
微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...
- 前端路由两种模式:hash、history
随着 ajax 的使用越来越广泛,前端的页面逻辑开始变得越来越复杂,特别是spa的兴起,前端路由系统随之开始流行. 从用户的角度看,前端路由主要实现了两个功能(使用ajax更新页面状态的情况下): 记 ...
- Java的学习路线图
在网上看到一个关于Java的学习路线图,个人感觉很详细.https://blog.csdn.net/s1547823103/article/details/79768938
- ubuntu下没有ping命令
root@node2:/# apt-get install inetutils-ping
- 微信小程序之富文本解析
亲身体验 wxparse 是个坑,弃之不用 微信小程序的 <rich-text>标签挺好用的 用法如下: 1.wxml页面 <rich-text nodes="{{node ...
- java.lang.IllegalAccessError: tried to access method
java.lang.IllegalAccessException: access to method denied 06-23 16:12:39.128 1253-1253/com.hbjyjt.oa ...
- layout_weight 全解析
[layout_weight 全解析] 参考:http://www.cnblogs.com/net168/p/4227144.html