C#连接MySql服务器,并进行增删改查的操作 public class MySqlDBConnector : IDisposable { private MySqlConnection _connection; private readonly object _locker = new object(); private static string _host; private static string _login; private static string _password; private static string _databaseName; private static string GetConnectionString() { if (string.IsNullOrEmpty(_host) || string.IsNullOrEmpty(_login) || string.IsNullOrEmpty(_password)) throw new CouldNotConnectToDBException("GetConnectionString(): Database connection parameters not initialized. Call firstly: DBConnector.InitializeDatabaseParameters(...)"); MySqlConnectionStringBuilder cnx = new MySqlConnectionStringBuilder { Server = _host, UserID = _login, Password = _password, //MinimumPoolSize = 10, //MaximumPoolSize = 200 }; if (string.IsNullOrEmpty(_databaseName) == false) cnx.Database = _databaseName; return cnx.ToString(); } /// <summary> /// Initialize database connection parameters /// </summary> /// <param name="host"></param> /// <param name="login"></param> /// <param name="password"></param> /// <param name="databaseName">Databasename</param> public static void InitializeDatabaseParameters(string host, string login, string password, string databaseName) { _host = host; _login = login; _password = password; _databaseName = databaseName; } /// <summary> /// Setup of the Connections to the local and the remote database server with standard user credentials. /// </summary> public void Connect() { lock (_locker) { if (_connection != null) { if (_connection.Ping()) return; // already connected Disconnect(); } _connection = Connect(GetConnectionString()); } } /// <summary> /// Connect to the database server using provided Connection Data. This can look like this: /// After Coonection has been opened, a Ping-check is performed to verify it. /// </summary> /// <param name="connectionData">Connection Data are used to setup the connection</param> /// <param name="evtHandler">Handler which occurs when the state of connection cahnged</param> /// <returns>Function returns the mysql connection</returns> /// <exception cref="CouldNotConnectToDBException">If the connection could not be setup to the database</exception> public static MySqlConnection Connect(string connectionData, StateChangeEventHandler evtHandler = null) { int SleepTmInMilliSec = 100; MySqlConnection mySqlConn; try { mySqlConn = new MySqlConnection(connectionData); if (evtHandler != null) mySqlConn.StateChange = evtHandler; mySqlConn.Open(); Thread.Sleep(SleepTmInMilliSec); var connected = mySqlConn.Ping(); if (connected == false) throw new CouldNotConnectToDBException("Connect(): Could not connect to the database"); } catch (Exception ex) { throw new CouldNotConnectToDBException(ex.Message, ex); } return mySqlConn; } /// <summary> /// Disconnect from the database server /// </summary> /// <exception cref="CouldNotDisconnectFromTheDBException">If it was not possible to discinnect from a database server. /// For more details see exception message.</exception> public void Disconnect() { lock (_locker) { try { _connection.Close(); _connection.Dispose(); _connection = null; } catch (Exception ex) { throw new CouldNotDisconnectFromTheDBException(ex.Message, ex); } } } /// <summary> /// Execute "select" statement and return result in DataTable /// </summary> /// <param name="sqlCommand"></param> /// <param name="transaction"></param> /// <returns></returns> /// <exception cref="NoActiveConnectionToDBException"></exception> /// <exception cref="SQLStatementException"></exception> public DataTable ExecuteSelect(string sqlCommand, DbTransaction transaction = null) { try { lock (_locker) { //check if the connection is already established. If not, setup the connection Connect(); MySqlTransaction mysqltransaction = transaction as MySqlTransaction; if (transaction != null && mysqltransaction == null) throw new Exception("ExecuteSelect(): Transaction object should have type 'MySqlTransaction'"); DataTable resultTable = new DataTable(); using (var mysqlAdapter = new MySqlDataAdapter()) { using (var mysqlCommand = new MySqlCommand { CommandText = sqlCommand, Connection = _connection, Transaction = mysqltransaction }) { mysqlAdapter.SelectCommand = mysqlCommand; mysqlAdapter.Fill(resultTable); } } return resultTable; } } catch (Exception ex) { if (ex is NoActiveConnectionToDBException) { throw; } // Original exception (ex) has to be set as inner exception: throw new SQLStatementException("Exception while executing the SQL statement. Please see inner exception for more details.", ex); } }
下载C#连接MySql服务器用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论