namespace ConsoleApplication2
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration; public class SqlHelper
{
/// <summary>
/// 连接字符串
/// </summary>ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;
/// <summary>
/// 准备Command对象
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
private static SqlCommand PrepareCommand(string sql, params SqlParameter[] spms)
{
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(sql, conn);
if (spms != null)
cmd.Parameters.AddRange(spms);
return cmd;
}
/// <summary>
/// 提交sql语句执行(增删改)
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns>影响行数</returns>
public static int ExecuteNonQuery(string sql, params SqlParameter[] spms)
{
int result = 0;
SqlCommand cmd = PrepareCommand(sql, spms);
try
{
cmd.Connection.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
throw new Exception(ex.Message);
}
finally
{
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
}
return result;
}
/// <summary>
/// 提交sql语句返回首行首列的值
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
public static object ExecuteScalar(string sql, params SqlParameter[] spms)
{
object result = null;
SqlCommand cmd = PrepareCommand(sql, spms);
try
{
cmd.Connection.Open();
result = cmd.ExecuteScalar();
}
catch (Exception ex)
{
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
throw new Exception(ex.Message);
}
finally
{
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
}
return result;
}
/// <summary>
/// 提交sql语句执行(增删改),bayistuta新增,2011/03/21 17:13
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns>影响行数</returns>
public static int ExecuteNonQuery(string sql, CommandType type, params SqlParameter[] spms)
{
int result = 0;
SqlCommand cmd = PrepareCommand(sql, spms);
cmd.CommandType = type;
try
{
cmd.Connection.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
throw new Exception(ex.Message);
}
finally
{
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
}
return result;
}
/// <summary>
/// 提交sql语句返回读取器,bayistuta新增,2011/03/25 21:26
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql, CommandType type, params SqlParameter[] spms)
{
SqlDataReader reader = null;
SqlCommand cmd = PrepareCommand(sql, spms);
cmd.CommandType = type;
try
{
cmd.Connection.Open();
//关闭reader对象,其对应的连接对象自动关闭
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (reader != null)
reader.Close();
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
throw new Exception(ex.Message);
}
return reader;
}
/// <summary>
/// 提交sql语句返回读取器
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] spms)
{
SqlDataReader reader = null;
SqlCommand cmd = PrepareCommand(sql, spms);
try
{
cmd.Connection.Open();
//关闭reader对象,其对应的连接对象自动关闭
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (reader != null)
reader.Close();
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
throw new Exception(ex.Message);
}
return reader;
}
/// <summary>
/// 查询实体类对象集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
public static List<T> GetList<T>(string sql, params SqlParameter[] spms)
{
List<T> list = new List<T>();
SqlDataReader reader = ExecuteReader(sql, spms);
while (reader.Read())
{
T t = CreateInstance<T>(reader);
list.Add(t);
}
reader.Close();
return list;
}
/// <summary>
/// 查询单个实体类对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
public static T GetSingle<T>(string sql, params SqlParameter[] spms)
{
T t = default(T);
SqlDataReader reader = ExecuteReader(sql, spms);
if (reader.Read())
{
t = CreateInstance<T>(reader);
}
reader.Close();
return t;
} /// <summary>
/// 返回DataTable
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spms">参数</param>
/// <returns></returns>
public static DataTable GetDataTable(string sql, params SqlParameter[] spms)
{
SqlCommand cmd = PrepareCommand(sql, spms); SqlDataAdapter da = new SqlDataAdapter(cmd); //创建DataAdapter数据适配器实例
DataSet ds = new DataSet();//创建DataSet实例
da.Fill(ds, "tables");//使用DataAdapter的Fill方法(填充),调用SELECT命令
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
return ds.Tables[0];
}
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spms">参数</param>
/// <returns></returns>
public static DataTable GetDataTable(string sql, CommandType cmdType, params SqlParameter[] spms)
{
SqlCommand cmd = PrepareCommand(sql, spms);
cmd.CommandType = cmdType;
SqlDataAdapter da = new SqlDataAdapter(cmd); //创建DataAdapter数据适配器实例
DataSet ds = new DataSet();//创建DataSet实例
da.Fill(ds, "tables");//使用DataAdapter的Fill方法(填充),调用SELECT命令
if (cmd.Connection.State != ConnectionState.Closed)
cmd.Connection.Close();
return ds.Tables[0];
}
/// <summary>
/// 查询记录条数
/// </summary>
/// <param name="sql"></param>
/// <param name="spms"></param>
/// <returns></returns>
public static int GetCount(string sql, params SqlParameter[] spms)
{
return (int)ExecuteScalar(sql, spms);
}
/// <summary>
/// 使用反射根据实体类的构造函数创建实例
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader"></param>
/// <returns></returns>
private static T CreateInstance<T>(IDataReader reader)
{
Type type = typeof(T);
T t = (T)Activator.CreateInstance(type, reader);
return t;
} /// <summary>
/// 防sql注入,替换字符串
/// </summary>
/// <param name="val">需要替换的值</param>
/// <returns>替换后的值</returns>
public static string GetParameterValue(string val)
{
return val.Replace("'", "''").Replace("-", "[-]");
}
} }

  

