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

namespace Dev.DbTools
{
public class ADOTools
{
//操作数据库的API
//public static readonly string CONNECTION_STR = ConfigurationManager.ConnectionStrings["TestStr"].ToString();
public static string CONNECTION_STR = null;

public static bool SetConnection(string strconn)
{
try
{
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
if (conn.State == ConnectionState.Open)
{

return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
throw ex;
}

}

/// <summary>
/// 执行SQL操作,ExcuteNoQuery
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>影响行数</returns>
public static int ExcuteNoQuery(string connectionStr, string strSql)
{
int flag = 0;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
flag = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
}

/// <summary>
/// 执行SQL操作,ExecuteScalar
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>返回唯一值</returns>
public static object ExecuteScalar(string connectionStr, string strSql)
{
object flag = 0;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
flag = cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
}

/// <summary>
/// Reader查询
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>影响行数</returns>
public static SqlDataReader ExcuteReader(string connectionStr, string strSql)
{
SqlConnection conn = new SqlConnection(connectionStr);
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}

/// <summary>
/// DataTable查询
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <returns>返回DataTable数据源</returns>
public static DataTable ExcuteDataTable(string connectionStr, string strSql)
{
DataTable dt = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
try
{
conn = new SqlConnection(connectionStr);
cmd = new SqlCommand(strSql, conn);
adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
}
catch (Exception ex)
{
dt = null;
throw ex;
}
finally
{
adapter.Dispose();
cmd.Dispose();
conn.Dispose();
}
return dt;
}

/// <summary>
/// DataTable参数化查询
/// </summary>
/// <param name="connectionStr">数据库链接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="CommandType">存储过程或者SQL</param>
/// <returns>返回DataTable数据源</returns>
public static DataTable ExcuteDataTable(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
DataTable dt = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
try
{
conn = new SqlConnection(connectionStr);
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
}
catch (Exception ex)
{
dt = null;
throw ex;
}
finally
{
adapter.Dispose();
cmd.Dispose();
conn.Dispose();
}
return dt;
}

/// <summary>
/// 参数化执行SQL
/// </summary>
/// <param name="connectionStr">连接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="cd">存储过程或者SQL</param>
/// <param name="sp">参数数组</param>
/// <returns>影响的行数</returns>
public static int ExcuteNoQuery(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
int flag = 0;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
flag = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
}

/// <summary>
/// 参数化执行SQL,返回插入后自增ID
/// </summary>
/// <param name="connectionStr">连接字符串</param>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="cd">存储过程或者SQL</param>
/// <param name="sp">参数数组</param>
/// <returns>影响的行数</returns>
public static int ExcuteNoQueryByReturnID(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
int flag = 0;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection(connectionStr);
conn.Open();
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
cmd.ExecuteNonQuery();
flag = Convert.ToInt32(sp[1].Value);
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
conn.Dispose();
}
return flag;
}

/// <summary>
/// 执行SQL事务,ExcuteNoQuery
/// </summary>
/// <param name="transaction">事务对象</param>
/// <param name="strSql">执行脚本</param>
/// <returns></returns>
public static int ExcuteNoQuery(SqlTransaction transaction,string strSql)
{
int flag = 0;
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(strSql, transaction.Connection, transaction);
flag = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
}
return flag;
}

/// <summary>
/// 执行SQL事务,ExecuteScalar
/// </summary>
/// <param name="transaction">事务对象</param>
/// <param name="strSql">执行脚本</param>
/// <returns></returns>
public static object ExecuteScalar(SqlTransaction transaction, string strSql)
{
object flag = 0;
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(strSql, transaction.Connection, transaction);
flag = cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
}
return flag;
}

public static void BulkInsert(DataTable dt, string tableName)
{
SqlConnection conn = new SqlConnection(CONNECTION_STR);
conn.Open();

using (SqlBulkCopy bulk = new SqlBulkCopy(conn))
{
bulk.BatchSize = 2000000;
bulk.BulkCopyTimeout = 500000000;
bulk.DestinationTableName = tableName;

foreach (DataColumn dc in dt.Columns)
{
bulk.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);
}

bulk.WriteToServer(dt);
}
}

