CS通用项目系统搭建——三层架构第二天 (补一篇完整的SqlHelper)
#region ExecuteNonQuery(如果是增,删,修)
/// <summary>
/// 执行sql命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{ SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
int val = cmd.ExecuteNonQuery(); return val;
}
} /// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{ using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, conn, spName, parameterValues);
int val = cmd.ExecuteNonQuery(); return val;
}
}
#endregion #region ExecuteReader(如果只是读数据)
/// <summary>
/// 执行sql命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>SqlDataReader 对象</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{ SqlConnection conn = new SqlConnection(connectionString);
try
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return rdr;
}
catch
{
conn.Close();
throw;
}
} /// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>受影响的行数</returns>
public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
{
SqlConnection conn = new SqlConnection(connectionString);
try
{
SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, conn, spName, parameterValues);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return rdr;
}
catch
{
conn.Close();
throw;
} }
#endregion #region ExecuteDataset(如果查询一个数据集) /// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>DataSet对象</returns>
public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, conn, spName, parameterValues); using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet(); da.Fill(ds); return ds;
}
}
} /// <summary>
/// 执行Sql 命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>DataSet 对象</returns>
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{ SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet(); da.Fill(ds); return ds;
}
}
} #endregion #region ExecuteScalar (如果你只查询一个值)
/// <summary>
/// 执行Sql 命令
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">sql语句/参数化sql语句/存储过程名</param>
/// <param name="commandParameters">参数</param>
/// <returns>执行结果对象</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, commandType, conn, commandText, commandParameters);
object val = cmd.ExecuteScalar(); return val;
}
} /// <summary>
/// 执行Sql Server存储过程
/// 注意:不能执行有out 参数的存储过程
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="spName">存储过程名</param>
/// <param name="parameterValues">对象参数</param>
/// <returns>执行结果对象</returns>
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
{
SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, spName, parameterValues);
object val = cmd.ExecuteScalar(); return val;
}
} #endregion #region Private Method
/// <summary>
/// 设置一个等待执行的SqlCommand对象
/// </summary>
/// <param name="cmd">SqlCommand 对象,不允许空对象</param>
/// <param name="conn">SqlConnection 对象,不允许空对象</param>
/// <param name="commandText">Sql 语句</param>
/// <param name="cmdParms">SqlParameters 对象,允许为空对象</param>
private static void PrepareCommand(SqlCommand cmd, CommandType commandType, SqlConnection conn, string commandText, SqlParameter[] cmdParms)
{
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open(); //设置SqlCommand对象
cmd.Connection = conn;
cmd.CommandText = commandText;
cmd.CommandType = commandType; if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
} /// <summary>
/// 设置一个等待执行存储过程的SqlCommand对象
/// </summary>
/// <param name="cmd">SqlCommand 对象,不允许空对象</param>
/// <param name="conn">SqlConnection 对象,不允许空对象</param>
/// <param name="spName">Sql 语句</param>
/// <param name="parameterValues">不定个数的存储过程参数,允许为空</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, string spName, params object[] parameterValues)
{
//打开连接
if (conn.State != ConnectionState.Open)
conn.Open(); //设置SqlCommand对象
cmd.Connection = conn;
cmd.CommandText = spName;
cmd.CommandType = CommandType.StoredProcedure; //获取存储过程的参数
SqlCommandBuilder.DeriveParameters(cmd); //移除Return_Value 参数
cmd.Parameters.RemoveAt(); //设置参数值
if (parameterValues != null)
{
for (int i = ; i < cmd.Parameters.Count; i++)
{
cmd.Parameters[i].Value = parameterValues[i]; }
}
}
#endregion
#region ExecuteNonQuery(如果是增,删,修) /// <summary> /// 执行sql命令 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">命令类型</param> /// <param name="commandText">sql语句/参数化sql语句/存储过程名</param> /// <param name="commandParameters">参数</param> /// <returns>受影响的行数</returns> public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, commandType, conn, commandText, commandParameters); int val = cmd.ExecuteNonQuery();
return val; } }
/// <summary> /// 执行Sql Server存储过程 /// 注意:不能执行有out 参数的存储过程 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="spName">存储过程名</param> /// <param name="parameterValues">对象参数</param> /// <returns>受影响的行数</returns> public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues) {
using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, spName, parameterValues); int val = cmd.ExecuteNonQuery();
return val; } } #endregion
#region ExecuteReader(如果只是读数据) /// <summary> /// 执行sql命令 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">命令类型</param> /// <param name="commandText">sql语句/参数化sql语句/存储过程名</param> /// <param name="commandParameters">参数</param> /// <returns>SqlDataReader 对象</returns> public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) {
SqlConnection conn = new SqlConnection(connectionString); try { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, commandType, conn, commandText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr; } catch { conn.Close(); throw; } }
/// <summary> /// 执行Sql Server存储过程 /// 注意:不能执行有out 参数的存储过程 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="spName">存储过程名</param> /// <param name="parameterValues">对象参数</param> /// <returns>受影响的行数</returns> public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues) { SqlConnection conn = new SqlConnection(connectionString); try { SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, spName, parameterValues); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return rdr; } catch { conn.Close(); throw; }
} #endregion
#region ExecuteDataset(如果查询一个数据集)
/// <summary> /// 执行Sql Server存储过程 /// 注意:不能执行有out 参数的存储过程 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="spName">存储过程名</param> /// <param name="parameterValues">对象参数</param> /// <returns>DataSet对象</returns> public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues) { using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, conn, spName, parameterValues);
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet();
da.Fill(ds);
return ds; } } }
/// <summary> /// 执行Sql 命令 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">命令类型</param> /// <param name="commandText">sql语句/参数化sql语句/存储过程名</param> /// <param name="commandParameters">参数</param> /// <returns>DataSet 对象</returns> public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { using (SqlConnection conn = new SqlConnection(connectionString)) {
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, commandType, conn, commandText, commandParameters); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet();
da.Fill(ds);
return ds; } } }
#endregion
#region ExecuteScalar (如果你只查询一个值) /// <summary> /// 执行Sql 命令 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="commandType">命令类型</param> /// <param name="commandText">sql语句/参数化sql语句/存储过程名</param> /// <param name="commandParameters">参数</param> /// <returns>执行结果对象</returns> public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, commandType, conn, commandText, commandParameters); object val = cmd.ExecuteScalar();
return val; } }
/// <summary> /// 执行Sql Server存储过程 /// 注意:不能执行有out 参数的存储过程 /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="spName">存储过程名</param> /// <param name="parameterValues">对象参数</param> /// <returns>执行结果对象</returns> public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues) { SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, spName, parameterValues); object val = cmd.ExecuteScalar();
return val; } }
#endregion
#region Private Method /// <summary> /// 设置一个等待执行的SqlCommand对象 /// </summary> /// <param name="cmd">SqlCommand 对象,不允许空对象</param> /// <param name="conn">SqlConnection 对象,不允许空对象</param> /// <param name="commandText">Sql 语句</param> /// <param name="cmdParms">SqlParameters 对象,允许为空对象</param> private static void PrepareCommand(SqlCommand cmd, CommandType commandType, SqlConnection conn, string commandText, SqlParameter[] cmdParms) { //打开连接 if (conn.State != ConnectionState.Open) conn.Open();
//设置SqlCommand对象 cmd.Connection = conn; cmd.CommandText = commandText; cmd.CommandType = commandType;
if (cmdParms != null) { foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } }
/// <summary> /// 设置一个等待执行存储过程的SqlCommand对象 /// </summary> /// <param name="cmd">SqlCommand 对象,不允许空对象</param> /// <param name="conn">SqlConnection 对象,不允许空对象</param> /// <param name="spName">Sql 语句</param> /// <param name="parameterValues">不定个数的存储过程参数,允许为空</param> private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, string spName, params object[] parameterValues) { //打开连接 if (conn.State != ConnectionState.Open) conn.Open();
//设置SqlCommand对象 cmd.Connection = conn; cmd.CommandText = spName; cmd.CommandType = CommandType.StoredProcedure;
//获取存储过程的参数 SqlCommandBuilder.DeriveParameters(cmd);
//移除Return_Value 参数 cmd.Parameters.RemoveAt(0);
//设置参数值 if (parameterValues != null) { for (int i = 0; i < cmd.Parameters.Count; i++) { cmd.Parameters[i].Value = parameterValues[i];
} } } #endregion
CS通用项目系统搭建——三层架构第二天 (补一篇完整的SqlHelper)的更多相关文章
- CS通用项目系统搭建——三层架构第二天
一.编写配置文件 首先找到编写配置文件的位置.winform会在app.config.webform写在web.config中. <?xml version="1.0" en ...
- CS通用项目系统搭建——三层架构第一天
CS通用项目:使用三层架构进行搭建 三层架构: 表现层(UI(User Interface)):展示给用户的层面,包含窗体控件数据等信息. 业务逻辑层(BLL(Business Logic Layer ...
- 关于ASP.NET或VS2005 搭建三层架构的理解
最近想学习ASP.NET建网站,关于ASP.NET或VS2005 搭建三层架构的理解,网上摘录了一些资料,对于第(2)点的讲解让我理解印象深刻,如下: (1)为何使用N层架构? 因为每一层都可以在仅仅 ...
- MVC项目实践,在三层架构下实现SportsStore-02,DbSession层、BLL层
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- MVC项目实践,在三层架构下实现SportsStore,从类图看三层架构
在"MVC项目实践,在三层架构下实现SportsStore-02,DbSession层.BLL层"一文的评论中,博友浪花一朵朵建议用类图来理解本项目的三层架构.于是就有了本篇: I ...
- MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- MVC项目实践,在三层架构下实现SportsStore-04,实现分页
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- MVC项目实践,在三层架构下实现SportsStore-05,实现导航
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
随机推荐
- 360软件的木马查杀、漏洞修复等组件不能使用,提示runtime error
一.故障现象:1.360软件的木马查杀.漏洞修复等组件不能使用,提示runtime error2.暴风影音等很多软件不能正常使用3.设备管理器不能打开,提示“MMC 不能打开文件”4.部分https安 ...
- 黄聪:PHP获取某一天前后任意时间
date("Y-m-d",strtotime('-30 days',strtotime('2016-9-30')));
- 【java】之算法复杂度o(1), o(n), o(logn), o(nlogn)
在描述算法复杂度时,经常用到o(1), o(n), o(logn), o(nlogn)来表示对应算法的时间复杂度, 这里进行归纳一下它们代表的含义: 这是算法的时空复杂度的表示.不仅仅用于表示时间复杂 ...
- 将NULL值转化为“”
// 将NULL转化为“”,1是需要修改的实体类参数,3是转化后的实体对象 String 2= JSON.toJSONString(1, SerializerFeature.WriteNullStri ...
- 2018秋寒假作业5—PTA编程总结2
1.实验代码: 7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成"贰万叁仟壹百零捌&qu ...
- Java中final、finally、finalize有什么区别?
final.finally和finalize的区别是什么? final: 最终的意思,可以修饰类,方法和变量. 它修饰的类,不能被继承 它修饰的方法,不能被重写 它修饰的变量,不能被改变 finall ...
- docker-网络基础配置和dockerfile
00x1: 端口映射: 如图:这就是把容器的 5000端口和主机的32768端口相映射,所以通过访问主机的32768端口就可以访问容器的web界面 这个端口是系统默认的如果自定义命令:docker r ...
- 各平台操作系统查询主机WWPN
查询主机WWPN 目录 3.4.3.8.2.3 查询主机WWPN 3.4.3.8.2.3.1 查看主机HBA相应端口的WWPN(Windows) 3.4.3.8.2.3.2 查看主机HBA相应端口的W ...
- linux下安装nexus repository及Intellij Idea集成私有maven
前段日子公司搞java项目,使用nexus repository搭建了私有maven库,现在把原来的私有nuget也迁到nexus repository上了,下面介绍下搭建流程: https://he ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...