ADO.NET工具类(三)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace AppUtility
{
public class ADOTools
{
//操作数据库的API
public static readonly string CONNECTION_STR = ConfigurationManager.ConnectionStrings["TestStr"].ToString(); /// <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 = ;
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 = ;
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 = ;
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 = ;
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[].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 = ;
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 = ;
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(strSql, transaction.Connection, transaction);
flag = cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
}
return flag;
}
}
}
ADO.NET工具类(三)的更多相关文章
- java工具类(三)之生成若干位随机数
java 生成若干位随机数的问题 在一次编程的过程中偶然碰到一个小问题,就是需要生成一个4位数的随机数,如果是一个不到4位大的数字,前面可以加0来显示.因为要求最后是一个4位的整数,不带小数点.当时就 ...
- JDK1.8 LocalDate 使用方式;LocalDate 封装Util,LocalDate工具类(三)
未完待续 ........ 前言: 大企鹅的日常分享,第三步,最近一直在想策略设计模式和工厂模式结合优化ifelse的写法,看了很多资料,终于写出了自己要写的东西,在这段时间里,也有求助小伙伴,但是, ...
- ADO.NET工具类(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- ADO.NET工具类(一)
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; usin ...
- 【转】java缩放图片、java裁剪图片代码工具类
一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...
- java中常用的工具类(三)
继续分享java中常用的一些工具类.前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库.后续的我会加上的!谢谢支持IT江湖 一.连接数据库的综合类 ...
- ADO.NET复习总结(5)--工具类SqlHelper 实现登录
工具类SqlHelper 即:完成常用数据库操作的代码封装 一.基础知识1.每次进行操作时,不变的代码: (1)连接字符串:(2)往集合存值:(3)创建连接对象.命令对象:(4)打开连接:(5)执行命 ...
- 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)
转至 (http://blog.csdn.net/yanzi1225627/article/details/17652643) 经常听人问Camera开发中,各种变形问题,今天有空就在此梳理总结下. ...
- 一、JDBC的概述 二、通过JDBC实现对数据的CRUD操作 三、封装JDBC访问数据的工具类 四、通过JDBC实现登陆和注册 五、防止SQL注入
一.JDBC的概述###<1>概念 JDBC:java database connection ,java数据库连接技术 是java内部提供的一套操作数据库的接口(面向接口编程),实现对数 ...
随机推荐
- Scripts may close only the windows that were opened by it
关闭当前窗体报以下js错误: Scripts may close only the windows that were opened by it (脚本只能关闭由它打开的窗口) 使用场景,在js中关闭 ...
- Materialized View模式
Materialized-View模式是在要求数据格式不利于查询操作的情况下,根据多个数据仓库的数据生成预生成的视图的一种模式.这种模式可以帮助支持高效的查询和数据提取,提高应用程序的性能. 问题 在 ...
- ASP.NET Core 与支付宝开发文档
一.目录 ASP.NET Core 2.0 使用支付宝PC网站支付 ASP.NET Core 2.0 支付宝当面付之扫码支付 常见使用问题解答 已有多个公司数个项目用本组件并上线,稳定使用. 二.项目 ...
- ASP.NET Core 集成测试
集成测试 集成测试,也叫组装测试或联合测试.在单元测试的基础上,将所有模块按照设计要求(如根据结构图)组装成为子系统或系统,进行集成测试. 实践表明,一些模块虽然能够单独地工作,但并不能保证连接起来也 ...
- C#中存储数据的集合:数组、集合、泛型、字典
为什么把这4个东西放在一起来说,因为c#中的这4个对象都是用来存储数据的集合……. 首先咱们把这4个对象都声明并实例化一下: //数组 ]; //集合 ArrayList m_AList = new ...
- Angular下载文件
public Down(path: string) { return this.http.get(path, { responseType: "blob" }).subscribe ...
- python四:函数练习--小白博客
为什么要有函数?函数式编程定义一次,多出调用函数在一定程度上可以理解为变量函数的内存地址加上()就是调用函数本身也可以当做参数去传参 不用函数:组织结构不清晰代码的重复性 def test():#te ...
- A direct formulation for sparse PCA using semidefinite programming
目录 背景 Sparse eigenvectors(单个向量的稀疏化) 初始问题(low-rank的思想?) 等价问题 最小化\(\lambda\) 得到下列问题(易推) 再来一个等价问题 条件放松( ...
- poj2226 Muddy Fields 填充棒子(二分匹配)
参考博客:https://blog.csdn.net/liujc_/article/details/51287019 参考博客:https://blog.csdn.net/acdreamers/art ...
- pandas删除某一列的方法
方法一:直接del df['column-name'] 删除sub_grade_列, 输入del df['sub_grade_x'] 方法二:采用drop方法,有下面三种等价的表达式: 1. df= ...