分类

链接

2018 年 4 月
 1
2345678
9101112131415
16171819202122
23242526272829
30  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
MySQL Connector/Net for .net 4.0 驱动包
.NET 暂无评论 阅读(577)

MySQL Connector/Net for .net 4.0 驱动包

http://mysql-connector-net-6.8.7.msi

 

附使用方法:

  1. using PW.Common;
  2. using PW.Model;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Data.Common;
  9. using System.Linq;
  10. using System.Text;
  11.  
  12. namespace PW.Db
  13. {
  14.  
  15.     /// <summary>
  16.     /// 数据访问基础类
  17.     /// </summary>
  18.     public class Sql
  19.     {
  20.         protected static string connectionString = "";
  21.         protected static DbProviderFactory provider;
  22.         public Sql()
  23.         {
  24.             using (var io = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Config.xml"))
  25.             {
  26.                 String xml = io.ReadToEnd();
  27.                 Config config = (Config)XmlUtil.Deserialize(typeof(Config), xml);
  28.                 connectionString = config.ConnectionString;
  29.                 provider = DbProviderFactories.GetFactory(config.ProviderName);
  30.  
  31.             }
  32.  
  33.         }
  34.  
  35.         private DbDataAdapter CreateDataAdapter(DbConnection conn)
  36.         {
  37.             if (conn is System.Data.SqlClient.SqlConnection)
  38.                 return new System.Data.SqlClient.SqlDataAdapter();
  39.  
  40.             if (conn is MySql.Data.MySqlClient.MySqlConnection)
  41.                return new MySql.Data.MySqlClient.MySqlDataAdapter();
  42.  
  43.             throw new NotImplementedException();
  44.         }
  45.  
  46.         #region  执行简单SQL语句
  47.  
  48.         /// <summary>
  49.         /// 执行SQL语句,返回影响的记录数
  50.         /// </summary>
  51.         /// <param name="SQLString">SQL语句</param>
  52.         /// <returns>影响的记录数</returns>
  53.         public int ExecuteSql(string SQLString)
  54.         {
  55.             using (DbConnection connection = provider.CreateConnection())
  56.             {
  57.                 connection.ConnectionString = connectionString;
  58.                 using (DbCommand cmd = provider.CreateCommand())
  59.                 {
  60.                     cmd.Connection = connection;
  61.                     cmd.CommandText = SQLString;
  62.                     try
  63.                     {
  64.                         connection.Open();
  65.                         int rows = cmd.ExecuteNonQuery();
  66.                         return rows;
  67.                     }
  68.                     catch (DbException E)
  69.                     {
  70.                         connection.Close();
  71.                         connection.Dispose();
  72.                         throw new Exception(E.Message);
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         /// <summary>
  79.         /// 执行多条SQL语句,实现数据库事务。
  80.         /// </summary>
  81.         /// <param name="SQLStringList">多条SQL语句</param>        
  82.         public void ExecuteSqlTran(ArrayList SQLStringList)
  83.         {
  84.             using (DbConnection conn = provider.CreateConnection())
  85.             {
  86.                 conn.ConnectionString = connectionString;
  87.                 conn.Open();
  88.                 using (DbCommand cmd = provider.CreateCommand())
  89.                 {
  90.                     cmd.Connection = conn;
  91.                     using (DbTransaction tx = conn.BeginTransaction())
  92.                     {
  93.                         cmd.Transaction = tx;
  94.                         try
  95.                         {
  96.                             for (int n = 0; n < SQLStringList.Count; n++)
  97.                             {
  98.                                 string strsql = SQLStringList[n].ToString();
  99.                                 if (strsql.Trim().Length > 1)
  100.                                 {
  101.                                     cmd.CommandText = strsql;
  102.                                     cmd.ExecuteNonQuery();
  103.                                 }
  104.                             }
  105.                             tx.Commit();
  106.                         }
  107.                         catch (DbException ex)
  108.                         {
  109.                             tx.Rollback();
  110.                             conn.Close();
  111.                             conn.Dispose();
  112.                             throw ex;
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         /// <summary>
  119.         /// 执行一条计算查询结果语句,返回查询结果(object)。
  120.         /// </summary>
  121.         /// <param name="SQLString">计算查询结果语句</param>
  122.         /// <returns>查询结果(object)</returns>
  123.         public object GetSingle(string SQLString)
  124.         {
  125.             using (DbConnection connection = provider.CreateConnection())
  126.             {
  127.                 connection.ConnectionString = connectionString;
  128.                 using (DbCommand cmd = provider.CreateCommand())
  129.                 {
  130.                     cmd.Connection = connection;
  131.                     cmd.CommandText = SQLString;
  132.                     try
  133.                     {
  134.                         connection.Open();
  135.                         object obj = cmd.ExecuteScalar();
  136.                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  137.                         {
  138.                             return null;
  139.                         }
  140.                         else
  141.                         {
  142.                             return obj;
  143.                         }
  144.                     }
  145.                     catch (DbException e)
  146.                     {
  147.                         connection.Close();
  148.                         connection.Dispose();
  149.                         throw new Exception(e.Message);
  150.                     }
  151.                 }
  152.             }
  153.         }
  154.         /// <summary>
  155.         /// 执行查询语句,返回SqlDataReader
  156.         /// </summary>
  157.         /// <param name="strSQL">查询语句</param>
  158.         /// <returns>SqlDataReader</returns>
  159.         public DbDataReader ExecuteReader(string strSQL)
  160.         {
  161.             DbConnection connection = provider.CreateConnection();
  162.             connection.ConnectionString = connectionString;
  163.             DbCommand cmd = provider.CreateCommand();
  164.             cmd.Connection = connection;
  165.             cmd.CommandText = strSQL;
  166.             try
  167.             {
  168.                 connection.Open();
  169.                 DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  170.                 return myReader;
  171.             }
  172.             catch (System.Data.Common.DbException e)
  173.             {
  174.                 connection.Close();
  175.                 connection.Dispose();
  176.                 throw new Exception(e.Message);
  177.             }
  178.  
  179.         }
  180.         /// <summary>
  181.         /// 执行查询语句,返回DataSet
  182.         /// </summary>
  183.         /// <param name="SQLString">查询语句</param>
  184.         /// <returns>DataSet</returns>
  185.         public DataSet GetDataSet(string SQLString)
  186.         {
  187.             using (DbConnection connection = provider.CreateConnection())
  188.             {
  189.                 connection.ConnectionString = connectionString;
  190.                 using (DbCommand cmd = provider.CreateCommand())
  191.                 {
  192.                     cmd.Connection = connection;
  193.                     cmd.CommandText = SQLString;
  194.                     try
  195.                     {
  196.                         DataSet ds = new DataSet();
  197.                         DbDataAdapter adapter = provider.CreateDataAdapter();
  198.                         adapter.SelectCommand = cmd;
  199.                         adapter.Fill(ds, "ds");
  200.                         return ds;
  201.                     }
  202.                     catch (DbException ex)
  203.                     {
  204.                         connection.Close();
  205.                         connection.Dispose();
  206.                         throw new Exception(ex.Message);
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.  
  212.         /// <summary>
  213.         /// 执行查询语句,返回DataSet
  214.         /// </summary>
  215.         /// <param name="SQLString">查询语句</param>
  216.         /// <returns>DataSet</returns>
  217.         public DataTable GetDataTable(string SQLString)
  218.         {
  219.             using (DbConnection connection = provider.CreateConnection())
  220.             {
  221.                 connection.ConnectionString = connectionString;
  222.                 using (DbCommand cmd = provider.CreateCommand())
  223.                 {
  224.                     cmd.Connection = connection;
  225.                     cmd.CommandText = SQLString;
  226.                     try
  227.                     {
  228.                         DataSet ds = new DataSet();
  229.                         DbDataAdapter adapter = provider.CreateDataAdapter();
  230.                         //DbDataAdapter adapter = CreateDataAdapter(connection);//provider.CreateDataAdapter();
  231.                         adapter.SelectCommand = cmd;
  232.                         adapter.Fill(ds, "ds");
  233.                         return ds.Tables[0];
  234.                     }
  235.                     catch (DbException ex)
  236.                     {
  237.                         connection.Close();
  238.                         connection.Dispose();
  239.                         throw new Exception(ex.Message);
  240.                     }
  241.                 }
  242.             }
  243.         }
  244.         #endregion
  245.  
  246.         #region 执行带参数的SQL语句
  247.  
  248.         /// <summary>
  249.         /// 执行SQL语句,返回影响的记录数
  250.         /// </summary>
  251.         /// <param name="SQLString">SQL语句</param>
  252.         /// <returns>影响的记录数</returns>
  253.         public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
  254.         {
  255.             using (DbConnection connection = provider.CreateConnection())
  256.             {
  257.                 connection.ConnectionString = connectionString;
  258.                 using (DbCommand cmd = provider.CreateCommand())
  259.                 {
  260.                     cmd.Connection = connection;
  261.                     cmd.CommandText = SQLString;
  262.                     try
  263.                     {
  264.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  265.                         int rows = cmd.ExecuteNonQuery();
  266.                         cmd.Parameters.Clear();
  267.                         return rows;
  268.                     }
  269.                     catch (DbException E)
  270.                     {
  271.                         connection.Close();
  272.                         connection.Dispose();
  273.                         throw new Exception(E.Message);
  274.                     }
  275.                 }
  276.             }
  277.         }
  278.  
  279.         /// <summary>
  280.         /// 执行多条SQL语句,实现数据库事务。
  281.         /// </summary>
  282.         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
  283.         public void ExecuteSqlTran(Hashtable SQLStringList)
  284.         {
  285.             using (DbConnection conn = provider.CreateConnection())
  286.             {
  287.                 conn.ConnectionString = connectionString;
  288.                 conn.Open();
  289.                 using (DbTransaction trans = conn.BeginTransaction())
  290.                 {
  291.                     using (DbCommand cmd = provider.CreateCommand())
  292.                     {
  293.                         try
  294.                         {
  295.                             //循环
  296.                             foreach (DictionaryEntry myDE in SQLStringList)
  297.                             {
  298.                                 string cmdText = myDE.Key.ToString();
  299.                                 DbParameter[] cmdParms = (DbParameter[])myDE.Value;
  300.                                 PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  301.                                 int val = cmd.ExecuteNonQuery();
  302.                                 cmd.Parameters.Clear();
  303.                             }
  304.                             trans.Commit();
  305.                         }
  306.                         catch (DbException ex)
  307.                         {
  308.                             trans.Rollback();
  309.                             conn.Close();
  310.                             conn.Dispose();
  311.                             throw ex;
  312.                         }
  313.                     }
  314.                 }
  315.             }
  316.         }
  317.  
  318.         /// <summary>
  319.         /// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
  320.         /// </summary>
  321.         /// <param name="SQLString">计算查询结果语句</param>
  322.         /// <returns>查询结果(object)</returns>
  323.         public object GetSingle(string SQLString, DbParameter[] cmdParms)
  324.         {
  325.             using (DbConnection connection = provider.CreateConnection())
  326.             {
  327.                 connection.ConnectionString = connectionString;
  328.                 using (DbCommand cmd = provider.CreateCommand())
  329.                 {
  330.                     try
  331.                     {
  332.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  333.                         object obj = cmd.ExecuteScalar();
  334.                         cmd.Parameters.Clear();
  335.                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  336.                         {
  337.                             return null;
  338.                         }
  339.                         else
  340.                         {
  341.                             return obj;
  342.                         }
  343.                     }
  344.                     catch (DbException e)
  345.                     {
  346.                         connection.Close();
  347.                         connection.Dispose();
  348.                         throw new Exception(e.Message);
  349.                     }
  350.                 }
  351.             }
  352.         }
  353.  
  354.         /// <summary>
  355.         /// 执行查询语句,返回SqlDataReader
  356.         /// </summary>
  357.         /// <param name="strSQL">查询语句</param>
  358.         /// <returns>SqlDataReader</returns>
  359.         public DbDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
  360.         {
  361.             DbConnection connection = provider.CreateConnection();
  362.             connection.ConnectionString = connectionString;
  363.             DbCommand cmd = provider.CreateCommand();
  364.             try
  365.             {
  366.                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  367.                 DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  368.                 cmd.Parameters.Clear();
  369.                 return myReader;
  370.             }
  371.             catch (DbException e)
  372.             {
  373.                 connection.Close();
  374.                 connection.Dispose();
  375.                 throw new Exception(e.Message);
  376.             }
  377.  
  378.         }
  379.  
  380.         /// <summary>
  381.         /// 执行查询语句,返回DataSet
  382.         /// </summary>
  383.         /// <param name="SQLString">查询语句</param>
  384.         /// <returns>DataSet</returns>
  385.         public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
  386.         {
  387.             using (DbConnection connection = provider.CreateConnection())
  388.             {
  389.                 connection.ConnectionString = connectionString;
  390.                 using (DbCommand cmd = provider.CreateCommand())
  391.                 {
  392.                     using (DbDataAdapter da = provider.CreateDataAdapter())
  393.                     {
  394.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  395.                         da.SelectCommand = cmd;
  396.                         DataSet ds = new DataSet();
  397.                         try
  398.                         {
  399.                             da.Fill(ds, "ds");
  400.                             cmd.Parameters.Clear();
  401.                             return ds;
  402.                         }
  403.                         catch (DbException ex)
  404.                         {
  405.                             connection.Close();
  406.                             connection.Dispose();
  407.                             throw new Exception(ex.Message);
  408.                         }
  409.                     }
  410.                 }
  411.             }
  412.         }
  413.  
  414.         private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, string cmdText, DbParameter[] cmdParms)
  415.         {
  416.             if (conn.State != ConnectionState.Open)
  417.             {
  418.                 conn.Open();
  419.             }
  420.             cmd.Connection = conn;
  421.             cmd.CommandText = cmdText;
  422.             if (trans != null)
  423.             {
  424.                 cmd.Transaction = trans;
  425.             }
  426.             cmd.CommandType = CommandType.Text;//cmdType;
  427.             if (cmdParms != null)
  428.             {
  429.                 foreach (DbParameter parm in cmdParms)
  430.                 {
  431.                     cmd.Parameters.Add(parm);
  432.                 }
  433.             }
  434.         }
  435.  
  436.         #endregion
  437.  
  438.         #region 存储过程操作
  439.         /// <summary>
  440.         /// 执行存储过程;
  441.         /// </summary>
  442.         /// <param name="storeProcName">存储过程名</param>
  443.         /// <param name="parameters">所需要的参数</param>
  444.         /// <returns>返回受影响的行数</returns>
  445.         public int RunProcedureExecuteSql(string storeProcName, DbParameter[] parameters)
  446.         {
  447.             using (DbConnection connection = provider.CreateConnection())
  448.             {
  449.                 connection.ConnectionString = connectionString;
  450.                 DbCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
  451.                 int rows = cmd.ExecuteNonQuery();
  452.                 cmd.Parameters.Clear();
  453.                 connection.Close();
  454.                 return rows;
  455.             }
  456.         }
  457.  
  458.         /// <summary>
  459.         /// 执行存储过程,返回首行首列的值
  460.         /// </summary>
  461.         /// <param name="storeProcName">存储过程名</param>
  462.         /// <param name="parameters">存储过程参数</param>
  463.         /// <returns>返回首行首列的值</returns>
  464.         public Object RunProcedureGetSingle(string storeProcName, DbParameter[] parameters)
  465.         {
  466.             using (DbConnection connection = provider.CreateConnection())
  467.             {
  468.                 connection.ConnectionString = connectionString;
  469.                 try
  470.                 {
  471.                     DbCommand cmd = BuildQueryCommand(connection, storeProcName, parameters);
  472.                     object obj = cmd.ExecuteScalar();
  473.                     cmd.Parameters.Clear();
  474.                     if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  475.                     {
  476.                         return null;
  477.                     }
  478.                     else
  479.                     {
  480.                         return obj;
  481.                     }
  482.                 }
  483.                 catch (DbException e)
  484.                 {
  485.                     connection.Close();
  486.                     connection.Dispose();
  487.                     throw new Exception(e.Message);
  488.                 }
  489.             }
  490.         }
  491.  
  492.         /// <summary>
  493.         /// 执行存储过程
  494.         /// </summary>
  495.         /// <param name="storedProcName">存储过程名</param>
  496.         /// <param name="parameters">存储过程参数</param>
  497.         /// <returns>SqlDataReader</returns>
  498.         public DbDataReader RunProcedureGetDataReader(string storedProcName, DbParameter[] parameters)
  499.         {
  500.             DbConnection connection = provider.CreateConnection();
  501.             connection.ConnectionString = connectionString;
  502.             DbDataReader returnReader;
  503.             DbCommand cmd = BuildQueryCommand(connection, storedProcName, parameters);
  504.             cmd.CommandType = CommandType.StoredProcedure;
  505.             returnReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  506.             cmd.Parameters.Clear();
  507.             return returnReader;
  508.         }
  509.  
  510.         /// <summary>
  511.         /// 执行存储过程
  512.         /// </summary>
  513.         /// <param name="storedProcName">存储过程名</param>
  514.         /// <param name="parameters">存储过程参数</param>
  515.         /// <returns>DataSet</returns>
  516.         public DataSet RunProcedureGetDataSet(string storedProcName, DbParameter[] parameters)
  517.         {
  518.             using (DbConnection connection = provider.CreateConnection())
  519.             {
  520.                 connection.ConnectionString = connectionString;
  521.                 DataSet dataSet = new DataSet();
  522.                 DbDataAdapter sqlDA = provider.CreateDataAdapter();
  523.                 sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
  524.                 sqlDA.Fill(dataSet);
  525.                 sqlDA.SelectCommand.Parameters.Clear();
  526.                 sqlDA.Dispose();
  527.                 return dataSet;
  528.             }
  529.         }
  530.  
  531.         /// <summary>
  532.         /// 执行多个存储过程,实现数据库事务。
  533.         /// </summary>
  534.         /// <param name="SQLStringList">存储过程的哈希表(value为存储过程语句,key是该语句的DbParameter[])</param>
  535.         public bool RunProcedureTran(Hashtable SQLStringList)
  536.         {
  537.             using (DbConnection connection = provider.CreateConnection())
  538.             {
  539.                 connection.ConnectionString = connectionString;
  540.                 connection.Open();
  541.                 using (DbTransaction trans = connection.BeginTransaction())
  542.                 {
  543.                     using (DbCommand cmd = provider.CreateCommand())
  544.                     {
  545.                         try
  546.                         {
  547.                             //循环
  548.                             foreach (DictionaryEntry myDE in SQLStringList)
  549.                             {
  550.                                 cmd.Connection = connection;
  551.                                 string storeName = myDE.Value.ToString();
  552.                                 DbParameter[] cmdParms = (DbParameter[])myDE.Key;
  553.  
  554.                                 cmd.Transaction = trans;
  555.                                 cmd.CommandText = storeName;
  556.                                 cmd.CommandType = CommandType.StoredProcedure;
  557.                                 if (cmdParms != null)
  558.                                 {
  559.                                     foreach (DbParameter parameter in cmdParms)
  560.                                     {
  561.                                         cmd.Parameters.Add(parameter);
  562.                                     }
  563.                                 }
  564.                                 int val = cmd.ExecuteNonQuery();
  565.                                 cmd.Parameters.Clear();
  566.                             }
  567.                             trans.Commit();
  568.                             return true;
  569.                         }
  570.                         catch
  571.                         {
  572.                             trans.Rollback();
  573.                             connection.Close();
  574.                             connection.Dispose();
  575.                             return false;
  576.                         }
  577.                     }
  578.                 }
  579.             }
  580.         }
  581.  
  582.         /// <summary>
  583.         /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
  584.         /// </summary>
  585.         /// <param name="connection">数据库连接</param>
  586.         /// <param name="storedProcName">存储过程名</param>
  587.         /// <param name="parameters">存储过程参数</param>
  588.         /// <returns>SqlCommand</returns>
  589.         private DbCommand BuildQueryCommand(DbConnection connection, string storedProcName, DbParameter[] parameters)
  590.         {
  591.             if (connection.State != ConnectionState.Open)
  592.             {
  593.                 connection.Open();
  594.             }
  595.             DbCommand command = provider.CreateCommand();
  596.             command.CommandText = storedProcName;
  597.             command.Connection = connection;
  598.             command.CommandType = CommandType.StoredProcedure;
  599.             if (parameters != null)
  600.             {
  601.                 foreach (DbParameter parameter in parameters)
  602.                 {
  603.                     command.Parameters.Add(parameter);
  604.                 }
  605.             }
  606.             return command;
  607.         }
  608.         #endregion
  609.     }
  610.  
  611. }

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:MySQL Connector/Net for .net 4.0 驱动包 | Bruce's Blog

发表评论

留言无头像?