Self-encapsulated ASP. NET MySQL database operation class

Source: Internet
Author: User

  1. /**
  2. * Beef Brisket
  3. * Date of Creation: March 7, 2010 17:35
  4. * Class Description: Operation class for MySQL database
  5. */
  6. Using System;
  7. Using System.Data;
  8. Using MySql.Data.MySqlClient;
  9. Namespace Niunan.BYLW.Utility
  10. {
  11. /// <summary> MySQL database operation class
  12. ///   
  13. // </summary>
  14. public class Mysqlhelper
  15. {
  16. private Mysqlconnection conn = null;
  17. private Mysqlcommand cmd = null;
  18. private Mysqldatareader SDR = null;
  19. Public mysqlhelper ()
  20. {
  21. //string connstr = webconfigurationmanager.connectionstrings["ConnStr"].  ToString ();
  22. string connstr = "Server=localhost;database=test;uid=root;pwd=123456;charset=utf8";
  23. conn = new Mysqlconnection (CONNSTR);
  24. }
  25. /// <summary> create command object
  26. ///   
  27. // </summary>
  28. /// <param name= "SQL" >sql statement </param>
  29. public void CreateCommand (string sql)
  30. {
  31. Conn. Open ();
  32. cmd = new Mysqlcommand (SQL, conn);
  33. }
  34. /// <summary> Add Parameters
  35. ///   
  36. // </summary>
  37. /// <param name= "paramname" > Parameter name </param>
  38. // <param name= "value" > Value </param>
  39. public void Addparameter (string paramname, object value)
  40. {
  41. Cmd.  Parameters.Add (new Mysqlparameter (paramname, value));
  42. }
  43. /// <summary> execute non-parametric additions and deletions to SQL statements
  44. ///    
  45. // </summary>
  46. /// <param name= "Cmdtext" > adding or deleting SQL statements </param>
  47. /// <param name= "ct" > Command type </param>
  48. // <returns></returns>
  49. Public bool ExecuteNonQuery ()
  50. {
  51. int res;
  52. Try
  53. {
  54. res = cmd. ExecuteNonQuery ();
  55. if (res > 0)
  56. {
  57. return true;
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. throw ex;
  63. }
  64. finally
  65. {
  66. if (Conn. state = = ConnectionState.Open)
  67. {
  68. Conn. Close ();
  69. }
  70. }
  71. return false;
  72. }
  73. /// <summary> Execute query SQL statement
  74. ///    
  75. // </summary>
  76. /// <param name= "cmdtext" > Query SQL statements </param>
  77. // <returns></returns>
  78. Public DataTable ExecuteQuery ()
  79. {
  80. DataTable dt = new DataTable ();
  81. using (SDR = cmd. ExecuteReader (commandbehavior.closeconnection))
  82. {
  83. Dt. Load (SDR);
  84. }
  85. return DT;
  86. }
  87. /// <summary> Returns the value of the first column of the first row of the results queried by the SQL statement query
  88. ///   
  89. // </summary>
  90. // <returns></returns>
  91. public string ExecuteScalar ()
  92. {
  93. string res = "";
  94. Try
  95. {
  96. Object obj = cmd.  ExecuteScalar ();
  97. if (obj! = null)
  98. {
  99. res = obj. ToString ();
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. throw ex;
  105. }
  106. finally
  107. {
  108. if (Conn. state = = ConnectionState.Open)
  109. {
  110. Conn. Close ();
  111. }
  112. }
  113. return res;
  114. }
  115. }
  116. }

Are the sqlitehelper of the operation of the class moved over, changed the mysqlconnection and other variable names, for MySQL often appear in Chinese garbled, after the experiment, the solution:


① use phpMyAdmin to build MySQL database, remember to choose UTF-8 encoding


② in the connection string in the database operation class above, remember to add Charset=utf8


In this way, using the above operation class to insert Chinese will not appear garbled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.