ADO.Net笔记整理(一)
几次装机,Notes已烟消云散,近日因为Node.js死活搞不定,无奈装机,备份好的东东,没想到磁盘扇区出现异常,可能是PE启动盘的病毒,只好将磁盘全部重新分区,恢复数据也懒得恢复了,日积月累关乎将来的开发效率,还好处于懵懂阶段。笔记一大堆,记忆力又不怎么好,所以web+log=blog;
SqlHelper类
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace DAL
{/// <summary>
/// SqlHelper类 by zy 2016-3-11
/// </summary>
public class SqlHelper
{
//如果项目中只连接了一个数据库 那么可以在此定义一个固定的连接字符串 public static string ConnectionStr { get; } =
ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; public static string ConnStr()
{
return ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
}
#region ExecuteNonQuery /// <summary>
/// 执行sql语句 返回受影响行数
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmdType">command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// 执行sql语句 返回当前插入的记录id(其实是ExecuteScalar转换成id返回)
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmdType">command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="returnID">返回id</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
out int returnID, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString))
{
returnID = 0;
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
returnID = (Int32) cmd.ExecuteScalar();
cmd.Parameters.Clear();
return returnID;
}
} /// <summary>
/// 执行sql语句 返回受影响行数(数据库连接SqlConnection)
/// </summary>
/// <param name="connection">SqlConnection连接</param>
/// <param name="cmdType">command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
} /// <summary>
/// 执行sql语句 返回受影响行数(事务SqlTransaction)
/// </summary>
/// <param name="trans">SqlTransaction事务</param>
/// <param name="cmdType">command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
} #endregion
#region ExecuteScalar
/// <summary>
/// 执行sql语句 返回结果集第一行第一列
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = new SqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// 执行sql语句 返回结果集第一行第一列(事务)
/// </summary>
/// <param name="trans">SqlTransaction事务</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
} /// <summary>
/// 执行sql语句 返回结果集第一行第一列(数据库连接)
/// </summary>
/// <param name="connection">SqlConnection连接</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
} #endregion
#region ExecuteReader
/// <summary>
/// 执行sql语句 返回reader
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="cmdType">command类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
} #endregion
#region DataSet
/// <summary>
/// 执行sql语句 返回DataSet结果集(无参数)
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">command类型</param>
/// <param name="commandText">sql语句</param>
/// <returns></returns>
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
{
return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[]) null);
} /// <summary>
/// 执行sql语句 返回DataSet结果集
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">command类型</param>
/// <param name="commandText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText,
params SqlParameter[] commandParameters)
{
//创建数据库连接 完成后dispose
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open(); //根据数据库连接 调用具体方法
return ExecuteDataset(cn, commandType, commandText, commandParameters);
}
} /// <summary>
/// 执行sql语句 返回DataSet结果集(数据库连接SqlConnection)
/// </summary>
/// <param name="connection">SqlConnection连接</param>
/// <param name="commandType">command类型</param>
/// <param name="commandText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText,
params SqlParameter[] commandParameters)
{
//创建command
SqlCommand cmd = new SqlCommand();
//准备command对象
PrepareCommand(cmd, connection, (SqlTransaction) null, commandType, commandText, commandParameters); //创建SqlDataAdapter和DataSet
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet(); //把结果集装进dataset
da.Fill(ds); //返回
return ds;
} #endregion DataSet
#region DataTable
/// <summary>
/// 执行sql语句 返回DataTable结果集(无参数)
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">command类型</param>
/// <param name="commandText">sql语句</param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText)
{
return ExecuteDataTable(connectionString, commandType, commandText, (SqlParameter[])null);
} /// <summary>
/// 执行sql语句 返回DataTable结果集
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="commandType">command类型</param>
/// <param name="commandText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText,
params SqlParameter[] commandParameters)
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
return ExecuteDataTable(cn, commandType, commandText, commandParameters);
}
} /// <summary>
/// 执行sql语句 返回DataTable结果集
/// </summary>
/// <param name="connection">SqlConnection连接</param>
/// <param name="commandType">command类型</param>
/// <param name="commandText">sql语句</param>
/// <param name="commandParameters">参数</param>
/// <returns></returns>
public static DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText,
params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, (SqlTransaction) null, commandType, commandText, commandParameters); SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(); da.Fill(dt);
return dt;
} #endregion DataTable
#region PrepareCommand
/// <summary>
/// 准备Command对象
/// </summary>
/// <param name="cmd">SqlCommand 对象</param>
/// <param name="conn">SqlConnection 对象</param>
/// <param name="trans">SqlTransaction 对象</param>
/// <param name="cmdType">Command 类型</param>
/// <param name="cmdText">sql语句</param>
/// <param name="cmdParms">参数</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans,
CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{
//打开SqlConnection连接
if (conn.State != ConnectionState.Open)
conn.Open();
//给Command的Connection CommandText设置值
cmd.Connection = conn;
cmd.CommandText = cmdText;
//如果是事务 设置事务
if (trans != null)
cmd.Transaction = trans;
//Command类型
cmd.CommandType = cmdType;
//Command参数
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
{
//在此可以操作参数 比如把参数值里的英文逗号全部改为中文逗号 或者其他操作
switch (parm.SqlDbType)
{
case SqlDbType.Char:
case SqlDbType.NChar:
case SqlDbType.NText:
case SqlDbType.NVarChar:
case SqlDbType.Text:
case SqlDbType.VarChar:
if (parm.Value != null && parm.Value != DBNull.Value && parm.ParameterName != "@SQLClause")
{
string tmp = parm.Value.ToString();
tmp = tmp.Replace(",", ",");
tmp = tmp.Replace("'", "'");
parm.Value = tmp;
}
break;
}//把参数添加到Command中的Parameters中
cmd.Parameters.Add(parm);
}
}
}#endregion
}}
原文地址不得,忘记哪里CV的了。
ADO.Net笔记整理(一)的更多相关文章
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- 从0开始学Swift笔记整理(五)
这是跟在上一篇博文后续内容: --Core Foundation框架 Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Sw ...
- Deep Learning(深度学习)学习笔记整理系列之(五)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- 学习ReactNative笔记整理一___JavaScript基础
学习ReactNative笔记整理一___JavaScript基础 ★★★笔记时间- 2017-1-9 ★★★ 前言: 现在跨平台是一个趋势,这样可以减少开发和维护的成本.第一次看是看的ReactNa ...
- Deep Learning(深度学习)学习笔记整理系列之(八)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(七)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(六)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(四)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列之(三)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
随机推荐
- javascript 倒计数功能
最近在项目中遇到一个倒计时功能,在网上没有找到合适的,就自己写了个方法.贴在这里,权且当个记录. export const timeRun = (timeStr, callBack) => { ...
- PHP正则表达式二分法实现mysql盲注脚本
$sUrl = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $sPost = 'inject=Inject&injection='; $sCharset = 'AB ...
- SpringBoot 动态更新 resources 目录的文件
一.前言 SpringBoot 打成 Jar 包形式运行后 ,resources 目录下文件的读取修改和原来不太一样,网上比较多的是关于读取的方式,修改的几乎没有,终于在 stackoverflow ...
- Ext.isNumber与Ext.isNumeric
Ext.isNumber: Ext.isNumber(1) true Ext.isNumber(new Number(1)) false Ext.isNumber("1") fal ...
- html 微信video放大后无法返回问题
android video播放视频放大后无法返回,先debug下debugx5.qq.com 发现用的不是X5内核 直接激活 debugmm.qq.com/?forcex5=true 问题解决 ...
- 《JavaScript高级程序设计》笔记:DOM(十)
Node类型 nodeType以下是一些重要的nodeType的取值:1: 元素element2: 属性attr3: 文本text8: 注释comments9: 文档document nodeName ...
- 生鲜配送管理系统_升鲜宝V2.0 价格组功能 操作说明_15382353715
价格组功能是B端供应链系统,必不可少的一个功能,其主要实现不同的客户不同的价格,B端系统有一个最大的不同就是,有些商品后台下单人员能看到的.有些商品在销售的那一瞬间,还不知道价格.所以这些商品只有后台 ...
- 【Linux】【MySQL】CentOS7、MySQL8.0.13 骚操作速查笔记——专治各种忘词水土不服
1.前言 [Linux][MySQL]CentOS7安装最新版MySQL8.0.13(最新版MySQL从安装到运行) 专治各种忘词,各种水土不服. - -,就是一个健忘贵的速查表:(当然不包括SQL的 ...
- K邻近回归算法
代码: # -*- coding: utf-8 -*- """ Created on Fri Jul 13 10:40:22 2018 @author: zhen &qu ...
- C语言面试程序阅读整理
一.数组和指针 1.数组和指针的存储 写出下面的输出结果: char str1[] = "abc"; char str2[] = "abc"; const ch ...