.NET的SqlHelper应用代码
首先需要引用命名空间 ,同时也需要右击'引用' --> '添加引用' --> '程序集' --> '框架' --> 'System.Configuration',SqlHelper属于三层中的DAL层:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class SqlHelper
{
private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString(); public static SqlConnection Open() //存储过程
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
return con;
} /// <summary>
/// Insert update delete
/// </summary>
/// <param name="commandText">SQL</param>
/// <param name="type">命令类型</param>
/// <param name="pars">参数列表</param>
/// <returns></returns>
public static int ExecuteNoQuery(string commandText, CommandType type, params SqlParameter[] pars)
{
int result = ;
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
SqlCommand cmd = new SqlCommand(commandText, con);
cmd.CommandType = type;
if (pars != null)
{
cmd.Parameters.AddRange(pars); }
result = cmd.ExecuteNonQuery(); }
return result;
}
/// <summary>
/// select
/// </summary>
/// <param name="commandText"></param>
/// <param name="type"></param>
/// <param name="pars"></param>
/// <returns></returns>
public static DataTable ExecuteDataTable(string commandText, CommandType type, params SqlParameter[] pars)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
SqlCommand cmd = new SqlCommand(commandText, con);
cmd.CommandType = type;
if (pars != null)
{
cmd.Parameters.AddRange(pars);
}
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(ds);
dt = ds.Tables[];
}
return dt;
}
private static SqlConnection con = null;
public static void CloseCon()
{
if (con != null && con.State == ConnectionState.Open)
{
con.Close();
}
}
/// <summary>
/// select
/// </summary>
/// <param name="commandText"></param>
/// <param name="type"></param>
/// <param name="pars"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string commandText, CommandType type, SqlParameter[] pars)
{ SqlCommand cmd = null;
SqlDataReader reader = null;
try
{
con = new SqlConnection(conStr);
con.Open();
cmd = new SqlCommand(commandText, con);
if (pars != null)
{
cmd.Parameters.AddRange(pars);
}
reader = cmd.ExecuteReader(); }
catch (SqlException ex)
{ }
return reader; }
public static object ExecScalre(string commandText, CommandType type, SqlParameter[] pars)
{
object obj = null;
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
SqlCommand cmd = new SqlCommand(commandText, con);
cmd.CommandType = type;
if (pars != null)
{
cmd.Parameters.AddRange(pars); }
obj = cmd.ExecuteScalar(); }
return obj;
}
Web.config 代码如下:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="conStr" connectionString="Data Source=LS--20161126TNE;Initial catalog=TestOne;User Id = sa ;Pwd=as123123"></add>
</connectionStrings>
</configuration>
.NET的SqlHelper应用代码的更多相关文章
- 微软 SqlHelper代码、功能、用法介绍:高效的组件
数据访问组件SqlHelper数据访问组件是一组通用的访问数据库的代码,在所有项目中都可以用,一般不需要修改.本节使用的是Microsoft提供的数据访问助手,其封装很严密,且应用简单. 首先要先添加 ...
- 关于SqlHelper的详解
SqlHelper是一个基于.NET Framework的数据库操作组件.组件中包含数据库操作方法.SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlComma ...
- SQLHelper 简介
什么是SQLHelper SqlHelper是一个基于.NETFramework的数据库操作组件.组件中包含数据库操作方法,目前SqlHelper有很多版本,主要以微软一开始发布的SqlHelper类 ...
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...
- 改进的sqlhelper学习日志
下面就是详细的sqlhelper的代码了 using System; using System.Collections.Generic; using System.Linq; using System ...
- 再学习sqlhelper
在机房收费重构系统的时候,第一次学习sqlhelper.当时感觉比较简单,没有写博客总结,现在又经过了图书馆的学习,感觉还是有必要写一写的. SqlHelper是一个基于.NETFramework的数 ...
- SqlHelper初探之二
在上一篇简单的介绍了sqlhelper的基本知识,接下来就让我们进一步学习他的实践过程. 首先:我们要明白的一件事Sqlhelper不是写出来的,而是在D层的代码中提炼出来的?那么就会反问一句“D层中 ...
- 数据操作的封装--sqlhelper
为了提高软件的灵活性和可维护性,软件的代码须要科学的管理.我们引入了架构这个词.设计模式提醒我们,软件中反复性的代码须要封装起来. 近期在做收费系统时.须要和数据库进行频繁的联系.既然是反复的使用,就 ...
- 【vb.net机房收费系统】之sqlhelper 标签: 数据库 2015-05-17 10:47 819人阅读 评论(15)
在敲机房收费重构版的时候,用到了sqlhelper,当时不知道怎么开始,各种听别人说,张晗说,一定要用sqlhelper,特别好用,我当时没有用balabala~当时一听,哎哎哎,这个高级,要搞一搞, ...
随机推荐
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- HDU-5086-Revenge of Segment Tree
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5086 这题太不应该了,比赛时没做出来,本来呢,以现在的水平这题是能够做出来的,可就是题目理解错了,按题 ...
- [源代码] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)
我在2016年10月发过一篇博客,介绍了我写过的一个多语言资源文件编辑器,并且做为免费软件发布给了出来. 陆续收到了一些朋友的反馈,有朋友很热心提了很多建议和关心的话,还有朋友发红包过来,让我很感动. ...
- JS日期时间加减实现
首先,上代码 var diffDate = function(date, diff) { return new Date( Date.UTC( date.getUTCFullYear(), date. ...
- 大大维的游戏机计划3--2048v1
前几天由于忙着过年串门,游戏机的计划搁置了几天.这两天终于空出了一块时间,抽空写了2048. 由于笔者前面自制了一个类似2048的游戏,所以写起来也算是轻车熟路,花了两个晚上也就差不多了. 废话少说, ...
- iOS 获取公历、农历日期的年月日
iOS 获取公历.农历日期的年月日 介绍三种方法获取 Date (NSDate) 的年月日. 用 date 表示当前日期.测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九. l ...
- [CSS3]学习笔记-文字与字体相关样式
1.给文字添加阴影 <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- Linux 菜鸟学习笔记--系统分区
硬盘分区 常识 主分区:最多只能有4个 扩展分区:用于突破主分区最多4个的限制 *最多只能有1个 *主分区+扩展分区最多有4个 *不能写入数据,只能包含逻辑分区 逻辑分区 格式化:实际是写入文件系统, ...
- [2017.02.07] Lua入门学习记录
#!/home/auss/Projects/Qt/annotated/lua -- 这是第一次系统学习Lua语言 --[[ 参考资料: 1. [Lua简明教程](http://coolshell.cn ...
- 为什么Java可以跨平台,而其他语言不行
你好 我是大福 你现在看的是大福笔记 今天复习了Java语言的概述 内容包括Java 语言的历史.语言特点及平台版本 JRE和JDK的区别 这篇文章的主题是总结下对Java语言特点中的跨平台原理. 在 ...