四个常用的.NET的SQLHELPER方法实例

所属分类: 网络编程 / ASP.NET 阅读数: 715
收藏 0 赞 0 分享

本文所述实例有别于网上常见的由代码生成器生成的sqlhelper,比如动软、CodeSmith等生成的。其实代码生成器生成的sqlhelper很多的方法在实际开发中都是用不到的,考虑初学者如果封装类的方法太多,会造成一定的困扰,也会给他们增加负担,所以本文列举出了再实际运用中总结的四个比较常用的方法,其实,最常用的应该是两个,就是查和增删改,其它两个也是用的比较少的。

需要说明的是,sqlhelper在winform的开发中用的比较多,在asp.net和mvc的项目中用的封装类跟winform有相似,但是也有一定的区别,因为大项目都是用那种比较好的框架,或者自己公司开发的框架,其中的封装类也有所不同,本文总结的这四个方法在winform中用比较常用。

主要代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace SQL
{
  public static class SqlHelper
  {
    /// <summary>
    /// 创建连接的字符串
    /// </summary>
    static readonly string connStr=ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

 #region 1.0 执行查询语句,返回一个表 + static DataTable ExcuteTable(string sql, params SqlParameter[] ps)
    /// <summary>
    /// 1.0 执行查询语句,返回一个表
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <param name="ps">参数数组</param>
    /// <returns>返回一张表</returns>
    public static DataTable ExcuteTable(string sql, params SqlParameter[] ps)
    {
      SqlDataAdapter da = new SqlDataAdapter(sql, connStr);
      da.SelectCommand.Parameters.AddRange(ps);
      DataTable dt = new DataTable();
      da.Fill(dt);
      return dt;
    } 
    #endregion

    #region 2.0 执行增删改的方法 + static int ExcuteNoQuery(string sql, params SqlParameter[] ps)
    /// <summary>
    /// 2.0 执行增删改的方法
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <param name="ps">参数数组</param>
    /// <returns>返回一条记录</returns>
    public static int ExcuteNoQuery(string sql, params SqlParameter[] ps)
    {
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        SqlCommand command = new SqlCommand(sql, conn);
        command.Parameters.AddRange(ps);
        return command.ExecuteNonQuery();
      }
    } 
    #endregion

    #region 3.0 执行存储过程的方法 + static int ExcuteProc(string procName, params SqlParameter[] ps)
    /// <summary>
    /// 3.0 执行存储过程的方法
    /// </summary>
    /// <param name="procName">存储过程名</param>
    /// <param name="ps">参数数组</param>
    /// <returns></returns>
    public static int ExcuteProc(string procName, params SqlParameter[] ps)
    {
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        SqlCommand command = new SqlCommand(procName, conn);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddRange(ps);
        return command.ExecuteNonQuery();
      }
    } 
    #endregion

    #region 4.0 查询结果集,返回的是首行首列 + static int ExecScalar(string sql, params SqlParameter[] ps)
    /// <summary>
    /// 4.0 查询结果集,返回的是首行首列
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <param name="ps">参数数组</param>
    /// <returns></returns>
    public static object ExecScalar(string sql, params SqlParameter[] ps) //调用的时候才判断是什么类型
    {
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        SqlCommand command = new SqlCommand(sql, conn);
        command.Parameters.AddRange(ps);
        return command.ExecuteScalar();
      }
    } 
    #endregion
  }
}

相信本文所述对大家的.net程序设计有一定的借鉴价值。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多