This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17352|Reply: 0

[Source] c# Connect to the Mysql database

[Copy link]
Posted on 11/20/2015 12:57:35 PM | | |
  1. /// <summary>
  2.     /// MySQL数据库操作
  3.     /// author:hhm
  4.     /// date:2012-2-22
  5.     /// </summary>
  6.     public class MySqlDbHelper
  7.     {
  8.         #region 私有变量
  9.         private const string defaultConfigKeyName = "DbHelper";//连接字符串 默认Key
  10.         private string connectionString;
  11.         private string providerName;

  12.         #endregion

  13.          #region 构造函数

  14.         /// <summary>
  15.         /// 默认构造函数(DbHelper)
  16.         /// </summary>
  17.         public MySqlDbHelper()
  18.         {
  19.             this.connectionString = ConfigurationManager.ConnectionStrings["DbHelper"].ConnectionString;
  20.             this.providerName = ConfigurationManager.ConnectionStrings["DbHelper"].ProviderName;
  21.         }

  22.         /// <summary>
  23.         /// DbHelper构造函数
  24.         /// </summary>
  25.         /// <param name="keyName">连接字符串名</param>
  26.         public MySqlDbHelper(string keyName)
  27.         {
  28.             this.connectionString = ConfigurationManager.ConnectionStrings[keyName].ConnectionString;
  29.             this.providerName = ConfigurationManager.ConnectionStrings[keyName].ProviderName;
  30.         }

  31.         #endregion

  32.         public int ExecuteNonQuery(string sql, params  MySqlParameter[] parameters)        {
  33.             MySqlConnection con = new MySqlConnection(connectionString);
  34.             con.Open();
  35.             MySqlCommand cmd = new MySqlCommand(sql, con);
  36.             foreach (MySqlParameter parameter in parameters)
  37.             {
  38.                 cmd.Parameters.Add(parameter);
  39.             }
  40.             int res =0;
  41.             try
  42.             {
  43.                 res = cmd.ExecuteNonQuery();
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 res = -1;
  48.             }
  49.             cmd.Dispose();
  50.             con.Close();
  51.             return res;
  52.         }

  53.         public object ExecuteScalar(string sql, params MySqlParameter[] parameters)
  54.         {
  55.             MySqlConnection con = new MySqlConnection(connectionString);
  56.             con.Open();
  57.             MySqlCommand cmd = new MySqlCommand(sql, con);
  58.             foreach (MySqlParameter parameter in parameters)
  59.             {
  60.                 cmd.Parameters.Add(parameter);
  61.             }
  62.             object res = cmd.ExecuteScalar();
  63.             cmd.Dispose();
  64.             con.Close();
  65.             return res;
  66.         }

  67.         public DataTable ExecuteDataTable(string sql, params MySqlParameter[] parameters)
  68.         {
  69.             MySqlConnection con = new MySqlConnection(connectionString);
  70.             con.Open();
  71.             MySqlCommand cmd = new MySqlCommand(sql, con);
  72.             foreach (MySqlParameter parameter in parameters)
  73.             {
  74.                 cmd.Parameters.Add(parameter);
  75.             }
  76.             DataSet dataset = new DataSet();//dataset放执行后的数据集合
  77.             MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
  78.             adapter.Fill(dataset);
  79.             cmd.Dispose();
  80.             con.Close();
  81.             return dataset.Tables[0];           
  82.         }
  83.     }
Copy code
MySql.Data.rar (101.28 KB, Number of downloads: 1, 售价: 1 粒MB)




Previous:asp.net Directory Listing Denied workaround appears on mvc websites
Next:Unsupported pixel format of the source or template image
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com