.NET的SqlHelper应用代码
2018-06-23 23:31:56来源:未知 阅读 ()
首先需要引用命名空间 ,同时也需要右击'引用' --> '添加引用' --> '程序集' --> '框架' --> '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 = 0; 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[0]; } 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>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 麻雀虽小五脏俱全 Dojo自定义控件应用 2020-02-20
- JSP session配置对web应用的影响 2019-12-27
- javascript IE中的DOM ready应用技巧 2019-11-17
- 用 JSON 处理缓存 2019-09-30
- ko knockoutjs动态属性绑定技巧应用 2019-09-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash