• SqlHelper DBHelper


    根据自己项目的开发需要,整理了一个SqlHelper类

    相比较网上通用的SqlHelper类方法主要有一下几点的不同:

    1.因为要操作多个数据库,所以数据库连接字符串没有写死到方法里,作为参数提供出来。

    2.因为涉及到多表多库的操作,涉及到数据库级别的事物,以及逻辑业务上的事物,因此,要从最基元的部分就尽可能屏蔽问题,进行严格的事物处理。

    方法如下:

    个人认为使用率较高也较为方便的是   ExecuteNonQuery (增、删、改),ExecuteDataTable(支持存储过程和SQL语句查询,返回tadatable)

    其中ExecuteNonQuery中的 iCount 参数就是用来进行事物处理的

    /// <summary>
    /// 对数据库执行增、删、改命令
    /// </summary>
    /// <param name="sql">T-Sql语句</param>
    /// <param name="pa">参数数组</param>
    /// <param name="iCount">成功情况下影响行数</param>
    /// <returns>受影响的记录数</returns>
    public static int ExecuteNonQuery(string connectionString, string sql, SqlParameter[] pa, int iCount)
    {
    using (SqlConnection Connection = new SqlConnection(connectionString))
    {
    Connection.Open();
    SqlTransaction trans = Connection.BeginTransaction();
    try
    {
    SqlCommand cmd = new SqlCommand(sql, Connection);
    cmd.Transaction = trans;
    if (pa != null)
    {
    cmd.Parameters.AddRange(pa);
    }

    if (Connection.State != ConnectionState.Open)
    {
    Connection.Open();
    }
    int result = cmd.ExecuteNonQuery();
    if (iCount != 0 && iCount != result)
    {
    trans.Rollback();
    return 0;
    }

    trans.Commit();
    return result;
    }
    catch (Exception ex)
    {
    trans.Rollback();
    return 0;
    }
    }
    }

    /// <summary>
    /// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataTable
    /// </summary>
    /// <param name="connectionString">连接字符串</param>
    /// <param name="type">命令类型(T-Sql语句或者存储过程)</param>
    /// <param name="sql">T-Sql语句或者存储过程的名称</param>
    /// <param name="pa">参数数组</param>
    /// <returns>结果集DataTable</returns>
    public static DataTable ExecuteDataTable(string connectionString, CommandType type, string sql, params SqlParameter[] pa)
    {
    using (SqlConnection Connection = new SqlConnection(connectionString))
    {
    try
    {
    if (Connection.State != ConnectionState.Open)
    Connection.Open();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand(sql, Connection);
    cmd.CommandType = type;
    if (pa != null)
    cmd.Parameters.AddRange(pa);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    if (ds.Tables != null && ds.Tables.Count > 0)
    {
    return ds.Tables[0];
    }
    }
    catch (Exception ex)
    {
    return null;
    }
    return null;
    }
    }

    /// <summary>
    /// 查询结果集中第一行第一列的值
    /// </summary>
    /// <param name="connectionString">连接字符串</param>
    /// <param name="sql">T-Sql语句</param>
    /// <param name="pa">参数数组</param>
    /// <returns>第一行第一列的值</returns>
    public static int ExecuteScalar(string connectionString, string sql, SqlParameter[] pa)
    {
    using (SqlConnection Connection = new SqlConnection(connectionString))
    {
    if (Connection.State != ConnectionState.Open)
    Connection.Open();
    SqlCommand cmd = new SqlCommand(sql, Connection);
    if (pa != null)
    cmd.Parameters.AddRange(pa);
    int result = Convert.ToInt32(cmd.ExecuteScalar());
    return result;
    }
    }

    /// <summary>
    /// 创建数据读取器
    /// </summary>
    /// <param name="connectionString">连接字符串</param>
    /// <param name="sql">T-Sql语句</param>
    /// <param name="pa">参数数组</param>
    /// <param name="Connection">数据库连接</param>
    /// <returns>数据读取器</returns>
    public static SqlDataReader ExecuteReader(string connectionString, string sql, SqlParameter[] pa, SqlConnection conn)
    {
    if (conn.State != ConnectionState.Open)
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    if (pa != null)
    cmd.Parameters.AddRange(pa);
    SqlDataReader reader = cmd.ExecuteReader();
    return reader;
    }

  • 相关阅读:
    spring reference
    Connector for Python
    LDAP
    REST
    java利用泛型实现不同类型可变参数
    java细节知识
    事务隔离的级别
    servlet cdi注入
    session and cookie简析
    CORS’s source, principle and implementation
  • 原文地址:https://www.cnblogs.com/lsmhome/p/9088315.html
Copyright © 2020-2023  润新知