封装sqlhelper类
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace WXCustomerCard.DataAccess {
public class DataBase {
private string connectionString ="server=.;uid=sa;password=123456;Database=WXCustomerCard;max pool size=1000";//server=120.24.159.211;uid=sa;password=20894796@qq.com;Database=WXCustomerCard;max pool size=1000
//private string connectionString = "server=120.24.159.211;uid=sa;password=20894796@qq.com;Database=WXCustomerCard;max pool size=1000";//"server=.;uid=sa;password=123456;Database=WXCustomerCard;max pool size=1000";//server=120.24.159.211;uid=sa;password=20894796@qq.com;Database=WXCustomerCard;max pool size=1000
public DataBase() {
}
public DataBase(string conStr) {
connectionString = conStr;
}
public object ExecuteScalar(string sql,List< DbParameter> parameters = null) {
try {
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlCommand command = connection.CreateCommand()) {
command.CommandType = CommandType.Text;
command.CommandText = sql;
if (parameters != null && parameters.Count > 0) {
foreach (SqlParameter item in parameters) {
command.Parameters.Add(item);
}
}
connection.Open();
var data = command.ExecuteScalar();
command.Parameters.Clear();
return data;
}
}
}
catch (Exception ex) {
throw ex;
}
}
public bool ExecuteNonQuery(string sql, List<DbParameter> parameters = null) {
try {
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlCommand command = connection.CreateCommand()) {
command.CommandType = CommandType.Text;
command.CommandText = sql;
if (parameters != null && parameters.Count > 0) {
foreach (SqlParameter item in parameters) {
command.Parameters.Add(item);
}
}
connection.Open();
return command.ExecuteNonQuery() > 0;
}
}
}
catch (Exception ex) {
throw ex;
}
}
public List<T> ExecuteReader<T>(string sql, List<DbParameter> parameters = null) where T : class, new() {
try {
using (SqlConnection connection = new SqlConnection(connectionString)) {
using (SqlCommand command = connection.CreateCommand()) {
command.CommandType = CommandType.Text;
command.CommandText = sql;
if (parameters != null && parameters.Count > 0) {
foreach (SqlParameter item in parameters) {
command.Parameters.Add(item);
}
}
connection.Open();
T t = new T();
var task = command.ExecuteReader();
List<T> list = DataConvert.DataReaderToEntities<T>(task);
if (list != null && list.Count > 0) {
return list;
}
return null;
}
}
}
catch (Exception ex) {
throw ex;
}
}
}
}
封装sqlhelper类的更多相关文章
- ado.net的简单数据库操作(二)之封装SqlHelperl类
今天我书接上回,接着昨天的ado.net的数据库操作的相关知识来讲哈! 从上篇文章给出的实例来看,你一定会发现,操作数据库其实还挺麻烦的,就连一个最简单的数据库操作语句都要包括 定义数据库连接字符串. ...
- JAVA WEB SQLHelper类的封装
在这次做项目中,我对自己最满意的就是封装了一下SQLHelper类,我对自己感到骄傲主要是 我是初学者,我刚开始不知道可以这样做,我只是想着试着去这样做了,结果真的可以,所以我 在我的模块就自己封装了 ...
- C#封装CRUD到SqlHelper类解读
1.简单说明一下,一般情况下,数据库连接字符串是在App.config文件中进行配置,然后再在代码中进行引用.因此,我们在这里先看一下App.config文件. 首先看需要添加的内容: 参数说明: n ...
- ADO.NET复习——自己编写SqlHelper类
今天复习了一次ADO.NET基础,整理一下自己的认为的重点: 编写SqlHelper类,方便我们执行数据库语句,这时可以直接调用封装在SqlHelper类的方法.现在大多数公司面试的时候,给你的面试题 ...
- 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类
在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...
- 微软官方SqlHelper类 数据库辅助操作类
数据库操作类真的没有必要自己去写,因为成熟的类库真的非常完善了,拿来直接用就好,省时省力. 本文就为大家介绍微软官方的程序PetShop4.0中的SqlHelper类,先来做一下简单的介绍,PetSh ...
- 数据操作的封装--sqlhelper
为了提高软件的灵活性和可维护性,软件的代码须要科学的管理.我们引入了架构这个词.设计模式提醒我们,软件中反复性的代码须要封装起来. 近期在做收费系统时.须要和数据库进行频繁的联系.既然是反复的使用,就 ...
- SpringMVC 自动封装枚举类的方法
springmvc默认无法自动封装枚举类,解决方法如下: 1.枚举类 public enum GoodsPromoteEnum { /** * 0 精品 */ fine("精品", ...
- 最新的SqlHelper 类
最新的SqlHelper 类 摘自:http://www.cnblogs.com/sufei/archive/2010/01/14/1648026.html using System; using S ...
随机推荐
- 小程序解析html标签wxPrase插件
微信小程序的标签和原来我们习惯用的标签是不一样的,例如视图容器标签小程序是view,然而html就很多比如常用的div就和小程序的view类似. 通常我们在开发小程序(从列表页跳转到详情页)通过富文本 ...
- mongo中的分页查询
/** * @param $uid * @param $app_id * @param $start_time * @param $end_time * @param $start_page * @p ...
- Kafka 客户端实现逻辑分析
这里主要分析kafka 客户端实现 (代码分析以perl kafka实现为准) kafka客户端分为生产者和消费者,生产者发送消息,消费者获取消息. 在kafka协议里客户端通信中用到的最多的四个协议 ...
- asp.net使用qq邮箱发送邮件
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- [leetcode-606-Construct String from Binary Tree]
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- Redis 基础数据结构与对象
Redis用到的底层数据结构有:简单动态字符串.双端链表.字典.压缩列表.整数集合.跳跃表等,Redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这些数据结构创建了一个对象系统,这个系统包 ...
- Java多线程中join方法详解
join()方法用于让当前执行线程等待join线程执行结束.其实现原理是不停的检查join线程是否存活,如果join线程存活则让当前线程永远等待. join()方法部分实现细节 while(isAli ...
- Unity User Group 北京站图文报道:《Unity虚拟现实实战技巧》
时间来到了盛夏,北京UUG活动也来到了第八期.本次活动的主题为<Unity虚拟现实实战技巧>,为此我们邀请了4位资深的行业大神.这次我们仍然在北京市海淀区丹棱街5号微软大厦举行活动,在这里 ...
- updateByPrimaryKey和updateByPrimaryKeySelective insert和insertSelective
这两个update都是使用generator生成的mapper.xml文件中,对dao层的更新操作 updateByPrimaryKey对你注入的字段全部更新(不判断是否为Null) updateBy ...
- 打造属于自己的支持版本迭代的Asp.Net Web Api Route
在目前的主流架构中,我们越来越多的看到web Api的存在,小巧,灵活,基于Http协议,使它在越来越多的微服务项目或者移动项目充当很好的service endpoint. 问题 以Asp.Net W ...