其他分享
首页 > 其他分享> > ADO.Net笔记整理(一)

ADO.Net笔记整理(一)

作者:互联网

几次装机,Notes已烟消云散,近日因为Node.js死活搞不定,无奈装机,备份好的东东,没想到磁盘扇区出现异常,可能是PE启动盘的病毒,只好将磁盘全部重新分区,恢复数据也懒得恢复了,日积月累关乎将来的开发效率,还好处于懵懂阶段。笔记一大堆,记忆力又不怎么好,所以web+log=blog;

SqlHelper类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{

/// <summary>
/// SqlHelper类 by zy 2016-3-11
/// </summary>
public  class SqlHelper
{
     //如果项目中只连接了一个数据库 那么可以在此定义一个固定的连接字符串

​    public static string ConnectionStr { get; } =
​        ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

​    public static string ConnStr()
​    {
​        return ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
​    }
​    #region ExecuteNonQuery

​    /// <summary>
​    /// 执行sql语句 返回受影响行数
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="cmdType">command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();

​        using (SqlConnection conn = new SqlConnection(connectionString))
​        {
​            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
​            int val = cmd.ExecuteNonQuery();
​            cmd.Parameters.Clear();
​            return val;
​        }
​    }

​    /// <summary>
​    /// 执行sql语句 返回当前插入的记录id(其实是ExecuteScalar转换成id返回)
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="cmdType">command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="returnID">返回id</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
​        out int returnID, params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();

​        using (SqlConnection conn = new SqlConnection(connectionString))
​        {
​            returnID = 0;
​            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
​            returnID = (Int32) cmd.ExecuteScalar();
​            cmd.Parameters.Clear();
​            return returnID;
​        }
​    }

​    /// <summary>
​    /// 执行sql语句 返回受影响行数(数据库连接SqlConnection)
​    /// </summary>
​    /// <param name="connection">SqlConnection连接</param>
​    /// <param name="cmdType">command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();

​        PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
​        int val = cmd.ExecuteNonQuery();
​        cmd.Parameters.Clear();
​        return val;
​    }

​    /// <summary>
​    /// 执行sql语句 返回受影响行数(事务SqlTransaction)
​    /// </summary>
​    /// <param name="trans">SqlTransaction事务</param>
​    /// <param name="cmdType">command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();
​        PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
​        int val = cmd.ExecuteNonQuery();
​        cmd.Parameters.Clear();
​        return val;
​    }

​    #endregion

​    #region ExecuteScalar

​    /// <summary>
​    /// 执行sql语句 返回结果集第一行第一列
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="cmdType">Command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();

​        using (SqlConnection connection = new SqlConnection(connectionString))
​        {
​            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
​            object val = cmd.ExecuteScalar();
​            cmd.Parameters.Clear();
​            return val;
​        }
​    }

​    /// <summary>
​    /// 执行sql语句 返回结果集第一行第一列(事务)
​    /// </summary>
​    /// <param name="trans">SqlTransaction事务</param>
​    /// <param name="cmdType">Command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();
​        PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
​        object val = cmd.ExecuteScalar();
​        cmd.Parameters.Clear();
​        return val;
​    }

​    /// <summary>
​    /// 执行sql语句 返回结果集第一行第一列(数据库连接)
​    /// </summary>
​    /// <param name="connection">SqlConnection连接</param>
​    /// <param name="cmdType">Command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();

​        PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
​        object val = cmd.ExecuteScalar();
​        cmd.Parameters.Clear();
​        return val;
​    }

​    #endregion

​    #region ExecuteReader

​    /// <summary>
​    /// 执行sql语句 返回reader
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="cmdType">command类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();
​        SqlConnection conn = new SqlConnection(connectionString);
​        try
​        {
​            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
​            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
​            cmd.Parameters.Clear();
​            return rdr;
​        }
​        catch
​        {
​            conn.Close();
​            throw;
​        }
​    }

​    #endregion

​    #region DataSet

​    /// <summary>
​    /// 执行sql语句 返回DataSet结果集(无参数)
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="commandType">command类型</param>
​    /// <param name="commandText">sql语句</param>
​    /// <returns></returns>
​    public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
​    {
​        return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[]) null);
​    }

​    /// <summary>
​    /// 执行sql语句 返回DataSet结果集
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="commandType">command类型</param>
​    /// <param name="commandText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText,
​        params SqlParameter[] commandParameters)
​    {
​        //创建数据库连接 完成后dispose
​        using (SqlConnection cn = new SqlConnection(connectionString))
​        {
​            cn.Open();

​            //根据数据库连接 调用具体方法
​            return ExecuteDataset(cn, commandType, commandText, commandParameters);
​        }
​    }

