///MySql數據庫操作類///publicclassMySqlHelper{//////MysqlConnection///privatestaticMySql.Data.MySqlClient.MySqlConnectionMysqlConn" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

.NET 使用 MySql.Data.dll 動態庫操作MySql的幫

系統 2614 0

.NET 使用 MySql.Data.dll 動態庫操作MySql的幫助類--MySqlHelper

參考演示樣例代碼,例如以下所看到的:

      /// <summary>
	/// MySql 數據庫操作類
	/// </summary>
	public class MySqlHelper
	{
		/// <summary>
		/// MysqlConnection
		/// </summary>
		private static MySql.Data.MySqlClient.MySqlConnection MysqlConnection;

		/// <summary>
		/// 獲MySql 連接置信息
		/// </summary>
		/// <returns></returns>
		public static MySql.Data.MySqlClient.MySqlConnection GetCon()
		{
			String mysqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Libor_MySql_QuoteCenter_ConnectionString"].ToString();

			if (MysqlConnection == null)
				using (MysqlConnection = new MySql.Data.MySqlClient.MySqlConnection(mysqlConnectionString)) { };

			if (MysqlConnection.State == System.Data.ConnectionState.Closed)
				MysqlConnection.Open();

			if (MysqlConnection.State == System.Data.ConnectionState.Broken)
			{
				MysqlConnection.Close();
				MysqlConnection.Open();
			}

			return MysqlConnection;
		}


		#region 運行MySQL語句或存儲過程,返回受影響的行數
		/// <summary>
		/// 運行MySQL語句或存儲過程
		/// </summary>
		/// <param name="type">命令類型</param>
		/// <param name="sqlString">sql語句</param>
		/// <param name="pstmt">參數</param>
		/// <returns>運行結果</returns>
		public static int ExecuteNonQuery(CommandType type, String sqlString, MySql.Data.MySqlClient.MySqlParameter[] para)
		{
			try
			{
				using (MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand())
				{
					com.Connection = GetCon();
					com.CommandText = @sqlString;
					com.CommandType = type;
					if (para != null)
						com.Parameters.AddRange(para);

					int val = com.ExecuteNonQuery();
					com.Parameters.Clear();

					return val;
				}
			}
			catch (Exception ex)
			{
				Logger.Error("運行MySQL語句或存儲過程,異常!", ex);

				return 0;
			}
			finally
			{
				if (MysqlConnection.State != ConnectionState.Closed)
					MysqlConnection.Close();
			}
		}


		/// <summary>
		/// 運行帶事務的SQL語句或存儲過程
		/// </summary>
		/// <param name="trans">事務</param>
		/// <param name="type">命令類型</param>
		/// <param name="sqlString">SQL語句</param>
		/// <param name="pstmt">參數</param>
		/// <returns>運行結果</returns>
		public static int ExecuteNonQuery(MySql.Data.MySqlClient.MySqlTransaction trans, CommandType type, String sqlString, MySql.Data.MySqlClient.MySqlParameter[] para)
		{
			try
			{
				using (MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand())
				{
					com.Connection = MysqlConnection;
					com.CommandText = @sqlString;
					com.CommandType = type;
					if (para != null)
						com.Parameters.AddRange(para);
					if (trans != null)
						com.Transaction = trans;

					int val = com.ExecuteNonQuery();
					com.Parameters.Clear();

					return val;
				}
			}
			catch (Exception ex)
			{
				Logger.Error("運行MySQL語句或存儲過程2,異常!", ex);

				return 0;
			}
			finally
			{
				if (MysqlConnection.State != ConnectionState.Closed)
					MysqlConnection.Close();
			}
		}
		#endregion


		#region 運行SQL語句或存儲過程,返回 DataTable
		/// <summary>
		/// 運行SQL語句或存儲過程,返回 DataTable
		/// </summary>
		/// <param name="type">命令類型</param>
		/// <param name="sqlString">SQL語句</param>
		/// <param name="pstmt">參數</param>
		/// <returns>運行結果</returns>
		public static DataTable ExecuteReaderToDataTable(CommandType type, String sqlString, MySql.Data.MySqlClient.MySqlParameter[] para)
		{
			DataTable dt = new DataTable();
			MySql.Data.MySqlClient.MySqlDataReader dr = null;

			try
			{
				using (MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand())
				{
					com.Connection = GetCon();
					com.CommandText = @sqlString;
					com.CommandType = type;
					if (para != null)
						com.Parameters.AddRange(para);

					using (dr = com.ExecuteReader(CommandBehavior.CloseConnection))
					{
						if (dr != null)
							dt.Load(dr);

						com.Parameters.Clear();
					}

					return dt;
				}
			}
			catch (Exception ex)
			{
				Logger.Error("運行SQL語句或存儲過程,返回 DataTable,異常!", ex);

				return null;
			}
			finally
			{
				if (dr != null && !dr.IsClosed)
					dr.Close();

				if (MysqlConnection.State != ConnectionState.Closed)
					MysqlConnection.Close();
			}
		}
		#endregion

	}
    

特別說明:

? ? ? ? ? ? ? 1、MySql.Data.dll? mysql官網提供的組件,下載后加入引用到當前項目就可以使用

? ? ? ? ? ?2、參數化處理

? ? ? ? ? ? ? ? 在SQLServer中參數化處 理符號為"@", 參數化演示樣例 如:

               SqlParameter[] param = { 
               new SqlParameter("@TABLEDATA", tableData)
         };
    
? ? ? ? ? ? ? ?在MySql中參數化處理符號為“?”,參數化示比如:

               MySql.Data.MySqlClient.MySqlParameter[] paras = {
		 new MySql.Data.MySqlClient.MySqlParameter("?LIBOR_NAME",name),
         };
    
其它參考文章例如以下:

http://www.jb51.net/article/30342.htm




.NET 使用 MySql.Data.dll 動態庫操作MySql的幫助類--MySqlHelper


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人午夜天堂 | 色图综合 | 狠狠草视频| 毛片成人永久免费视频 | 无码缴情做A爱片毛片A片 | 精品国产第一国产综合精品 | 91在线短视频 | 久久综合图区亚洲综合图区 | 久草福利在线视频 | 中文字幕欧美在线 | 瑟瑟在线 | 亚洲乱轮视频 | 国产一级毛片午夜福 | 在线色网站 | 久久99国产精品久久99无号码 | 久久精品亚洲欧美日韩精品中文字幕 | 成人性生交A片免费看麻豆 色倩网站 | 久久久精品中文字幕 | 国产精品嫩草影视在线观看 | 高清一区二区亚洲欧美日韩 | 国产精品福利片免费看 | 奇米影视在线观看 | 色综合久久丁香婷婷 | 四虎网址 | 亚洲午夜久久久久中文字幕久 | 九九影院理论片 | 精品国产一区二区三区久久 | 国产萝控精品福利视频免费观看 | 就操在线 | 欧美专区在线 | 久久综合九色综合欧美狠狠 | 91青青青青青爽在线 | 日本欧美人xxxxx在线观看 | 五月婷婷色视频 | 国产欧美日韩在线 | 99色这里只有精品 | 人人干在线| 久久中文精品 | 日本高清va不卡视频在线观看 | 99久久国产综合精品女小说 | 日本三级不卡 |