MySql c#通用类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;//导命名空间
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace Test.DAL.MySql
{
public class DBHelp { private readonly string connString = ConfigurationManager.ConnectionStrings["mysql"].ToString(); private MySqlConnection mySqlConnection = null; private MySqlCommand mySqlCommand = null; protected MySqlDataReader mySqlDataReader = null; private MySqlDataAdapter mySqlDataAdapter = null; private DataSet dataSet = null; private int i = 0; private MySqlConnection ExecuteMySqlConnection() { try { this.mySqlConnection = new MySqlConnection(this.connString); this.mySqlConnection.Open(); } catch (Exception) { throw; } return this.mySqlConnection; } private MySqlCommand ExecuteMySqlCommand(string sql,CommandType commandType,MySqlParameter[] mySqlParameter) { try { this.mySqlConnection = this.ExecuteMySqlConnection(); this.mySqlCommand = new MySqlCommand(sql, this.mySqlConnection); if (mySqlParameter != null && mySqlParameter.Length > 0) { this.mySqlCommand.Parameters.AddRange(mySqlParameter); } this.mySqlCommand.CommandType = commandType; } catch (Exception) { throw; } return this.mySqlCommand; } protected MySqlDataReader ExecuteMySqlDataReader(string sql, CommandType commandType, MySqlParameter[] mySqlParameter) { try { this.mySqlCommand = this.ExecuteMySqlCommand(sql, commandType, mySqlParameter); this.mySqlDataReader = this.mySqlCommand.ExecuteReader(); } catch (Exception) { throw; } return this.mySqlDataReader; } protected DataSet ExecuteDataSet(string sql, CommandType commandType, MySqlParameter[] mySqlParameter) { this.dataSet = new DataSet(); try { this.mySqlCommand = this.ExecuteMySqlCommand(sql, commandType, mySqlParameter); this.mySqlDataAdapter = new MySqlDataAdapter(this.mySqlCommand); this.mySqlDataAdapter.Fill(dataSet); } catch (Exception) { throw; } return this.dataSet; } protected int ExecuteScalar(string sql, CommandType commandType, MySqlParameter[] mySqlParameter) { try { this.mySqlCommand = this.ExecuteMySqlCommand(sql, commandType, mySqlParameter); this.i = int.Parse(this.mySqlCommand.ExecuteScalar().ToString()); } catch (Exception) { throw; } return this.i; } protected int ExecuteNonQuery(string sql, CommandType commandType, MySqlParameter[] mySqlParameter) { try { this.mySqlCommand = this.ExecuteMySqlCommand(sql, commandType, mySqlParameter); this.i = this.mySqlCommand.ExecuteNonQuery(); } catch (Exception) { throw; } return this.i; } protected void CloseConnection(MySqlConnection mySqlConnection) { try { if (mySqlConnection != null) { mySqlConnection = null; mySqlConnection.Close(); } } catch (Exception) { throw; } } protected void CloseDataReader(MySqlDataReader mySqlDataReader) { try { if (mySqlDataReader != null) { mySqlDataReader = null; mySqlDataReader.Close(); } } catch (Exception) { throw; } } }
}
最新文章
- PHP从PHP5.0到PHP7.1的性能全评测
- webpack脚手架搭建(简单版)
- ios 关键字 IB_DESIGNABLE IBInspectable 尝鲜
- Java Hour 18 来个CURD吧 (三)
- Redis应用
- windows Phone 浏览器窗口的尺寸
- selenium RC
- 验证SQLServer死锁进程
- 【CJOJ2440】大话西游(树链剖分)
- Spring Cloud Contract
- 携程Java后台开发三面面经
- Thread之八:interrupt中断
- 配置react-sass
- VXLAN
- 计算Python运行时间
- python 3.6.5 sys模块和os模块
- java内存的分配和管理
- 海康ipc onvif抓包分析
- Mysql5.7.22版本,插入中文乱码的问题
- 鲁宾斯坦说:";思维是在概括中完成的。";
热门文章
- element select下拉框绑定number类型
- linux 的sed命令解释 sed ':t;N;s/\n/,/;b t' 将换行符换成逗号
- Django cookie、session使用
- Python-程序的控制结构
- 清北学堂模拟赛d4t5 b
- hdu 4888 最大流给出行列和求矩阵
- 某种密码(password.*)
- org.hibernate.PropertyValueException: not-null property references a null or transient value: model.
- 2.5-冗余VLAN
- 我们的一个已投产项目的高可用数据库实战 - mongo 副本集的搭建具体过程