MySQL Connector/Net for .net 4.0 驱动包
MySQL Connector/Net for .net 4.0 驱动包
http://mysql-connector-net-6.8.7.msi
附使用方法:
- using PW.Common;
- using PW.Model;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.Common;
- using System.Linq;
- using System.Text;
- namespace PW.Db
- {
- /// <summary>
- /// 数据访问基础类
- /// </summary>
- public class Sql
- {
- protected static string connectionString = "";
- protected static DbProviderFactory provider;
- public Sql()
- {
- using (var io = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
- {
- String xml = io.ReadToEnd();
- Config config = (Config)XmlUtil.Deserialize(typeof(Config), xml);
- connectionString = config.ConnectionString;
- provider = DbProviderFactories.GetFactory(config.ProviderName);
- }
- }
- private DbDataAdapter CreateDataAdapter(DbConnection conn)
- {
- if (conn is System.Data.SqlClient.SqlConnection)
- return new System.Data.SqlClient.SqlDataAdapter();
- if (conn is MySql.Data.MySqlClient.MySqlConnection)
- return new MySql.Data.MySqlClient.MySqlDataAdapter();
- throw new NotImplementedException();
- }
- #region 执行简单SQL语句
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- public int ExecuteSql(string SQLString)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- cmd.Connection = connection;
- cmd.CommandText = SQLString;
- try
- {
- connection.Open();
- int rows = cmd.ExecuteNonQuery();
- return rows;
- }
- catch (DbException E)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(E.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行多条SQL语句,实现数据库事务。
- /// </summary>
- /// <param name="SQLStringList">多条SQL语句</param>
- public void ExecuteSqlTran(ArrayList SQLStringList)
- {
- using (DbConnection conn = provider.CreateConnection())
- {
- conn.ConnectionString = connectionString;
- conn.Open();
- using (DbCommand cmd = provider.CreateCommand())
- {
- cmd.Connection = conn;
- using (DbTransaction tx = conn.BeginTransaction())
- {
- cmd.Transaction = tx;
- try
- {
- for (int n = 0; n < SQLStringList.Count; n++)
- {
- string strsql = SQLStringList[n].ToString();
- if (strsql.Trim().Length > 1)
- {
- cmd.CommandText = strsql;
- cmd.ExecuteNonQuery();
- }
- }
- tx.Commit();
- }
- catch (DbException ex)
- {
- tx.Rollback();
- conn.Close();
- conn.Dispose();
- throw ex;
- }
- }
- }
- }
- }
- /// <summary>
- /// 执行一条计算查询结果语句,返回查询结果(object)。
- /// </summary>
- /// <param name="SQLString">计算查询结果语句</param>
- /// <returns>查询结果(object)</returns>
- public object GetSingle(string SQLString)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- cmd.Connection = connection;
- cmd.CommandText = SQLString;
- try
- {
- connection.Open();
- object obj = cmd.ExecuteScalar();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (DbException e)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(e.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行查询语句,返回SqlDataReader
- /// </summary>
- /// <param name="strSQL">查询语句</param>
- /// <returns>SqlDataReader</returns>
- public DbDataReader ExecuteReader(string strSQL)
- {
- DbConnection connection = provider.CreateConnection();
- connection.ConnectionString = connectionString;
- DbCommand cmd = provider.CreateCommand();
- cmd.Connection = connection;
- cmd.CommandText = strSQL;
- try
- {
- connection.Open();
- DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- return myReader;
- }
- catch (System.Data.Common.DbException e)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(e.Message);
- }
- }
- /// <summary>
- /// 执行查询语句,返回DataSet
- /// </summary>
- /// <param name="SQLString">查询语句</param>
- /// <returns>DataSet</returns>
- public DataSet GetDataSet(string SQLString)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- cmd.Connection = connection;
- cmd.CommandText = SQLString;
- try
- {
- DataSet ds = new DataSet();
- DbDataAdapter adapter = provider.CreateDataAdapter();
- adapter.SelectCommand = cmd;
- adapter.Fill(ds, "ds");
- return ds;
- }
- catch (DbException ex)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(ex.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行查询语句,返回DataSet
- /// </summary>
- /// <param name="SQLString">查询语句</param>
- /// <returns>DataSet</returns>
- public DataTable GetDataTable(string SQLString)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- cmd.Connection = connection;
- cmd.CommandText = SQLString;
- try
- {
- DataSet ds = new DataSet();
- DbDataAdapter adapter = provider.CreateDataAdapter();
- //DbDataAdapter adapter = CreateDataAdapter(connection);//provider.CreateDataAdapter();
- adapter.SelectCommand = cmd;
- adapter.Fill(ds, "ds");
- return ds.Tables[0];
- }
- catch (DbException ex)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(ex.Message);
- }
- }
- }
- }
- #endregion
- #region 执行带参数的SQL语句
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- cmd.Connection = connection;
- cmd.CommandText = SQLString;
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- int rows = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return rows;
- }
- catch (DbException E)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(E.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行多条SQL语句,实现数据库事务。
- /// </summary>
- /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
- public void ExecuteSqlTran(Hashtable SQLStringList)
- {
- using (DbConnection conn = provider.CreateConnection())
- {
- conn.ConnectionString = connectionString;
- conn.Open();
- using (DbTransaction trans = conn.BeginTransaction())
- {
- using (DbCommand cmd = provider.CreateCommand())
- {
- try
- {
- //循环
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- string cmdText = myDE.Key.ToString();
- DbParameter[] cmdParms = (DbParameter[])myDE.Value;
- PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- }
- trans.Commit();
- }
- catch (DbException ex)
- {
- trans.Rollback();
- conn.Close();
- conn.Dispose();
- throw ex;
- }
- }
- }
- }
- }
- /// <summary>
- /// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
- /// </summary>
- /// <param name="SQLString">计算查询结果语句</param>
- /// <returns>查询结果(object)</returns>
- public object GetSingle(string SQLString, DbParameter[] cmdParms)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- 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 (DbException e)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(e.Message);
- }
- }
- }
- }
- /// <summary>
- /// 执行查询语句,返回SqlDataReader
- /// </summary>
- /// <param name="strSQL">查询语句</param>
- /// <returns>SqlDataReader</returns>
- public DbDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
- {
- DbConnection connection = provider.CreateConnection();
- connection.ConnectionString = connectionString;
- DbCommand cmd = provider.CreateCommand();
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return myReader;
- }
- catch (DbException e)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(e.Message);
- }
- }
- /// <summary>
- /// 执行查询语句,返回DataSet
- /// </summary>
- /// <param name="SQLString">查询语句</param>
- /// <returns>DataSet</returns>
- public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- using (DbCommand cmd = provider.CreateCommand())
- {
- using (DbDataAdapter da = provider.CreateDataAdapter())
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- da.SelectCommand = cmd;
- DataSet ds = new DataSet();
- try
- {
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- return ds;
- }
- catch (DbException ex)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(ex.Message);
- }
- }
- }
- }
- }
- private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, string cmdText, DbParameter[] 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 (DbParameter parm in cmdParms)
- {
- cmd.Parameters.Add(parm);
- }
- }
- }
- #endregion
- #region 存储过程操作
- /// <summary>
- /// 执行存储过程;
- /// </summary>
- /// <param name="storeProcName">存储过程名</param>
- /// <param name="parameters">所需要的参数</param>
- /// <returns>返回受影响的行数</returns>
- public int RunProcedureExecuteSql(string storeProcName, DbParameter[] parameters)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- DbCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
- int rows = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- connection.Close();
- return rows;
- }
- }
- /// <summary>
- /// 执行存储过程,返回首行首列的值
- /// </summary>
- /// <param name="storeProcName">存储过程名</param>
- /// <param name="parameters">存储过程参数</param>
- /// <returns>返回首行首列的值</returns>
- public Object RunProcedureGetSingle(string storeProcName, DbParameter[] parameters)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- try
- {
- DbCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
- object obj = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
- {
- return null;
- }
- else
- {
- return obj;
- }
- }
- catch (DbException e)
- {
- connection.Close();
- connection.Dispose();
- throw new Exception(e.Message);
- }
- }
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="storedProcName">存储过程名</param>
- /// <param name="parameters">存储过程参数</param>
- /// <returns>SqlDataReader</returns>
- public DbDataReader RunProcedureGetDataReader(string storedProcName, DbParameter[] parameters)
- {
- DbConnection connection = provider.CreateConnection();
- connection.ConnectionString = connectionString;
- DbDataReader returnReader;
- DbCommand cmd = BuildQueryCommand(connection, storedProcName, parameters);
- cmd.CommandType = CommandType.StoredProcedure;
- returnReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return returnReader;
- }
- /// <summary>
- /// 执行存储过程
- /// </summary>
- /// <param name="storedProcName">存储过程名</param>
- /// <param name="parameters">存储过程参数</param>
- /// <returns>DataSet</returns>
- public DataSet RunProcedureGetDataSet(string storedProcName, DbParameter[] parameters)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- DataSet dataSet = new DataSet();
- DbDataAdapter sqlDA = provider.CreateDataAdapter();
- sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
- sqlDA.Fill(dataSet);
- sqlDA.SelectCommand.Parameters.Clear();
- sqlDA.Dispose();
- return dataSet;
- }
- }
- /// <summary>
- /// 执行多个存储过程,实现数据库事务。
- /// </summary>
- /// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
- public bool RunProcedureTran(Hashtable SQLStringList)
- {
- using (DbConnection connection = provider.CreateConnection())
- {
- connection.ConnectionString = connectionString;
- connection.Open();
- using (DbTransaction trans = connection.BeginTransaction())
- {
- using (DbCommand cmd = provider.CreateCommand())
- {
- try
- {
- //循环
- foreach (DictionaryEntry myDE in SQLStringList)
- {
- cmd.Connection = connection;
- string storeName = myDE.Value.ToString();
- DbParameter[] cmdParms = (DbParameter[])myDE.Key;
- cmd.Transaction = trans;
- cmd.CommandText = storeName;
- cmd.CommandType = CommandType.StoredProcedure;
- if (cmdParms != null)
- {
- foreach (DbParameter parameter in cmdParms)
- {
- cmd.Parameters.Add(parameter);
- }
- }
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- }
- trans.Commit();
- return true;
- }
- catch
- {
- trans.Rollback();
- connection.Close();
- connection.Dispose();
- return false;
- }
- }
- }
- }
- }
- /// <summary>
- /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
- /// </summary>
- /// <param name="connection">数据库连接</param>
- /// <param name="storedProcName">存储过程名</param>
- /// <param name="parameters">存储过程参数</param>
- /// <returns>SqlCommand</returns>
- private DbCommand BuildQueryCommand(DbConnection connection, string storedProcName, DbParameter[] parameters)
- {
- if (connection.State != ConnectionState.Open)
- {
- connection.Open();
- }
- DbCommand command = provider.CreateCommand();
- command.CommandText = storedProcName;
- command.Connection = connection;
- command.CommandType = CommandType.StoredProcedure;
- if (parameters != null)
- {
- foreach (DbParameter parameter in parameters)
- {
- command.Parameters.Add(parameter);
- }
- }
- return command;
- }
- #endregion
- }
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP