• 数据连接类 这里采用mysql


    数据库通用操作类,自己参照几个通用类改写的,用起来还是蛮不错。。。。  这里用的mysql 要是其他数据库自行更改下就好

     public class MySqlHelper
        {
            public static string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ToString();
    
            #region 通用方法
    
            /// <summary>
            /// 创建一个连接对象
            /// </summary>
            /// <param name="connectionString">数据库连接字符串</param>
            /// <returns></returns>
            public static MySqlConnection CreateConnection(string connectionString)
            {
                return new MySqlConnection(connectionString);
            }
    
            /// <summary>
            /// 关闭一个数据库对象
            /// </summary>
            /// <param name="connection">数据库对象</param>
            public static void CloseConnection(MySqlConnection connection)
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
    
            /// <summary>
            /// 打开一个数据库对象
            /// </summary>
            /// <param name="connection">数据库对象</param>
            public static void OpenConnection(MySqlConnection connection)
            {
                if (connection != null)
                {
                    connection.Open();
                }
            }
            #endregion
    
            #region 执行数据库命令,返回最大自增列id
    
            /// <summary>
            /// 执行数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回受影响的行数</returns>
            public static object ExecuteScalar(MySqlTransaction transaction, string commandText)
            {
                return ExecuteScalar(transaction, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="connection">数据库连接对象</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回受影响的行数</returns>
            public static object ExecuteScalar(MySqlConnection connection, string commandText)
            {
                return ExecuteScalar(connection, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回首行首列 最后插入的自增列id
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回受影响的行数</returns>
            private static object ExecuteScalar(MySqlTransaction transaction, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    try
                    {
                        cmd.Connection = transaction.Connection;
                        cmd.Transaction = transaction;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (commandParameters != null)
                        {
                            cmd.Parameters.AddRange(commandParameters);
                        }
                        return cmd.ExecuteScalar();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                    }
                }
            }
    
            /// <summary>
            /// 执行数据库命令,返回首行首列 最后插入的自增列id
            /// </summary>
            /// <param name="connection">数据库连接对象</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回受影响的行数</returns>
            private static object ExecuteScalar(MySqlConnection connection, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    try
                    {
                        cmd.Connection = connection;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (commandParameters != null)
                        {
                            cmd.Parameters.AddRange(commandParameters);
                        }
                        return cmd.ExecuteScalar();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                    }
                }
            }
            #endregion
    
            #region  执行数据库命令,返回受影响的行数
    
            /// <summary>
            /// 执行数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回受影响的行数</returns>
            public static int ExecuteNonQuery(MySqlConnection connection, string commandText)
            {
                return ExecuteNonQuery(connection, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回受影响的行数</returns>
            public static int ExecuteNonQuery(MySqlTransaction transaction, string commandText)
            {
                return ExecuteNonQuery(transaction, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行存储过程数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回受影响的行数</returns>
            public static int ExecuteProcedureNonQuery(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteNonQuery(connection, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行存储过程数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回受影响的行数</returns>
            public static int ExecuteProcedureNonQuery(MySqlTransaction transaction, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteNonQuery(transaction, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回受影响的行数</returns>
            private static int ExecuteNonQuery(MySqlConnection connection, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    try
                    {
                        cmd.Connection = connection;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (commandParameters != null)
                        {
                            cmd.Parameters.AddRange(commandParameters);
                        }
                        return cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("错误描述:{0}{1}", ex.Message, Environment.NewLine));
                    }
                }
            }
    
            /// <summary>
            /// 执行数据库命令,返回受影响的行数
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回受影响的行数</returns>
            private static int ExecuteNonQuery(MySqlTransaction transaction, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    try
                    {
                        cmd.Connection = transaction.Connection;
                        cmd.Transaction = transaction;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (commandParameters != null)
                        {
                            cmd.Parameters.AddRange(commandParameters);
                        }
                        return cmd.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                    }
                }
            }
    
            #endregion 执行数据库命令,返回受影响的行数
    
            #region 执行数据库命令,返回一个数据表
    
            /// <summary>
            /// 执行数据库命令,返回一个数据表
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回一个数据表</returns>
            public static DataTable ExecuteDataTable(MySqlConnection connection, string commandText)
            {
                return ExecuteDataTable(connection, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据表
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回一个数据表</returns>
            public static DataTable ExecuteDataTable(MySqlTransaction transaction, string commandText)
            {
                return ExecuteDataTable(transaction, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行存储过程数据库命令,返回一个数据表
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据表</returns>
            public static DataTable ExecuteProcedureDataTable(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteDataTable(connection, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行存储过程数据库命令,返回一个数据表
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据表</returns>
            public static DataTable ExecuteProcedurDataTable(MySqlTransaction transaction, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteDataTable(transaction, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据表
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据表</returns>
            private static DataTable ExecuteDataTable(MySqlConnection connection, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = connection;
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    if (commandParameters != null)
                    {
                        cmd.Parameters.AddRange(commandParameters);
                    }
    
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        try
                        {
                            da.Fill(dt);
                            return dt;
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                        }
                    }
                }
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据表
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据表</returns>
            private static DataTable ExecuteDataTable(MySqlTransaction transaction, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = transaction.Connection;
                    cmd.Transaction = transaction;
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    if (commandParameters != null)
                    {
                        cmd.Parameters.AddRange(commandParameters);
                    }
    
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        try
                        {
                            da.Fill(dt);
                            return dt;
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                        }
                    }
                }
            }
    
            #endregion 执行数据库命令,返回一个数据表
    
            #region 执行数据库命令,返回一个数据集
    
            /// <summary>
            /// 执行数据库命令,返回一个数据集
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回一个数据集</returns>
            public static DataSet ExecuteDataset(MySqlConnection connection, string commandText)
            {
                return ExecuteDataset(connection, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据集
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回一个数据集</returns>
            public static DataSet ExecuteDataset(MySqlTransaction transaction, string commandText)
            {
                return ExecuteDataset(transaction, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据集
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据集</returns>
            public static DataSet ExecuteDataset(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteDataset(connection, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据集
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据集</returns>
            public static DataSet ExecuteDataset(MySqlTransaction transaction, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteDataset(transaction, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据集
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据集</returns>
            private static DataSet ExecuteDataset(MySqlConnection connection, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = connection;
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    if (commandParameters != null)
                    {
                        cmd.Parameters.AddRange(commandParameters);
                    }
    
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        try
                        {
                            da.Fill(ds);
                            return ds;
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                        }
                    }
                }
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据集
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据集</returns>
            private static DataSet ExecuteDataset(MySqlTransaction transaction, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Transaction = transaction;
                    cmd.Connection = transaction.Connection;
                    cmd.CommandText = commandText;
                    cmd.CommandType = commandType;
                    if (commandParameters != null)
                    {
                        cmd.Parameters.AddRange(commandParameters);
                    }
    
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        try
                        {
                            da.Fill(ds);
                            return ds;
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                        }
                    }
                }
            }
    
            #endregion 执行数据库命令,返回一个数据集
    
            #region 执行数据库命令,返回一个数据读取器
    
            /// <summary>
            /// 执行数据库命令,返回一个数据读取器
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回一个数据读取器</returns>
            public static MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText)
            {
                return ExecuteReader(connection, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据读取器
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <returns>返回一个数据读取器</returns>
            public static MySqlDataReader ExecuteReader(MySqlTransaction transaction, string commandText)
            {
                return ExecuteReader(transaction, commandText, CommandType.Text, null);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据读取器
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据读取器</returns>
            public static MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteReader(connection, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据读取器
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据读取器</returns>
            public static MySqlDataReader ExecuteReader(MySqlTransaction transaction, string commandText, params MySqlParameter[] commandParameters)
            {
                return ExecuteReader(transaction, commandText, CommandType.StoredProcedure, commandParameters);
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据读取器
            /// </summary>
            /// <param name="connection">数据库连接</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据读取器</returns>
            private static MySqlDataReader ExecuteReader(MySqlConnection connection, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    try
                    {
                        cmd.Connection = connection;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (commandParameters != null)
                        {
                            cmd.Parameters.AddRange(commandParameters);
                        }
    
                        return cmd.ExecuteReader();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                    }
                }
            }
    
            /// <summary>
            /// 执行数据库命令,返回一个数据读取器
            /// </summary>
            /// <param name="transaction">数据库连接上的事务</param>
            /// <param name="commandType">数据库命令的类型</param>
            /// <param name="commandText">数据库命令的文本</param>
            /// <param name="commandParameters">数据库命令的参数</param>
            /// <returns>返回一个数据读取器</returns>
            private static MySqlDataReader ExecuteReader(MySqlTransaction transaction, string commandText, CommandType commandType, params MySqlParameter[] commandParameters)
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    try
                    {
                        cmd.Transaction = transaction;
                        cmd.Connection = transaction.Connection;
                        cmd.CommandText = commandText;
                        cmd.CommandType = commandType;
                        if (commandParameters != null)
                        {
                            cmd.Parameters.AddRange(commandParameters);
                        }
    
                        return cmd.ExecuteReader();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(string.Format("错误描述:{0}{1}", e.Message, Environment.NewLine));
                    }
                }
            }
    
            #endregion 执行数据库命令,返回一个数据读取器
        }
  • 相关阅读:
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    团 队 作 业 ———— 随 堂 小 测
    Alpha 冲刺 (5/10)
    Alpha 冲刺 (4/10)
    Beta冲刺博客汇总(麻瓜制造者)
    Beta冲刺(3/5)(麻瓜制造者)
    快速搭建一个Express工程骨架
    个人作业——软件产品案例分析
  • 原文地址:https://www.cnblogs.com/HelloXZ/p/3803977.html
Copyright © 2020-2023  润新知