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

本文所述实例有别于网上常见的由代码生成器生成的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程序设计有一定的借鉴价值。

(0)

相关推荐

  • php中分页及SqlHelper类用法实例

    本文实例讲述了php中分页及SqlHelper类用法.分享给大家供大家参考,具体如下: 文档目录结构如下: SqlHelper.php代码如下: <?php /** * Created by JetBrains PhpStorm. * User: lee * Date: 13-7-26 * Time: 下午8:30 * To change this template use File | Settings | File Templates. */ class SqlHelper{ private

  • C#基于SQLiteHelper类似SqlHelper类实现存取Sqlite数据库的方法

    本文实例讲述了C#基于SQLiteHelper类似SqlHelper类实现存取Sqlite数据库的方法.分享给大家供大家参考.具体如下: 这个类不是我实现的,英文原文地址为http://www.eggheadcafe.com/articles/20050315.asp,这里修改了原文中分析sql语句参数的方法,将方法名修改为AttachParameters,将其修饰符修改为private,并直接传递command到这个方法,直接绑定参数到comand.修改后的代码如下 using System;

  • c#中SqlHelper封装SqlDataReader的方法

    本文实例讲述了c#中SqlHelper封装SqlDataReader的方法.分享给大家供大家参考.具体如下: /// <summary> /// 执行sql语句返回一个DataReader /// 当返回DataReader的时候,注意: /// 1.Connection不能关闭 /// 2.DataReader不能关闭 /// 3.command对象执行ExecuteReader()的时候需要传递一个参数CommandBehavior.CloseConnection /// </sum

  • C#实现较为实用的SQLhelper

    第一次写博客,想不到写什么好b( ̄▽ ̄)d ,考虑的半天决定从sqlhelper开始,sqlhelper对程序员来说就像helloworld一样,很简单却又很重要,helloworld代表着程序员萌新第一次写代码,而sqlhelper则是初次接触数据库(不知道这种说法对不对). 好了不废话了,下面直接上代码(无话可说了): public class SQLHelper { // 超时时间 private static int Timeout = 1000; // 数据库名称 public con

  • C# SqlHelper应用开发学习

    本文实例为大家分享了C# SqlHelper应用技巧,供大家参考,具体内容如下 使用App.config配置文件封装连接字符串,方便重复使用 --->添加App.conifg配置文件 --->Add : ConnectionString: --->添加引用 <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supported

  • 自己编写sqlhelper类示例分享

    自己编写SqlHelper,大家参考使用吧 复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;

  • 微软官方SqlHelper类 数据库辅助操作类 原创

    数据库操作类真的没有必要自己去写,因为成熟的类库真的非常完善了,拿来直接用就好,省时省力. 本文就为大家介绍微软官方的程序PetShop4.0中的SqlHelper类,先来做一下简单的介绍,PetShop是一个范例,微软用它来展示.Net企业系统开发的能力. 那SqlHelper中封装了哪些方法呢? 里面的函数一堆,常用的就那几个,无非就是增删改查嘛,来看下几种常用的函数: 1.ExecuteNonQuery 执行增删改 2.ExecuteReader 执行查询 3.ExecuteScalar

  • c# SQLHelper(for winForm)实现代码

    SQLHelper.cs 复制代码 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace HelloWinForm.DBUtility { class SQLHelper { #reg

  • asp.net SqlHelper数据访问层的使用

    本文章主要介绍SqlHelper使用. 每个项目都要用到数据访问层,我做的也不例外,但是我把数据访问层做成独立项目,没有什么太大的目的,数据访问层,仅仅做数据访问用,不包含任何逻辑. 为什么要使用数据访问层? 如果不使用数据访问层,那么你的代码里会出现很多SqlConnection.SqlCommand.SqlDataReader.Open. Close--这些类和方法,而且代码量很大,让你不胜其烦,而且代码写起来,其实都是体力活,没有技术含量.因此我们要把数据访问层封装起来,方便重用.微软已经

  • C#实现操作MySql数据层类MysqlHelper实例

    本文实例讲述了C#实现操作MySql数据层类MysqlHelper.分享给大家供大家参考.具体如下: using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using MySql.Data; using MySql.Data.MySqlCli

随机推荐