///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片久久AV | 亚洲欧美日韩精品久久奇米色影视 | 色视在线 | 亚洲成人久久久 | 亚洲欧美无人区乱码 | 国产在线精品一区二区夜色 | 日本黄色不卡视频 | 欧美一区二区在线视频 | 国产精品久久久久影院色老大 | 婷婷丁香色综合图亚洲 | 九九99热久久精品在线9 | 亚洲天堂视频在线观看 | 国产成人av免费观看 | 日干夜干天天干 | 国产亚洲一区二区三区在线观看 | 性欧美一级毛片在线播放 | 狠狠色丁香婷婷综合橹不卡 | 国产精品自线在线播放 | 91视频网国产 | 午夜影院在线播放 | 蜜芽在线| 国产精品久久国产精品 | 亚洲欧美在线精品一区二区 | 久草在线网址 | 免费看那种视频 | 欧美极品bbbbⅹxxxx | 一本一道dvd在线播放器 |