public static DataSet ExcuteDataSet(string connectionStr, string strSql, CommandType cd, SqlParameter[] sp)
{
DataSet ds = null;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter adapter = null;
try
{
conn = new SqlConnection(connectionStr);
cmd = new SqlCommand(strSql, conn);
cmd.CommandType = cd;
cmd.Parameters.AddRange(sp);
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
}
catch (Exception ex)
{
ds = null;
throw ex;
}
finally
{
adapter.Dispose();
cmd.Dispose();
conn.Dispose();
}
return ds;
}
}
}

ADO工具类的更多相关文章

  1. 另一个ado工具类

    using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using Sy ...

  2. ADO.NET复习总结(5)--工具类SqlHelper 实现登录

    工具类SqlHelper 即:完成常用数据库操作的代码封装 一.基础知识1.每次进行操作时,不变的代码: (1)连接字符串:(2)往集合存值:(3)创建连接对象.命令对象:(4)打开连接:(5)执行命 ...

  3. 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil

    基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...

  4. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

  5. 【Unity】工具类系列教程—— 代码自动化生成!

    转载自:https://zhuanlan.zhihu.com/p/30716595?utm_medium=social&utm_source=qq [为什么要做自动化工具] 工具类的创建是为了 ...

  6. 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式

    适用于app.config与web.config的ConfigUtil读写工具类   之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一 ...

  7. Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    Go/Python/Erlang编程语言对比分析及示例   本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...

  8. C#中SQLite的使用及工具类

    目录 SQLite简介 存储类 亲和类型 引用System.Data.SQLite.dll 软件包分类 使用本机库预加载 常用部署包 工具类 参考资料 SQLite简介 SQLite是一款轻型的数据库 ...

  9. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

随机推荐

  1. JDK动态代理(1)-----------new 对象的方式

    //case 1: 直接newHelloWorldImpl helloWorldImpl = new HelloWorldImpl(); //case 2: 反射拿到类之后,通过newInstance ...

  2. RestFul风格API(Swagger)--从零开始Swagger

    引言:随着技术的革新,现在的系统基本上都是前后端分离,并且在各自的道路上越走越远,而前后端之间通信或者联系的桥梁就是API,而这里基于RESTful风格的API框架就来了!欲知后事如何,客官别急,往下 ...

  3. Maven报错Archive for required library:某.jar' in project '项目名'

    Maven报错Archive for required library:某.jar' in project '项目名'cannot be read or is not a valid ZIP file ...

  4. 2018年计划小目标(9月)PMP

    从6.23结束敏捷的系统贯穿学习考试,6.24开始做传统项目管理,系统学习计划,打包报考,(╥╯^╰╥):ACP+实战+PMP+软考,历时两个月 每天上下班路上3个小时,听录音,(报的远程班,倍速听了 ...

  5. 牛客网 Python 编程输入规范

    import sys try: while True: line = sys.stdin.readline().strip() if line == '': break lines = line.sp ...

  6. NYOJ-16-矩形嵌套 记忆化搜索

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. Python-Django下载与基本命令

    1.下载Django: pip3 install django 2.创建一个django project django-admin.py startproject mysite 当前目录下会生成mys ...

  8. img :src=“” url()

    <img :src="logoImg"> this.logoImg='/static/images/'+adminUser.Logo; v-bind:style=&qu ...

  9. 在k-means或kNN,我们是用欧氏距离来计算最近的邻居之间的距离。为什么不用曼哈顿距离?

    曼哈顿距离只计算水平或垂直距离,有维度的限制.另一方面,欧氏距离可用于任何空间的距离计算问题. 因为,数据点可以存在于任何空间,欧氏距离是更可行的选择.例如:想象一下国际象棋棋盘,象或车所 做的移动是 ...

  10. MySQL中有关NULL的计算

    mysql> select NULL=NULL; #判断两个NULL是否相等,结果不是1也不是0 +-----------+ | NULL=NULL | +-----------+ | NULL ...