​    /// <summary>
​    /// 执行sql语句 返回DataSet结果集(数据库连接SqlConnection)
​    /// </summary>
​    /// <param name="connection">SqlConnection连接</param>
​    /// <param name="commandType">command类型</param>
​    /// <param name="commandText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText,
​        params SqlParameter[] commandParameters)
​    {
​        //创建command
​        SqlCommand cmd = new SqlCommand();
​        //准备command对象
​        PrepareCommand(cmd, connection, (SqlTransaction) null, commandType, commandText, commandParameters);

​        //创建SqlDataAdapter和DataSet
​        SqlDataAdapter da = new SqlDataAdapter(cmd);
​        DataSet ds = new DataSet();

​        //把结果集装进dataset
​        da.Fill(ds);

​        //返回
​        return ds;
​    }

​    #endregion DataSet

​    #region DataTable

​    /// <summary>
​    /// 执行sql语句 返回DataTable结果集(无参数)
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="commandType">command类型</param>
​    /// <param name="commandText">sql语句</param>
​    /// <returns></returns>
​    public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText)
​    {
​        return ExecuteDataTable(connectionString, commandType, commandText, (SqlParameter[])null);
​    }

​    /// <summary>
​    /// 执行sql语句 返回DataTable结果集
​    /// </summary>
​    /// <param name="connectionString">连接字符串</param>
​    /// <param name="commandType">command类型</param>
​    /// <param name="commandText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText,
​        params SqlParameter[] commandParameters)
​    {
​        using (SqlConnection cn = new SqlConnection(connectionString))
​        {
​            cn.Open();
​            return ExecuteDataTable(cn, commandType, commandText, commandParameters);
​        }
​    }

​    /// <summary>
​    /// 执行sql语句 返回DataTable结果集
​    /// </summary>
​    /// <param name="connection">SqlConnection连接</param>
​    /// <param name="commandType">command类型</param>
​    /// <param name="commandText">sql语句</param>
​    /// <param name="commandParameters">参数</param>
​    /// <returns></returns>
​    public static DataTable ExecuteDataTable(SqlConnection connection, CommandType commandType, string commandText,
​        params SqlParameter[] commandParameters)
​    {
​        SqlCommand cmd = new SqlCommand();
​        PrepareCommand(cmd, connection, (SqlTransaction) null, commandType, commandText, commandParameters);

​        SqlDataAdapter da = new SqlDataAdapter(cmd);
​        DataTable dt = new DataTable();

​        da.Fill(dt);

​        return dt;
​    }

​    #endregion DataTable

​    #region PrepareCommand

​    /// <summary>
​    /// 准备Command对象
​    /// </summary>
​    /// <param name="cmd">SqlCommand 对象</param>
​    /// <param name="conn">SqlConnection 对象</param>
​    /// <param name="trans">SqlTransaction 对象</param>
​    /// <param name="cmdType">Command 类型</param>
​    /// <param name="cmdText">sql语句</param>
​    /// <param name="cmdParms">参数</param>
​    private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans,
​        CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
​    {
​        //打开SqlConnection连接
​        if (conn.State != ConnectionState.Open)
​            conn.Open();
​        //给Command的Connection CommandText设置值
​        cmd.Connection = conn;
​        cmd.CommandText = cmdText;
​        //如果是事务 设置事务
​        if (trans != null)
​            cmd.Transaction = trans;
​        //Command类型
​        cmd.CommandType = cmdType;
​        //Command参数
​        if (cmdParms != null)
​        {
​            foreach (SqlParameter parm in cmdParms)
​            {
​                //在此可以操作参数 比如把参数值里的英文逗号全部改为中文逗号 或者其他操作
​                switch (parm.SqlDbType)
​                {
​                    case SqlDbType.Char:
​                    case SqlDbType.NChar:
​                    case SqlDbType.NText:
​                    case SqlDbType.NVarChar:
​                    case SqlDbType.Text:
​                    case SqlDbType.VarChar:
​                        if (parm.Value != null && parm.Value != DBNull.Value && parm.ParameterName != "@SQLClause")
​                        {
​                            string tmp = parm.Value.ToString();
​                            tmp = tmp.Replace(",", ",");
​                            tmp = tmp.Replace("'", "'");
​                            parm.Value = tmp;
​                        }
​                        break;
​                }


                 //把参数添加到Command中的Parameters中
                 cmd.Parameters.Add(parm);
             }
         }
     }

   #endregion
}

}

原文地址不得,忘记哪里CV的了。

标签:语句,string,cmd,SqlConnection,笔记,sql,ADO,Net,commandParameters
来源: https://www.cnblogs.com/fenqinearl/p/10662156.html