C#连接MySQL数据库的方法步骤

  using MySql.Data.MySqlClient;

  using System;

  using System.Collections.Generic;

  using System.Data;

  using System.Linq;

  using System.Text;

  using System.Threading.Tasks;

  namespace CSharpConnectMySQL

  {

  public class MySqlHelper

  {

  private static string connstr = "server=127.0.0.1;database=shop;username=root;password=123456;";

  #region 执行查询语句,返回MySqlDataReader

  ///

  /// 执行查询语句,返回MySqlDataReader

  ///

  ///

  ///

  public static MySqlDataReader ExecuteReader(string sqlString)

  {

  MySqlConnection connection = new MySqlConnection(connstr);

  MySqlCommand cmd = new MySqlCommand(sqlString, connection);

  MySqlDataReader myReader = null;

  try

  {

  connection.Open();

  myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

  return myReader;

  }

  catch (System.Data.SqlClient.SqlException e)

  {

  connection.Close();

  throw new Exception(e.Message);

  }

  finally

  {

  if (myReader == null)

  {

  cmd.Dispose();

  connection.Close();

  }

  }

  }

  #endregion

  #region 执行带参数的查询语句,返回 MySqlDataReader

  ///

  /// 执行带参数的查询语句,返回MySqlDataReader

  ///

  ///

  ///

  ///

  public static MySqlDataReader ExecuteReader(string sqlString, params MySqlParameter[] cmdParms)

  {

  MySqlConnection connection = new MySqlConnection(connstr);

  MySqlCommand cmd = new MySqlCommand();

  MySqlDataReader myReader = null;

  try

  {

  PrepareCommand(cmd, connection, null, sqlString, cmdParms);

  myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

  cmd.Parameters.Clear();

  return myReader;

  }

  catch (System.Data.SqlClient.SqlException e)

  {

  connection.Close();

  throw new Exception(e.Message);

  }

  finally

  {

  if (myReader == null)

  {

  cmd.Dispose();

  connection.Close();

  }

  }

  }

  #endregion

  #region 执行sql语句,返回执行行数

  ///

  /// 执行sql语句,返回执行行数

  ///

  ///

  ///

  public static int ExecuteSql(string sql)

  {

  using (MySqlConnection conn = new MySqlConnection(connstr))

  {

  using (MySqlCommand cmd = new MySqlCommand(sql, conn))

  {

  try

  {

  conn.Open();

  int rows = cmd.ExecuteNonQuery();

  return rows;

  }

  catch (MySql.Data.MySqlClient.MySqlException e)

  {

  conn.Close();

  //throw e;

  Console.WriteLine(e.Message);

  }

  finally

  {

  cmd.Dispose();

  conn.Close();

  }

  }

  }

  return -1;

  }

  #endregion

  #region 执行带参数的sql语句,并返回执行行数

  ///

  /// 执行带参数的sql语句,并返回执行行数

  ///

  ///

  ///

  ///

  public static int ExecuteSql(string sqlString, params MySqlParameter[] cmdParms)

  {

  using (MySqlConnection connection = new MySqlConnection(connstr))

  {

  using (MySqlCommand cmd = new MySqlCommand())

  {

  try

  {

  PrepareCommand(cmd, connection, null, sqlString, cmdParms);

  int rows = cmd.ExecuteNonQuery();

  cmd.Parameters.Clear();

  return rows;

  }

  catch (System.Data.SqlClient.SqlException E)

  {

  throw new Exception(E.Message);

  }

  finally

  {

  cmd.Dispose();

  connection.Close();

  }

  }

  }

  }

  #endregion

  #region 执行查询语句,返回DataSet

  ///

  /// 执行查询语句,返回DataSet

  ///

  ///

  ///

  public static DataSet GetDataSet(string sql)

  {

  using (MySqlConnection conn = new MySqlConnection(connstr))

  {

  DataSet ds = new DataSet();

  try

  {

  conn.Open();

  MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn);

  DataAdapter.Fill(ds);

  }

  catch (Exception ex)

  {

  //throw ex;

  Console.WriteLine(ex.Message);

  }

  finally

  {

  conn.Close();

  }

  return ds;

  }

  }

  #endregion

  #region 执行带参数的查询语句,返回DataSet

  ///

  /// 执行带参数的查询语句,返回DataSet

  ///

  ///

  ///

  ///

  public static DataSet GetDataSet(string sqlString, params MySqlParameter[] cmdParms)

  {

  using (MySqlConnection connection = new MySqlConnection(connstr))

  {

  MySqlCommand cmd = new MySqlCommand();

  PrepareCommand(cmd, connection, null, sqlString, cmdParms);

  using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))

  {

  DataSet ds = new DataSet();

  try

  {

  da.Fill(ds, "ds");

  cmd.Parameters.Clear();

  }

  catch (System.Data.SqlClient.SqlException ex)

  {

  throw new Exception(ex.Message);

  }

  finally

  {

  cmd.Dispose();

  connection.Close();

  }

  return ds;

  }

  }

  }

  #endregion

  #region 执行带参数的sql语句,并返回 object

  ///

  /// 执行带参数的sql语句,并返回object

  ///

  ///

  ///

  ///

  public static object GetSingle(string sqlString, params MySqlParameter[] cmdParms)

  {

  using (MySqlConnection connection = new MySqlConnection(connstr))

  {

  using (MySqlCommand cmd = new MySqlCommand())

  {

  try

  {

  PrepareCommand(cmd, connection, null, sqlString, cmdParms);

  object obj = cmd.ExecuteScalar();

  cmd.Parameters.Clear();

  if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))

  {

  return null;

  }

  else

  {

  return obj;

  }

  }

  catch (System.Data.SqlClient.SqlException e)

  {

  throw new Exception(e.Message);

  }

  finally

  {

  cmd.Dispose();

  connection.Close();

  }

  }

  }

  }

  #endregion

  ///

  /// 执行存储过程,返回数据集

  ///

  /// 存储过程名

  /// 存储过程参数

  /// DataSet

  public static DataSet RunProcedureForDataSet(string storedProcName, IDataParameter[] parameters)

  {

  using (MySqlConnection connection = new MySqlConnection(connstr))

  {

  DataSet dataSet = new DataSet();

  connection.Open();

  MySqlDataAdapter sqlDA = new MySqlDataAdapter();

  sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);

  sqlDA.Fill(dataSet);

  connection.Close();

  return dataSet;

  }

  }

  ///

  /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)

  ///

  /// 数据库连接

  /// 存储过程名

  /// 存储过程参数

  /// SqlCommand

  private static MySqlCommand BuildQueryCommand(MySqlConnection connection, string storedProcName,

  IDataParameter[] parameters)

  {

  MySqlCommand command = new MySqlCommand(storedProcName, connection);

  command.CommandType = CommandType.StoredProcedure;

  foreach (MySqlParameter parameter in parameters)

  {

  command.Parameters.Add(parameter);

  }

  return command;

  }

  #region 装载MySqlCommand对象

  ///

  /// 装载MySqlCommand对象

  ///

  private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, 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 = CommandType.Text; //cmdType;

  if (cmdParms != null)

  {

  foreach (MySqlParameter parm in cmdParms)

  {

  cmd.Parameters.Add(parm);

  }

  }

  }

  #endregion

  }

  }