C# ADO.NET 封装的增删改查
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 封装的增删改查的更多相关文章
- Ado.net[登录,增删改查,Get传值,全选,不选,批量删除,批量更新]
[虽然说,开发的时候,我们可以使用各种框架,ado.net作为底层的东西,作为一个合格的程序员,在出问题的时候我们还是要知道如何调试] 一.增删改查 cmd.ExecuteReader();执行查询, ...
- 03 基于umi搭建React快速开发框架(封装列表增删改查)
前言 大家在做业务系统的时候,很多地方都是列表增删改查,做这些功能占据了大家很长时间,如果我们有类似的业务,半个小时就能做出一套那是不是很爽呢. 这样我们就可以有更多的时间学习一些新的东西.我们这套框 ...
- node.js封装数据库增删改查
数据库增删改查的封装 小编不容易 const sql = { insert: function (Collection, insertData) { return new Promise((resol ...
- JDBC连接Greenplum数据库,封装了增删改查
要启动好gp服务,再尝试连接 192.168.94.135是主节点(master)的ip 驱动Jar包在官网获取 嫌麻烦,可以直接用我在网盘分享的Jar包,版本较老 链接:https://pan.ba ...
- ADO.NET ORM数据库增删改查封装(工具一)
约束 public abstract class BaseModel { public int Id { get; set; } } 连接字符串 public static readonly stri ...
- ADO.NET基础(增删改查)
ADO.NET是一种数据访问技术,就是将C#和MSSQL连接起来的一个纽带.可以通过ADO.NET将内存中的临时数据写入到数据库中,也可以将数据库中的数据提取到内存中供程序调用. ADO.NET是所有 ...
- ADO.NET五大对象增删改查使用
string ConnStr = "Data Source=.;Initial Catalog=ADO_DEMO;Integrated Security=True"; #regio ...
- PHP历程(封装的增删改查方法)
db.class.php 主要方法 <?php /** * 数据库配置信息 */ define('DB_HOST','127.0.0.1'); //服务器 define('DB_USER', ...
- 后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)
using IBatisNet.Common.Exceptions; using IBatisNet.DataAccess; using IBatisNet.DataAccess.DaoSession ...
随机推荐
- 《DSP using MATLAB》Problem 3.8
2018年元旦,他乡加班中,外面尽是些放炮的,别人的繁华与我无关. 代码: %% ----------------------------------------------------------- ...
- hadoop之 map个数控制
hadooop提供了一个设置map个数的参数mapred.map.tasks,我们可以通过这个参数来控制map的个数.但是通过这种方式设置map的个数,并不是每次都有效的.原因是mapred.map. ...
- yii 获取当前模块名、控制器名 、动作名
yii1 1. 获取控制器名 在控制器中获取控制器名: $name = $this->getId(); 在视图中获取控制器名: $name = Yii::app()->controller ...
- java初始化块执行顺序
java中初始化块的执行顺序在构造器之前,多个初始化块之间定义在前的先执行.如下: public class InitialBlockTest { // The first one { System. ...
- Linux中的其他命令
1. 修改文件的所有者 chown 用户名 文件名 2. 修改文件所属组 chgrp 组名 文件名 3. 创建用户,创建组,将用户添加到组中等 修改film文件夹的所有者和所属组 修改film文 ...
- 重置SQL Server sa密码
查询分析器,连接时,身份验证使用"使用windows身份验证" 然后,执行: EXEC sp_password NULL, '新密码', 'Sa'
- JAVA Date类与Calendar类【转】
Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...
- yii framework config 可以被配置的项目
http://hi.baidu.com/lossless1009/item/990fdb33a52ffcf1e7bb7a4c <?php002 003 // 取消下行的注释,来定义一个路径别名0 ...
- java枚举使用详解,整理的很好,只能收下。。。
java枚举使用详解 在实际编程中,往往存在着这样的“数据集”,它们的数值在程序中是稳定的,而且“数据集”中的元素是有限的. 例如星期一到星期日七个数据元素组成了一周的“数据集”,春夏秋冬四个数据元素 ...
- c# 与 java 语法异同
Java and C# ComparisonThis is a quick reference guide to highlight some key syntactical differences ...