封装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 ...
随机推荐
- 【LeetCode】217. Contains Duplicate
题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...
- Python如何调用新浪api接口的问题
前言:这些天在研究如何调用新浪开放平台的api分析新浪微博用户的数据 成果:成功调用了新浪api获取了用户的一些个人信息和无数条公共微博 不足:新浪开放平台访问有限制,返回的数据着实有限,不足以分析问 ...
- Latex加速: TexStudio的安装和使用
TexStudio可以加速Latex的编辑和写作,这里简单介绍一下TexStudio的安装,配置和使用.但是有一个重要的前提,TexStudio会使Latex源代码和pdf文件并排显示,比较占用桌面的 ...
- 排序算法总结及Java实现
1. 整体介绍 分类 排序大的分类可以分为两种,内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.主要需要理解的都是内排序算法: 内排序可以分为 ...
- VBS连接远程Oracle
原文链接:http://hi.baidu.com/coo_boi/item/5a2e1860ded285136995e6a7 连接方式还是用的ADO,驱动是MSDAORA. 使用oracle前,ora ...
- vijos1037题解
题目: 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念"9?11"事件,Mr.F决定自己用水晶来搭建一座双塔. Mr. ...
- JavaScript一个cookie存储的类
所有输出都在浏览器的控制台中 <script type="text/javascript"> /** * cookieStorage.js * 本类实现像localSt ...
- JavaScript跨域请求和jsonp请求实例
<script type="text/javascript" src="./whenReady.js"></script> <sc ...
- 用户代理字符串(navigator.userAgent)检测方法
最近在看<JavaScript 高级程序设计(第三版)>,发现其中关于用户代理字符串检测技术的一些方法,觉得讲的很详细.用户代理字符串(navigator.userAgent)中包含了大量 ...
- 【PHP】PHP面向对象编程--phpOOP入门
PHP从入门到精通 之PHP的面相对象编程 面向对象编程(Object Oriented Programming, OOP, 面向对象程序设计)是一种计算机编程架构,OOP的一条基本原则是计算机程序 ...