///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條評論
主站蜘蛛池模板: 人人干天天干 | 欧美一级亚洲一级 | 在线不卡日本 | 婷婷色站 | 午夜在线播放免费高清观看 | 一区二区三区免费视频观看 | 午夜性盈盈 | 欧美亚洲国产精品久久 | 成人动漫久久 | 一级欧美毛片成人 | 2020年国产高中毛片在线视频 | 日本一级毛片视频 | 天天曰夜夜操 | 国产精品毛片 | 久热精品视频在线 | 久久精品视频9 | 香香影院在线观看 | 亚洲欧美一区二区三区国产精品 | 日本私人影院 | 狠狠色丁婷婷综合久久 | 国产欧美曰韩一区二区三区 | 久久香蕉国产线看观看乱码 | 国产精品久久国产精麻豆99网站 | 香蕉视频在线观看免费 | 国产精品999在线 | 国产99视频精品一区 | 999精品视频这里只有精品 | 亚洲综合精品香蕉久久网97 | 成人亚洲国产精品久久 | 亚洲免费精品视频 | 91香蕉福利一区二区三区 | 日本阿v精品视频在线观看 日本爱爱免费视频 | 大尺度福利视频在线观看网址 | 波多野结衣在线一区二区 | 天天操夜夜操狠狠操 | 久久精品亚洲日本波多野结衣 | 国产亚洲一区二区三区在线 | 亚洲日本一区二区三区在线不卡 | 免费高清成人啪啪网站 | 香港三级做爰大爽视频 | 风流一代在线播放 |