- /**
- * Beef Brisket
- * Date of Creation: March 7, 2010 17:35
- * Class Description: Operation class for MySQL database
- */
- Using System;
- Using System.Data;
- Using MySql.Data.MySqlClient;
- Namespace Niunan.BYLW.Utility
- {
- /// <summary> MySQL database operation class
- ///
- // </summary>
- public class Mysqlhelper
- {
- private Mysqlconnection conn = null;
- private Mysqlcommand cmd = null;
- private Mysqldatareader SDR = null;
- Public mysqlhelper ()
- {
- //string connstr = webconfigurationmanager.connectionstrings["ConnStr"]. ToString ();
- string connstr = "Server=localhost;database=test;uid=root;pwd=123456;charset=utf8";
- conn = new Mysqlconnection (CONNSTR);
- }
- /// <summary> create command object
- ///
- // </summary>
- /// <param name= "SQL" >sql statement </param>
- public void CreateCommand (string sql)
- {
- Conn. Open ();
- cmd = new Mysqlcommand (SQL, conn);
- }
- /// <summary> Add Parameters
- ///
- // </summary>
- /// <param name= "paramname" > Parameter name </param>
- // <param name= "value" > Value </param>
- public void Addparameter (string paramname, object value)
- {
- Cmd. Parameters.Add (new Mysqlparameter (paramname, value));
- }
- /// <summary> execute non-parametric additions and deletions to SQL statements
- ///
- // </summary>
- /// <param name= "Cmdtext" > adding or deleting SQL statements </param>
- /// <param name= "ct" > Command type </param>
- // <returns></returns>
- Public bool ExecuteNonQuery ()
- {
- int res;
- Try
- {
- res = cmd. ExecuteNonQuery ();
- if (res > 0)
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (Conn. state = = ConnectionState.Open)
- {
- Conn. Close ();
- }
- }
- return false;
- }
- /// <summary> Execute query SQL statement
- ///
- // </summary>
- /// <param name= "cmdtext" > Query SQL statements </param>
- // <returns></returns>
- Public DataTable ExecuteQuery ()
- {
- DataTable dt = new DataTable ();
- using (SDR = cmd. ExecuteReader (commandbehavior.closeconnection))
- {
- Dt. Load (SDR);
- }
- return DT;
- }
- /// <summary> Returns the value of the first column of the first row of the results queried by the SQL statement query
- ///
- // </summary>
- // <returns></returns>
- public string ExecuteScalar ()
- {
- string res = "";
- Try
- {
- Object obj = cmd. ExecuteScalar ();
- if (obj! = null)
- {
- res = obj. ToString ();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (Conn. state = = ConnectionState.Open)
- {
- Conn. Close ();
- }
- }
- return res;
- }
- }
- }
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.