one pragmatical sqlhelper

news/2024/7/8 1:34:03
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("-", "[-]");}}}

  

转载于:https://www.cnblogs.com/hualiu0/p/5216221.html


http://lihuaxi.xjx100.cn/news/257219.html

相关文章

牛客网:为什么不能将实数作为 HashMap 的 key?

欢迎关注方志朋的博客&#xff0c;回复”666“获面试宝典1.起因让我关注到这一点的起因是一道题:牛客网上的max-points-on-a-line题目是这么描述的:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.大意就是给我一些点的…

「摸鱼」神器来了,Python 实现人脸监测制作神器

作者 | 李秋键 出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09; 最近都在讨论工作摸鱼&#xff0c;网易云音乐也出了合理摸鱼时间表&#xff0c;今天给大家推荐如何用python实现摸鱼~码住呦&#xff01; 引言&#xff1a;脸部表情是人类情绪的最直接外部表现之一和进…

冲上热搜!清华95后博士,搞科研演绎浪漫爱情故事获赞千万

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达本文系募格课堂整合&#xff0c;参考来源&#xff1a;清华大学、中国新闻周刊、辽沈晚报、微博从清华本科毕业到博士后&#xff0c;他可以是拿过奥赛金牌、做起实验训练有素…

【C#串口编程计划】通信协议解析 -- byte[]与常用类型的转换

刚刚完成一个串口通讯的系统。目前在把串口通信的代码整合到团队的类库中&#xff08;把串口通信与网口Soket通讯整合起来&#xff0c;后面只需要配置参数&#xff0c;就可实现网络与串口通讯的转换&#xff09;&#xff0c;故C#串口编程计划的最后一篇图文“C#通讯类库框架”还…

Applet相关知识

1、Applet定义 Applet是采用Java编程语言编写的小应用程序&#xff0c;该程序可以包含在 HTML&#xff08;标准通用标记语言的一个应用&#xff09;页中&#xff0c;与在页中包含图像的方式大致相同。含有Applet的网页的HTML文件代码中部带有<applet> 和</applet>这…

伤感的故事

人的生活本来就是哪么的平淡无味。如果是一对老人&#xff0c;我会认为这样的平淡是幸福的&#xff0c;因为他们两个人从年轻一起走到白发苍苍。如果是年轻人&#xff0c;哪么是可悲的。我记得有一个人给我讲了一个故事&#xff0c;故事的内容就是&#xff1a;“有一天一个老和…

ecshop修改注册、增加手机

1.去掉“用户名”注册 a.去掉提交 user_passport.dwt页面去掉 <input name"username" type"text" size"30" id"username" οnblur"is_registered(this.value);" class"input_login" />提交 b.去掉js表单验证…

面经:什么是Transformer位置编码?

↑↑↑关注后"星标"Datawhale每日干货 & 每月组队学习&#xff0c;不错过Datawhale干货 作者&#xff1a;陈安东&#xff0c;中央民族大学&#xff0c;Datawhale成员过去的几年里&#xff0c;Transformer大放异彩&#xff0c;在各个领域疯狂上分。它究竟是做什么…