159 lines
6.1 KiB
C#
159 lines
6.1 KiB
C#
|
using MySql.Data.MySqlClient;
|
|||
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data;
|
|||
|
using System.Linq;
|
|||
|
using System.Web;
|
|||
|
|
|||
|
namespace NGTools.Tools
|
|||
|
{
|
|||
|
public class MySQLHelper
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
///SQLHelper 的摘要说明
|
|||
|
/// </summary>
|
|||
|
//数据库连接字符串(注意:这里的“DBConnectionString”一定要与web.config文件中connectionStrings节点值一致)
|
|||
|
public static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
|
|||
|
// 用于缓存参数的HASH表
|
|||
|
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 准备执行一个命令
|
|||
|
/// </summary>
|
|||
|
/// <param name="cmd">sql命令</param>
|
|||
|
/// <param name="conn">OleDb连接</param>
|
|||
|
/// <param name="trans">OleDb事务</param>
|
|||
|
/// <param name="cmdType">命令类型例如 存储过程或者文本</param>
|
|||
|
/// <param name="cmdText">命令文本,例如:Select * from Products</param>
|
|||
|
/// <param name="cmdParms">执行命令的参数</param>
|
|||
|
private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] 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 (MySqlParameter parameter in cmdParms)
|
|||
|
{
|
|||
|
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
|
|||
|
(parameter.Value == null))
|
|||
|
{
|
|||
|
parameter.Value = DBNull.Value;
|
|||
|
}
|
|||
|
cmd.Parameters.Add(parameter);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
|
|||
|
/// </summary>
|
|||
|
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
|
|||
|
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
|
|||
|
/// <param name="commandParameters">执行命令所用参数的集合</param>
|
|||
|
/// <returns>执行命令所影响的行数</returns>
|
|||
|
/// <summary>
|
|||
|
/// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
|
|||
|
/// </summary>
|
|||
|
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
|
|||
|
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
|
|||
|
/// <param name="commandParameters">执行命令所用参数的集合</param>
|
|||
|
/// <returns>执行命令所影响的行数</returns>
|
|||
|
public static int ExecuteNonQuery(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
|
|||
|
{
|
|||
|
MySqlCommand cmd = new MySqlCommand();
|
|||
|
using (MySqlConnection conn = new MySqlConnection(connectionString))
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
|
|||
|
int val = cmd.ExecuteNonQuery();
|
|||
|
cmd.Parameters.Clear();
|
|||
|
return val;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 返回DataSet
|
|||
|
/// </summary>
|
|||
|
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
|
|||
|
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
|
|||
|
/// <param name="commandParameters">执行命令所用参数的集合</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static DataSet ExecuteDataSet(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
|
|||
|
{
|
|||
|
//创建一个MySqlCommand对象
|
|||
|
MySqlCommand cmd = new MySqlCommand();
|
|||
|
//创建一个MySqlConnection对象
|
|||
|
MySqlConnection conn = new MySqlConnection(connectionString);
|
|||
|
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
|
|||
|
try
|
|||
|
{
|
|||
|
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数
|
|||
|
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
|
|||
|
//调用 MySqlCommand 的 ExecuteReader 方法
|
|||
|
MySqlDataAdapter adapter = new MySqlDataAdapter();
|
|||
|
adapter.SelectCommand = cmd;
|
|||
|
DataSet ds = new DataSet();
|
|||
|
adapter.Fill(ds);
|
|||
|
//清除参数
|
|||
|
cmd.Parameters.Clear();
|
|||
|
conn.Close();
|
|||
|
return ds;
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
throw e;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 返回DataTable
|
|||
|
/// </summary>
|
|||
|
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
|
|||
|
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
|
|||
|
/// <param name="commandParameters">执行命令所用参数的集合</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static DataTable ExecuteDataTable(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
|
|||
|
{
|
|||
|
//创建一个MySqlCommand对象
|
|||
|
MySqlCommand cmd = new MySqlCommand();
|
|||
|
//创建一个MySqlConnection对象
|
|||
|
MySqlConnection conn = new MySqlConnection(connectionString);
|
|||
|
//在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
|
|||
|
try
|
|||
|
{
|
|||
|
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数
|
|||
|
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
|
|||
|
//调用 MySqlCommand 的 ExecuteReader 方法
|
|||
|
MySqlDataAdapter adapter = new MySqlDataAdapter();
|
|||
|
adapter.SelectCommand = cmd;
|
|||
|
DataTable dt = new DataTable();
|
|||
|
adapter.Fill(dt);
|
|||
|
//清除参数
|
|||
|
cmd.Parameters.Clear();
|
|||
|
conn.Close();
|
|||
|
return dt;
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|