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 ;
}
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);
}
}
}

C# ADO.NET 封装的增删改查的更多相关文章

  1. Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]

    [虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...

  2. 03 基于umi搭建React快速开发框架(封装列表增删改查)

    前言 大家在做业务系统的时候,很多地方都是列表增删改查,做这些功能占据了大家很长时间,如果我们有类似的业务,半个小时就能做出一套那是不是很爽呢. 这样我们就可以有更多的时间学习一些新的东西.我们这套框 ...

  3. node.js封装数据库增删改查

    数据库增删改查的封装 小编不容易 const sql = { insert: function (Collection, insertData) { return new Promise((resol ...

  4. JDBC连接Greenplum数据库,封装了增删改查

    要启动好gp服务,再尝试连接 192.168.94.135是主节点(master)的ip 驱动Jar包在官网获取 嫌麻烦,可以直接用我在网盘分享的Jar包,版本较老 链接:https://pan.ba ...

  5. ADO.NET ORM数据库增删改查封装(工具一)

    约束 public abstract class BaseModel { public int Id { get; set; } } 连接字符串 public static readonly stri ...

  6. ADO.NET基础(增删改查)

    ADO.NET是一种数据访问技术,就是将C#和MSSQL连接起来的一个纽带.可以通过ADO.NET将内存中的临时数据写入到数据库中,也可以将数据库中的数据提取到内存中供程序调用. ADO.NET是所有 ...

  7. ADO.NET五大对象增删改查使用

    string ConnStr = "Data Source=.;Initial Catalog=ADO_DEMO;Integrated Security=True"; #regio ...

  8. PHP历程(封装的增删改查方法)

    db.class.php   主要方法 <?php /** * 数据库配置信息 */ define('DB_HOST','127.0.0.1'); //服务器 define('DB_USER', ...

  9. 后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)

    using IBatisNet.Common.Exceptions; using IBatisNet.DataAccess; using IBatisNet.DataAccess.DaoSession ...

随机推荐

  1. Fire Game 双向bfs

    Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...

  2. JSONObject JSONArray json字符串 HashMap ArryList 在java开发中用到的数据结构

    1.JSONObject  长成这样的:   { "key1":value1, "key2":value2, "key3":value3} ...

  3. Git Authoritative Guide 学习

    一.git命令1.git add -u : 将工作区中所有改动的文件添加到暂存区(修改.删除),但是不提交未被git跟踪的文件 -i : 可以进入交互界面选择性提交 -A : 相对于-u,它还提交新建 ...

  4. vulcanjs 核心架构概念

    基于包的架构 为了保证系统的灵活以及可扩展,vulcanjs 使用基于包的架构设计,每一个功能都是一个包,可以方便的添加,移除 扩展.而不是修改 vulcan 的设计哲学是进行系统扩展,而不是编辑修改 ...

  5. JUC集合之 ConcurrentSkipListSet

    ConcurrentSkipListSet介绍 ConcurrentSkipListSet是线程安全的有序的集合,适用于高并发的场景. ConcurrentSkipListSet和TreeSet,它们 ...

  6. mysql的一些 参数查询

    1 查询 事务 超时时间: SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait_timeout'; (默认innodb引擎事务的超时时间) 2 查询事务隔离级别 ...

  7. win7 X64 使用VS2008 ->编译报错LINK : fatal error LNK1000: Internal error during Incr的解决

    编译报错LINK : fatal error LNK1000: Internal error during Incr的解决 Win7 旗舰版 Microsoft Visual Studio 2008 ...

  8. 关于Jquery 插件开发,写的很清楚了。。。

    转自:http://blog.jobbole.com/30550/ 本文由 伯乐在线 - 戴嘉华 翻译.未经许可,禁止转载!英文出处:Extraordinarythoughts.欢迎加入翻译小组. 如 ...

  9. 关于seo优化的核心思想

    简单说下,针对网页检索结果进行评估,主要是围绕精确率和召回率进行,具体如下:1.相关性:query与结果说的是不是一回事2.需求强度:抓住主要需求3.丰富程度:详细全面4.有效性:能否真正满足5.时效 ...

  10. make_heap()等函数的用法

    1.make_heap() make_heap()用于把一个可迭代容器变成一个堆,默认是大顶堆. 它有三个参数.第一个参数是指向开始元素的迭代器,第二个参数是指向最末尾元素的迭代器,第三个参数是les ...