zoukankan      html  css  js  c++  java
  • C# SQL数据库的访问类

     /// <summary>
        /// SQL数据库的访问类
        /// </summary>
        public class SQLHelper
        {
            /// <summary>
            /// 执行非查询
            /// </summary>
            /// <param name="connectionString">连接字符串</param>
            /// <param name="cmdType">命令类型</param>
            /// <param name="cmdText">命令的文本</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>
            /// 执行非查询
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="cmdType">命令的类型</param>
            /// <param name="cmdText">命令的文本</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>
            /// 执行非查询
            /// </summary>
            /// <param name="trans">事务</param>
            /// <param name="cmdType">命令的类型</param>
            /// <param name="cmdText">命令的文本</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;
            }
    
            /// <summary>
            /// 执行查询
            /// </summary>
            /// <param name="connectionString">练级字符串</param>
            /// <param name="cmdType">命令的类型</param>
            /// <param name="cmdText">命令的文本</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;
                }
            }
    
            /// <summary>
            /// 执行查询
            /// </summary>
            /// <param name="connectionString">练级字符串</param>
            /// <param name="cmdType">命令的类型</param>
            /// <param name="cmdText">命令的文本</param>
            /// <param name="commandParameters">命令的参数数组</param>
            /// <returns>返回结果集</returns>
            public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
            {
                SqlCommand cmd = new SqlCommand();
                try
                {
                    PrepareCommand(cmd, transaction.Connection, transaction, cmdType, cmdText, commandParameters);
                    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.Default);
                    cmd.Parameters.Clear();
                    return rdr;
                }
                catch
                {                
                    throw;
                }
            }
    
            /// <summary>
            /// 读取数据
            /// </summary>
            /// <param name="connectionString">数据库连接字符串</param>
            /// <param name="cmdType">数据命令类型</param>
            /// <param name="cmdText">数据命令的文本</param>
            /// <param name="cmd">需要被执行的数据命令</param>
            /// <param name="commandParameters">数据命令的参数</param>
            /// <returns>返回影响的结果集行数</returns>
            public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, out SqlCommand cmd, params SqlParameter[] commandParameters)
            {
                cmd = new SqlCommand();
                SqlConnection conn = new SqlConnection(connectionString);
                try
                {
                    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                    SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    return rdr;
                }
                catch
                {
                    conn.Close();
                    throw;
                }
            }
            //匹配参数
            private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
            {
    
                if (conn.State != ConnectionState.Open)
                    conn.Open();
    
                cmd.Connection = conn;
                cmd.CommandText = cmdText;
    
                if (trans != null)
                    cmd.Transaction = trans;
    
                cmd.CommandType = cmdType;
    
                if (cmdParms != null)
                {
                    foreach (SqlParameter parm in cmdParms)
                        cmd.Parameters.Add(parm);
                }
            }
    
        }
  • 相关阅读:
    fedora29 安装mongodb 4.0,6问题记录
    乱花渐入迷人眼------从解决jqueryEasyUI上传插件提交ajax请求谈网页调试
    thinkphp5权限仿制
    基于tornado python pandas和bootstrap上传组件的mongodb数据添加工具
    tornado下pandas ndarray乱试
    一个tornado框架下的文件上传案例
    一例tornado框架下利用python panda对数据进行crud操作
    PHP 基础语法实例及注意事项
    转:github使用教程(重装系统后遇到问题该文章帮我解决了)
    ubuntu同步系统时间命令
  • 原文地址:https://www.cnblogs.com/haibing0107/p/6141242.html
Copyright © 2011-2022 走看看