日期:2014-05-16  浏览次数:20615 次

数据库助手类

引用命名空间:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

?

具体代码如下:

private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataAdapter sda = null;
private DataSet ds = null;

//构造函数定义连接字符串
public SqlHelper()
{
    string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
    conn = new SqlConnection(connStr);
}

//打开连接
private SqlConnection getconn()
{
    if (conn.State == ConnectionState.Closed)
    {
        conn.Open();
    }
    return conn;
}

/// <summary>执行带参数的增删改sql语句或存储过程
/// 
/// </summary>
/// <param name="cmdText">增删改sql语句或存储过程</param>
/// <param name="paras">参数成员</param>
/// <returns>受影响行数</returns>
public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
    int i;
    try
    {
        //执行存储过程命令
        cmd = new SqlCommand(cmdText, getconn());
        //cmd访问类型
        cmd.CommandType = ct;
        //传递存储过程参数
        cmd.Parameters.AddRange(paras);
        //返回受影响行数
        i = cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {

        throw ex;
    }
    finally
    {
        conn.Close();
    }
    return i;
}

/// <summary>执行带参数的查询sql语句或存储过程
/// 
/// </summary>
/// <param name="cmdText">查询的sql语句或存储过程</param>
/// <param name="paras">参数成员</param>
/// <returns>返回查询表</returns>
public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
{
    DataTable dt = new DataTable();
    try
    {
        //执行存储过程命令
        cmd = new SqlCommand(cmdText, getconn());
        //cmd访问类型
        cmd.CommandType = ct;
        //传递存储过程参数
        cmd.Parameters.AddRange(paras);
        //读取数据
        sda = new SqlDataAdapter(cmd);
        //填充数据表
        ds = new DataSet();
        sda.Fill(ds);
        dt = ds.Tables[0];
    }
    catch (Exception ex)
    {

        throw ex;
    }
    finally
    {
        conn.Close();
    }
    return dt;
}

/// <summary> 执行不带参数的查询sql语句或存储过程(方法重载)
///
/// </summary>
/// <param name="cmdText">查询的sql语句或存储过程</param>
/// <returns>返回数据表</returns>
public DataTable ExecuteQuery(string cmdText, CommandType ct)
{
    DataTable dt = new DataTable();
    try
    {
        //执行存储过程命令
        cmd = new SqlCommand(cmdText, getconn());
        //cmd访问类型
        cmd.CommandType = ct;
        //读取数据
        sda = new SqlDataAdapter(cmd);
        //填充数据表
        ds = new DataSet();
        sda.Fill(ds);
        dt = ds.Tables[0];
    }
    catch (Exception ex)
    {

        throw ex;
    }
    finally
    {
        conn.Close();
    }
    return dt;
}

/// <summary>执行带参数的返回受影响行数的sql语句或存储过程
/// 
/// </summary>
/// <param name="cmdText">sql语句或存储过程</param>
/// <param name="paras">参数成员</param>
/// <returns>返回受影响行数</returns>
public int ExecuteScalar(string cmdText, SqlParameter[] paras, CommandType ct)
{
    int i;
    try
    {
        //执行存储过程命令
        cmd = new SqlCommand(cmdText, getconn());
        //cmd访问类型
        cmd.CommandType = ct;
        //传递存储过程参数
        cmd.Parameters.AddRange(paras);
        //返回受影响行数
        i = (int)cmd.ExecuteScalar();
    }
    catch (Exception ex)
    {

        throw ex;
    }
    finally
    {
        conn.Close();
    }
    return i;
}

/// <summary>执行不带参数的返回受影响行数的sql语句或存储过程(方法重载)
/// 
/// </summary>
/// <param name="cmdText">sql语句或存储过程</param>
/// <param name="paras">参数成员</param>
/// <returns>返回行数</returns>
public int ExecuteScalar(string cmdText, CommandType ct)
{
    int i;
    try
    {
        cmd = new SqlCommand(cmdText, getconn());
        //cmd访问类型
        cmd.CommandType = ct;
        //返回受影响行数
        i = (int)cmd.ExecuteScalar();
    }
    catch (Exception ex)
    {

        throw ex;
    }
    finally