///MySql數(shù)據(jù)庫操作類///publicclassMySqlHelper{//////MysqlConnection///privatestaticMySql.Data.MySqlClient.MySqlConnectionMysqlConn" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

.NET 使用 MySql.Data.dll 動態(tài)庫操作MySql的幫

系統(tǒng) 2533 0

.NET 使用 MySql.Data.dll 動態(tài)庫操作MySql的幫助類--MySqlHelper

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

      /// <summary>
	/// MySql 數(shù)據(jù)庫操作類
	/// </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語句或存儲過程,返回受影響的行數(shù)
		/// <summary>
		/// 運行MySQL語句或存儲過程
		/// </summary>
		/// <param name="type">命令類型</param>
		/// <param name="sqlString">sql語句</param>
		/// <param name="pstmt">參數(shù)</param>
		/// <returns>運行結(jié)果</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>
		/// 運行帶事務(wù)的SQL語句或存儲過程
		/// </summary>
		/// <param name="trans">事務(wù)</param>
		/// <param name="type">命令類型</param>
		/// <param name="sqlString">SQL語句</param>
		/// <param name="pstmt">參數(shù)</param>
		/// <returns>運行結(jié)果</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">參數(shù)</param>
		/// <returns>運行結(jié)果</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官網(wǎng)提供的組件,下載后加入引用到當(dāng)前項目就可以使用

? ? ? ? ? ?2、參數(shù)化處理

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

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

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

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




.NET 使用 MySql.Data.dll 動態(tài)庫操作MySql的幫助類--MySqlHelper


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲综合干 | 国产区一区二区三 | h片免费观看 | 亚洲综合插 | 国产精品19p | 欧美一级毛片免费网站 | 久久免费视频播放 | 亚洲精品乱码久久久久久中文字幕 | 美女操穴 | 日韩精品一区二区三区免费视频 | 视频一区色眯眯视频在线 | 日韩伦理一区二区三区 | 四虎影视网站 | 国产精品成人扳一级aa毛片 | 亚洲国产系列久久精品99人人 | 一区二区三区四区在线 | 欧美大片在线观看成人 | 亚色视频在线观看 | 天堂一区 | 久久爱com| 亚州国产 | 免费区欧美一级毛片精品 | 精品动漫中文字幕一区二区三区 | 黄色亚洲毛片 | 国产免费一区二区三区在线 | 亚洲欧美日韩专区一 | 久久久久久久久久久福利观看 | 亚洲精品在线播放视频 | 一级播放 | 国产激情自拍视频 | 97在线免费视频观看 | 一级无毛| 国语国产真人对白毛片 | 日韩一区二区免费看 | 久久精品国产精品亚洲婷婷 | 99九九精品国产高清自在线 | 久久这里只精品国产99热8 | 欧美日日夜夜 | 午夜操 | 性欧美视频a毛片在线播放 性欧美视频在线观看 | 中文字幕 二区 三区 搜查官 |