이 글은 기계 번역의 미러 문서이며, 원본 기사로 바로 이동하려면 여기를 클릭해 주세요.

보기: 17352|회답: 0

[출처] c# MySQL 데이터베이스에 연결

[링크 복사]
게시됨 2015. 11. 20. 오후 12:57:35 | | |
  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.     }
코드 복사
MySql.Data.rar (101.28 KB, 다운로드 횟수: 1, 售价: 1 粒MB)




이전의:asp.net 디렉토리 목록 거부 우회 방법이 mvc 웹사이트에 나타납니다
다음:소스 이미지나 템플릿의 지원되지 않는 픽셀 형식
면책 조항:
Code Farmer Network에서 발행하는 모든 소프트웨어, 프로그래밍 자료 또는 기사는 학습 및 연구 목적으로만 사용됩니다; 위 내용은 상업적 또는 불법적인 목적으로 사용되지 않으며, 그렇지 않으면 모든 책임이 사용자에게 부담됩니다. 이 사이트의 정보는 인터넷에서 가져온 것이며, 저작권 분쟁은 이 사이트와는 관련이 없습니다. 위 내용은 다운로드 후 24시간 이내에 컴퓨터에서 완전히 삭제해야 합니다. 프로그램이 마음에 드신다면, 진짜 소프트웨어를 지원하고, 등록을 구매하며, 더 나은 진짜 서비스를 받아주세요. 침해가 있을 경우 이메일로 연락해 주시기 바랍니다.

Mail To:help@itsvse.com