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内部提供的一套操作数据库的接口(面向接口编程),实现对数 ...
随机推荐
- Java 中常见的数据结构
1.数据结构有什么作用? 当使用 Java 里面的容器类时,你有没有想过,怎么 ArrayList 就像一个无限扩充的数组,也好像链表之类的.很好使用,这就是数据结构的用处,只不过你在不知不觉中使用了 ...
- DataX
#!/bin/bash[ ! -d /opop ] && mkdir /opopcd /opopwget http://192.168.1.129/package/DataX/{jdk ...
- Shiro核心概述
0.写在前面的话 最近在考虑权限相关的东西,于是就找到了Shiro,开涛老师的Shiro教程博客(<跟我学Shiro>)写得实在很好还带所有源码,所以我也就没有自己再总结各个阶段的笔记,只 ...
- Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
1. Hystrix Dashboard (断路器:hystrix 仪表盘) Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...
- MySQL常用SQL语句/函数/存储过程
一句话总结 SELECT count(*) FROM user WHERE id>0 GROUP BY name HAVING count(*)>1 ORDER BY count(*)DE ...
- 【出错记录】Tomcat非root用户启动无法拥有权限读写文件
简单记录下,如有必要,将深入补充: 一.非root用户运行Tomcat及原因 由于项目中,为了安全需要,Tomcat将禁止以root形式启动,原因很简单,举个例子,一旦有人恶意将jsp文件透过某个别的 ...
- 吉特日化MES-日化生产称料基本步骤
在日化行业称料是一个非常重要的环节,整个生产过程中称料所占据的时间也比较长,特别是遇到对料体精度高,量大的情况下称料都比较困难,汇总一下人工称料的基本过程: (1) 称量任务准备:根据生产工单或者生产 ...
- 【教程】switch上的Human Fall Flat如何设置本地双人?
1. 保证两个手柄已插入主机上 2. 进入游戏至游戏开始界面 3. 将主机插入拓展坞,等待电视显示 4. 稍等数秒,电视上会提示使用手柄方式 5. 此时按照多人游戏的手柄操作方法即可
- Random()种子数
Random rand =new Random(25); int i; i=rand.nextInt(100); 初始化时25并没有起直接作用,rand.nextInt(100);中的100是随机数的 ...
- 不能再忽视了!宝宝不肯吃粥的N个原因,你避免了几个?
辅食不懂怎么添加? 宝宝吃饭爱挑食? 营养均衡和多样化的辅食 在这里你都能找到 宝宝辅食微课堂 不能再忽视了!宝宝不肯吃粥的N个原因,你避免了几个? 2017-10-09 09:35 辅食不懂怎么添加 ...