/// <summary>
/// MySqlHelper操作类
/// </summary>
public sealed partial class MySQLHelper
{
public static readonly string DBConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[""].ToString();
/// <summary>
/// 批量操作每批次记录数
/// </summary>
public static int BatchSize = ; /// <summary>
/// 超时时间
/// </summary>
public static int CommandTimeOut = ; /// <summary>
///初始化MySqlHelper实例
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
public MySQLHelper(string connectionString)
{
this.ConnectionString = connectionString;
} /// <summary>
/// 数据库连接字符串
/// </summary>
public string ConnectionString { get; set; } #region 实例方法 #region ExecuteNonQuery /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
public int ExecuteNonQuery(string commandText, params MySqlParameter[] parms)
{
return ExecuteNonQuery(ConnectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
public int ExecuteNonQuery(CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteNonQuery(ConnectionString, commandType, commandText, parms);
} #endregion ExecuteNonQuery #region ExecuteScalar /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <typeparam name="T">返回对象类型</typeparam>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public T ExecuteScalar<T>(string commandText, params MySqlParameter[] parms)
{
return ExecuteScalar<T>(ConnectionString, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public object ExecuteScalar(string commandText, params MySqlParameter[] parms)
{
return ExecuteScalar(ConnectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public object ExecuteScalar(CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteScalar(ConnectionString, commandType, commandText, parms);
} #endregion ExecuteScalar #region ExecuteDataReader /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private MySqlDataReader ExecuteDataReader(string commandText, params MySqlParameter[] parms)
{
return ExecuteDataReader(ConnectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private MySqlDataReader ExecuteDataReader(CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataReader(ConnectionString, commandType, commandText, parms);
}
#endregion #region ExecuteDataRow /// <summary>
/// 执行SQL语句,返回结果集中的第一行
/// </summary>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行</returns>
public DataRow ExecuteDataRow(string commandText, params MySqlParameter[] parms)
{
return ExecuteDataRow(ConnectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行
/// </summary>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行</returns>
public DataRow ExecuteDataRow(CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataRow(ConnectionString, commandType, commandText, parms);
} #endregion ExecuteDataRow #region ExecuteDataTable /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一个数据表</returns>
public DataTable ExecuteDataTable(string commandText, params MySqlParameter[] parms)
{
return ExecuteDataTable(ConnectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一个数据表</returns>
public DataTable ExecuteDataTable(CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(ConnectionString, commandType, commandText, parms).Tables[];
} #endregion ExecuteDataTable #region ExecuteDataSet /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
public DataSet ExecuteDataSet(string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(ConnectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
public DataSet ExecuteDataSet(CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(ConnectionString, commandType, commandText, parms);
} #endregion ExecuteDataSet #region 批量操作 /// <summary>
/// 使用MySqlDataAdapter批量更新数据
/// </summary>
/// <param name="table">数据表</param>
public void BatchUpdate(DataTable table)
{
BatchUpdate(ConnectionString, table);
} /// <summary>
///大批量数据插入,返回成功插入行数
/// </summary>
/// <param name="table">数据表</param>
/// <returns>返回成功插入行数</returns>
public int BulkInsert(DataTable table)
{
return BulkInsert(ConnectionString, table);
} #endregion 批量操作 #endregion 实例方法 #region 静态方法 private static void PrepareCommand(MySqlCommand command, MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, MySqlParameter[] parms)
{
if (connection.State != ConnectionState.Open) connection.Open(); command.Connection = connection;
command.CommandTimeout = CommandTimeOut;
// 设置命令文本(存储过程名或SQL语句)
command.CommandText = commandText;
// 分配事务
if (transaction != null)
{
command.Transaction = transaction;
}
// 设置命令类型.
command.CommandType = commandType;
if (parms != null && parms.Length > )
{
//预处理MySqlParameter参数数组,将为NULL的参数赋值为DBNull.Value;
foreach (MySqlParameter parameter in parms)
{
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
}
command.Parameters.AddRange(parms);
}
} #region ExecuteNonQuery /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, string commandText, params MySqlParameter[] parms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
return ExecuteNonQuery(connection, CommandType.Text, commandText, parms);
}
} /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
return ExecuteNonQuery(connection, commandType, commandText, parms);
}
} /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteNonQuery(connection, null, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
public static int ExecuteNonQuery(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteNonQuery(transaction.Connection, transaction, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回影响的行数
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回影响的行数</returns>
private static int ExecuteNonQuery(MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, connection, transaction, commandType, commandText, parms);
int retval = command.ExecuteNonQuery();
command.Parameters.Clear();
return retval;
} #endregion ExecuteNonQuery #region ExecuteScalar /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <typeparam name="T">返回对象类型</typeparam>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static T ExecuteScalar<T>(string connectionString, string commandText, params MySqlParameter[] parms)
{
object result = ExecuteScalar(connectionString, commandText, parms);
if (result != null)
{
return (T)Convert.ChangeType(result, typeof(T)); ;
}
return default(T);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(string connectionString, string commandText, params MySqlParameter[] parms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
return ExecuteScalar(connection, CommandType.Text, commandText, parms);
}
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
return ExecuteScalar(connection, commandType, commandText, parms);
}
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteScalar(connection, null, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
public static object ExecuteScalar(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteScalar(transaction.Connection, transaction, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行第一列
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一行第一列</returns>
private static object ExecuteScalar(MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, connection, transaction, commandType, commandText, parms);
object retval = command.ExecuteScalar();
command.Parameters.Clear();
return retval;
} #endregion ExecuteScalar #region ExecuteDataReader /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private static MySqlDataReader ExecuteDataReader(string connectionString, string commandText, params MySqlParameter[] parms)
{
MySqlConnection connection = new MySqlConnection(connectionString);
return ExecuteDataReader(connection, null, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private static MySqlDataReader ExecuteDataReader(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
MySqlConnection connection = new MySqlConnection(connectionString);
return ExecuteDataReader(connection, null, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private static MySqlDataReader ExecuteDataReader(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataReader(connection, null, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private static MySqlDataReader ExecuteDataReader(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataReader(transaction.Connection, transaction, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回只读数据集
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回只读数据集</returns>
private static MySqlDataReader ExecuteDataReader(MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, connection, transaction, commandType, commandText, parms);
return command.ExecuteReader(CommandBehavior.CloseConnection);
} #endregion #region ExecuteDataRow /// <summary>
/// 执行SQL语句,返回结果集中的第一行
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>,返回结果集中的第一行</returns>
public static DataRow ExecuteDataRow(string connectionString, string commandText, params MySqlParameter[] parms)
{
DataTable dt = ExecuteDataTable(connectionString, CommandType.Text, commandText, parms);
return dt.Rows.Count > ? dt.Rows[] : null;
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>,返回结果集中的第一行</returns>
public static DataRow ExecuteDataRow(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
DataTable dt = ExecuteDataTable(connectionString, commandType, commandText, parms);
return dt.Rows.Count > ? dt.Rows[] : null;
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>,返回结果集中的第一行</returns>
public static DataRow ExecuteDataRow(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
DataTable dt = ExecuteDataTable(connection, commandType, commandText, parms);
return dt.Rows.Count > ? dt.Rows[] : null;
} /// <summary>
/// 执行SQL语句,返回结果集中的第一行
/// </summary>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>,返回结果集中的第一行</returns>
public static DataRow ExecuteDataRow(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
DataTable dt = ExecuteDataTable(transaction, commandType, commandText, parms);
return dt.Rows.Count > ? dt.Rows[] : null;
} #endregion ExecuteDataRow #region ExecuteDataTable /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一个数据表</returns>
public static DataTable ExecuteDataTable(string connectionString, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(connectionString, CommandType.Text, commandText, parms).Tables[];
} /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一个数据表</returns>
public static DataTable ExecuteDataTable(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(connectionString, commandType, commandText, parms).Tables[];
} /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一个数据表</returns>
public static DataTable ExecuteDataTable(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(connection, commandType, commandText, parms).Tables[];
} /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集中的第一个数据表</returns>
public static DataTable ExecuteDataTable(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(transaction, commandType, commandText, parms).Tables[];
} /// <summary>
/// 执行SQL语句,返回结果集中的第一个数据表
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="tableName">数据表名称</param>
/// <returns>返回结果集中的第一个数据表</returns>
public static DataTable ExecuteEmptyDataTable(string connectionString, string tableName)
{
return ExecuteDataSet(connectionString, CommandType.Text, string.Format("select * from {0} where 1=-1", tableName)).Tables[];
} #endregion ExecuteDataTable #region ExecuteDataSet /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandText">SQL语句</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
public static DataSet ExecuteDataSet(string connectionString, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(connectionString, CommandType.Text, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
return ExecuteDataSet(connection, commandType, commandText, parms);
}
} /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
public static DataSet ExecuteDataSet(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(connection, null, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
public static DataSet ExecuteDataSet(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
return ExecuteDataSet(transaction.Connection, transaction, commandType, commandText, parms);
} /// <summary>
/// 执行SQL语句,返回结果集
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="transaction">事务</param>
/// <param name="commandType">命令类型(存储过程,命令文本, 其它.)</param>
/// <param name="commandText">SQL语句或存储过程名称</param>
/// <param name="parms">查询参数</param>
/// <returns>返回结果集</returns>
private static DataSet ExecuteDataSet(MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] parms)
{
MySqlCommand command = new MySqlCommand(); PrepareCommand(command, connection, transaction, commandType, commandText, parms);
MySqlDataAdapter adapter = new MySqlDataAdapter(command); DataSet ds = new DataSet();
adapter.Fill(ds);
if (commandText.IndexOf("@") > )
{
commandText = commandText.ToLower();
int index = commandText.IndexOf("where ");
if (index < )
{
index = commandText.IndexOf("\nwhere");
}
if (index > )
{
ds.ExtendedProperties.Add("SQL", commandText.Substring(, index - )); //将获取的语句保存在表的一个附属数组里,方便更新时生成CommandBuilder
}
else
{
ds.ExtendedProperties.Add("SQL", commandText); //将获取的语句保存在表的一个附属数组里,方便更新时生成CommandBuilder
}
}
else
{
ds.ExtendedProperties.Add("SQL", commandText); //将获取的语句保存在表的一个附属数组里,方便更新时生成CommandBuilder
} foreach (DataTable dt in ds.Tables)
{
dt.ExtendedProperties.Add("SQL", ds.ExtendedProperties["SQL"]);
} command.Parameters.Clear();
return ds;
} #endregion ExecuteDataSet #region 批量操作 /// <summary>
///使用MySqlDataAdapter批量更新数据
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="table">数据表</param>
public static void BatchUpdate(string connectionString, DataTable table)
{
MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand command = connection.CreateCommand();
command.CommandTimeout = CommandTimeOut;
command.CommandType = CommandType.Text;
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
MySqlCommandBuilder commandBulider = new MySqlCommandBuilder(adapter);
commandBulider.ConflictOption = ConflictOption.OverwriteChanges; MySqlTransaction transaction = null;
try
{
connection.Open();
transaction = connection.BeginTransaction();
//设置批量更新的每次处理条数
adapter.UpdateBatchSize = BatchSize;
//设置事物
adapter.SelectCommand.Transaction = transaction; if (table.ExtendedProperties["SQL"] != null)
{
adapter.SelectCommand.CommandText = table.ExtendedProperties["SQL"].ToString();
}
adapter.Update(table);
transaction.Commit();/////提交事务
}
catch (MySqlException ex)
{
if (transaction != null) transaction.Rollback();
throw ex;
}
finally
{
connection.Close();
connection.Dispose();
}
} /// <summary>
///大批量数据插入,返回成功插入行数
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="table">数据表</param>
/// <returns>返回成功插入行数</returns>
public static int BulkInsert(string connectionString, DataTable table)
{
if (string.IsNullOrEmpty(table.TableName)) throw new Exception("请给DataTable的TableName属性附上表名称");
if (table.Rows.Count == ) return ;
int insertCount = ;
string tmpPath = Path.GetTempFileName();
string csv = DataTableToCsv(table);
File.WriteAllText(tmpPath, csv);
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
MySqlTransaction tran = null;
try
{
conn.Open();
tran = conn.BeginTransaction();
MySqlBulkLoader bulk = new MySqlBulkLoader(conn)
{
FieldTerminator = ",",
FieldQuotationCharacter = '"',
EscapeCharacter = '"',
LineTerminator = "\r\n",
FileName = tmpPath,
NumberOfLinesToSkip = ,
TableName = table.TableName,
};
bulk.Columns.AddRange(table.Columns.Cast<DataColumn>().Select(colum => colum.ColumnName).ToList());
insertCount = bulk.Load();
tran.Commit();
}
catch (MySqlException ex)
{
if (tran != null) tran.Rollback();
throw ex;
}
}
File.Delete(tmpPath);
return insertCount;
} /// <summary>
///将DataTable转换为标准的CSV
/// </summary>
/// <param name="table">数据表</param>
/// <returns>返回标准的CSV</returns>
private static string DataTableToCsv(DataTable table)
{
//以半角逗号(即,)作分隔符,列为空也要表达其存在。
//列内容如存在半角逗号(即,)则用半角引号(即"")将该字段值包含起来。
//列内容如存在半角引号(即")则应替换成半角双引号("")转义,并用半角引号(即"")将该字段值包含起来。
StringBuilder sb = new StringBuilder();
DataColumn colum;
foreach (DataRow row in table.Rows)
{
for (int i = ; i < table.Columns.Count; i++)
{
colum = table.Columns[i];
if (i != ) sb.Append(",");
if (colum.DataType == typeof(string) && row[colum].ToString().Contains(","))
{
sb.Append("\"" + row[colum].ToString().Replace("\"", "\"\"") + "\"");
}
else sb.Append(row[colum].ToString());
}
sb.AppendLine();
} return sb.ToString();
} #endregion 批量操作 #endregion 静态方法
}

MySQL 帮助类 MySQLHelper的更多相关文章

  1. .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper

    .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper 參考演示样例代码,例如以下所看到的: /// <summary> /// MySql ...

  2. DELPHI XE MYSQL数据库操作类 MYSQLHELPER

    注: 无需odbc配置 {* * MySQL Helper v1.0 * 2015.6.19 * 说明: * 这是一个操作MySQL的类,该类必须和libmysql.dll,dbxmys.dll两个文 ...

  3. Mysql操作方法类

    帮助类: using System; using System.Collections.Generic; using System.Data; using System.Linq; using Sys ...

  4. ecshop的Mysql操作类

    摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...

  5. 深入理解php的MySQL连接类

    php的MySQL连接类.  后面几个show_databases和show_tables....等方法都用了一堆echo,好像一直不喜欢在类的方法里直接用输出语句,不过这也只是列举数据库和表名,构造 ...

  6. php+mysql分页类的入门实例

    php+mysql分页类的简单代码 时间:2016-02-25 06:16:26来源:网络 导读:php+mysql分页类的简单代码,二个php分页类代码,对商品进行分页展示,当前页面数,每个页面展示 ...

  7. C#操作MySQL的类

    C#操作MySQL的类 public class MySqlService { private static log4net.ILog logger = log4net.LogManager.GetL ...

  8. PHP:自己写的mysql操作类

    a{ font-weight: bold; display: block; text-align: center; color: #5887bf; font-size: 22px; } .conten ...

  9. 着重基础之—MySql Blob类型和Text类型

    着重基础之—MySql Blob类型和Text类型 在经历了几个Java项目后,遇到了一些问题,在解决问题中体会到基础需要不断的回顾与巩固. 最近做的项目中,提供给接口调用方数据同步接口,传输的数据格 ...

随机推荐

  1. Ubuntu和Win7双系统,ubuntu被删,重新启动之后显示,no such partition

    准备一张windows7的系统安装盘.从光盘启动电脑,在光盘启动完毕之后.按shift+F10,调出cmd命令终端,在终端输入:bootrec/fixmbr OK.重新启动之后就搞定了

  2. 赵雅智_Android的getResources()资源引用

    今天做一个Android的刮刮乐项目.里面用到非常多的地方用到了getResources. <span style="font-size:12px;"> // 获得图片 ...

  3. 使用heartbeat+monit实现主备双热备份系统

    一.使用背景 项目须要实现主备双热自己主动切换的功能,保证系统7*24小时不间断执行.现已有两台双网卡的IBM的server,为了不再添加成本採购独立外部存储设备和双机热备软件.採用了linux下开源 ...

  4. 10.11无法打开Xcode6.4的解决方法

    前言 mac升级到10.11版本号并安装Xcode7.0Beta之后,Dock中的Xcode6.3图标上出现一个禁止符号,打开提示到App store更新最新版本号,更新到6.4之后问题依然,还是提示 ...

  5. iOS开发——基础篇——iOS开发 Xcode8中遇到的问题及改动

      iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...

  6. TableLayout与MigLayout

    最近新接触的两个Layout,另外之前用的GridBagLayoutHelper以及最近听说的Qt for java的QCSS据说也不错, 只是Qt的跨平台需要单独发布,假如使用QT for java ...

  7. luogu 3951 小凯的疑惑

    noip2017 D1T1 小凯的疑惑 某zz选手没有看出这道结论题,同时写出了exgcd却不会用,只能打一个哈希表骗了30分 题目大意: 两个互质的正整数a和b,求一个最小的正整数使这个数无法表示为 ...

  8. Ado.net设计模式

    连接类 连接类有固定的使用模式,这是常用的乐观模式: using (var conn = new SqlConnection(connstr)) { conn.Open(); //执行各种数据库操作 ...

  9. Flask的多app应用,多线程如何体现

    一.多app应用 在一个py文件中创建多个Flask的app对象 from werkzeug.wsgi import DispatcherMiddleware from werkzeug.servin ...

  10. bzoj1999 (洛谷1099) 树网的核——dfs

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1999  https://www.luogu.org/problemnew/show/P109 ...