码迷,mamicode.com
首页 > 数据库 > 详细

SqlHelper

时间:2019-04-12 13:30:43      阅读:625      评论:0      收藏:0      [点我收藏+]

标签:ring   .com   adapter   获取   create   ctc   get   serve   sqlt   

 首先需要先配置连接字符串:

<connectionStrings>
    <add name="Sql" connectionString="server=数据库地址;uid=用户名;pwd=密码;database=数据库名" providerName="System.Data.SqlClient"/>
</connectionStrings>

SqlHepler类代码如下:

public static class SqlHelper
{
    /// <summary>
    /// 获取连接字符串
    /// </summary>
    /// <returns>连接字符串</returns>
    public static string GetSqlConnectionString()
    {
        return ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
    }

    /// <summary>
    /// 封装一个执行的sql 返回受影响的行数
    /// </summary>
    /// <param name="sqlText">执行的sql脚本</param>
    /// <param name="parameters">参数集合</param>
    /// <returns>受影响的行数</returns>
    public static int ExecuteNonQuery(string sqlText, params SqlParameter[] parameters)
    {
        using (SqlConnection conn = new SqlConnection(GetSqlConnectionString()))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = sqlText;
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteNonQuery();
            }
        }
    }

    /// <summary>
    /// 执行sql,返回查询结果中的第一行第一列的值
    /// </summary>
    /// <param name="sqlText">执行的sql脚本</param>
    /// <param name="parameters">参数集合</param>
    /// <returns>查询结果中的第一行第一列的值</returns>
    public static object ExecuteScalar(string sqlText, params SqlParameter[] parameters)
    {
        using (SqlConnection conn = new SqlConnection(GetSqlConnectionString()))
        {
            using (SqlCommand cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = sqlText;
                cmd.Parameters.AddRange(parameters);
                return cmd.ExecuteScalar();
            }
        }
    }

    /// <summary>
    /// 执行sql 返回一个DataTable
    /// </summary>
    /// <param name="sqlText">执行的sql脚本</param>
    /// <param name="parameters">参数集合</param>
    /// <returns>返回一个DataTable</returns>
    public static DataTable ExecuteDataTable(string sqlText, params SqlParameter[] parameters)
    {
        using (SqlDataAdapter adapter = new SqlDataAdapter(sqlText, GetSqlConnectionString()))
        {
            DataTable dt = new DataTable();
            adapter.SelectCommand.Parameters.AddRange(parameters);
            adapter.Fill(dt);
            return dt;
        }
    }

    /// <summary>
    /// 执行sql脚本
    /// </summary>
    /// <param name="sqlText">执行的sql脚本</param>
    /// <param name="parameters">参数集合</param>
    /// <returns>返回一个SqlDataReader</returns>
    public static SqlDataReader ExecuteReader(string sqlText, params SqlParameter[] parameters)
    {
        //SqlDataReader要求,它读取数据的时候有,它独占它的SqlConnection对象,而且SqlConnection必须是Open状态
        SqlConnection conn = new SqlConnection(GetSqlConnectionString());//不要释放连接,因为后面还需要连接打开状态
        SqlCommand cmd = conn.CreateCommand();
        conn.Open();
        cmd.CommandText = sqlText;
        cmd.Parameters.AddRange(parameters);
        //CommandBehavior.CloseConnection当SqlDataReader释放的时候,顺便把SqlConnection对象也释放掉
        return cmd.ExecuteReader(CommandBehavior.CloseConnection);
    }
}

 

SqlHelper

标签:ring   .com   adapter   获取   create   ctc   get   serve   sqlt   

原文地址:https://www.cnblogs.com/Gzu_zb/p/10695414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!