one pragmatical sqlhelper的更多相关文章

  1. 不该活着的SqlHelper和DBHelper

    前言: 还记得刚学ADO.NET的情景么? 还记得当年是怎么从ADO.NET被忽悠到用SqlHelper的么? 话说从入门到走上工作岗位那些年,我们就一直被纯纯地教导或引导,ADO.NET太原始,得封 ...

  2. SqlHelper中IN集合场景下的参数处理

    我手头有个古老的项目,持久层用的是古老的ADO.net.前两天去昆明旅游,其中的一个景点是云南民族村,通过导游介绍知道了一个古老的民族——基诺族,“基”在这个族内代表舅舅,“基诺”意为“跟在舅舅后边” ...

  3. 支持多返回值存储过程的SqlHelper

    public readonly string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionS ...

  4. 微软版的SqlHelper.cs类

    一,微软SQLHelper.cs类 中文版: using System; using System.Data; using System.Xml; using System.Data.SqlClien ...

  5. 万能的SqlHelper,麻麻再也不用担心用什么数据库了

    以前只用一种数据库,倒也无所谓,但是再数据库切换的时候,发现代码差不多呀. 最初,两种数据库,大不了写两个SqlHelper,但是多了也就发现代码重用率太低了吧. 因此,下面的SqlHelper诞生了 ...

  6. sqlHelper做增删改查,SQL注入处理,存储值,cookie,session

    一.存储值 eg:登录一个页面,在进入这个页面之前你怎么知道它登没登录呢?[在登录成功之后我们把状态保存起来] 存储值得方式有两种,一种是cookie,一种是session 1.1区别: 代码: if ...

  7. sqlHelper做增删改查

    1.把数据库里面的数据显示出来 sqlHelper怎么用:[网上可以下载,需要可以找楼主要] 1.拷贝到项目,修改它的命名空间等于当前项目名称 2.数据库的连接信息,用户名,密码,登录方式等 < ...

  8. SqlHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  9. .net C# SqlHelper for Oracle

    适用于Oracle的sqlhelper 需要使用ODP.Net,引用Oracle.DataAccess.dll 推荐安装ODAC 代码如下: using System; using System.Co ...

随机推荐

  1. 【前端_React】React小书

    参考书籍:React.js 小书

  2. php扩展开发-函数

    我们首先找到快速上手文章里面关于函数定义的代码,以此说明然后开发PHP的函数 //php_myext.h PHP_FUNCTION(myext_hello);//函数申明,所有在myext.c文件定义 ...

  3. 转发一个关于下载qq无损音乐的博客

    import requests import json headers = { 'Host': 'c.y.qq.com', 'Referer': 'http://c.y.qq.com/', 'User ...

  4. Darwin's Letter【达尔文的信】

    Darwin's Letter A letter written by Charles Darwin in 1875 has been returned to the Smithsonian Inst ...

  5. Labyrinth POJ - 1383

    Labyrinth POJ - 1383 The northern part of the Pyramid contains a very large and complicated labyrint ...

  6. Android使用Glide加载Gif.解决Glide加载Gif非常慢问题

    在Glide文档中找了半天没发现加载Gif的方式.然后通过基本的用法去加载: Glide.with(MainActivity.this).load(url).asGif().into(imageVie ...

  7. hive操作语句

    设置属性: //设置本地执行作set hive.exec.mode.local.auto=true; //设置动态分区 set hive.exec.dynamic.partition=true; se ...

  8. spark的flatMap和map区别

    map()是将函数用于RDD中的每个元素,将返回值构成新的RDD. flatmap()是将函数应用于RDD中的每个元素,将返回的迭代器的所有内容构成新的RDD,这样就得到了一个由各列表中的元素组成的R ...

  9. jar包导入仓库中

    mvn install:install-file -Dfile=F:/kaptcha-2.3.jar -DgroupId=com.google.code.kaptcha -DartifactId=ka ...

  10. Linux QA

    gitee: https://gitee.com/dhclly/icedog.script.test/blob/master/doc/linux/linux-qa.md 1. linux 中的 ll( ...