. Net based on mysql DBhelper implementation on the Internet few dbhelper based on mysql database, so the implementation of mysql dbhelper, encapsulated a variety of methods to call database functions, it can be applied to small c # programs. Remember to change the password and port number when using mysql. the default value of mysql is 3306. I have defined the new port number, so it is 3307.
Using System; using System. collections. generic; using System. text; using System. data; using MySql. data. mySqlClient; namespace studentdb {public class DBHelper {// guide the database to connect to the database and call the Web. config file private static MySqlConnection connection; // create a Connection public static MySqlConnection connection Connection {get {MySqlConnection myConn = new MySqlConnection ("Database = student; Data Source = localhost; User Id = root; password = pw; pooling = false; CharSet = gb2312; port = 3307 "); string connectionString = myConn. connectionString; if (connection = null) {connection = new MySqlConnection (connectionString); // open the connection. open ();} else if (connection. state = System. data. connectionState. closed) {connection. open ();} else if (connection. state = System. data. connectionState. broken) {connection. close (); connection. open () ;}return connection ;}/// (no parameter) returns the number of rows executed (delete, modify, and update) public static int ExecuteCommand (string safeSql) {MySqlCommand cmd = new MySqlCommand (safeSql, Connection); int result = cmd. executeNonQuery (); return result;} // (with parameters) public static int ExecuteCommand (string SQL, params MySqlParameter [] values) {MySqlCommand cmd = new MySqlCommand (SQL, Connection ); cmd. parameters. addRange (values); return cmd. executeNonQuery ();} // (no parameter) returns the first column of the first row (delete modification and update) public static int GetScalar (string safeSql) {MySqlCommand cmd = new MySqlCommand (safeSql, connection); int result = Convert. toInt32 (cmd. executeScalar (); return result;} // (with parameters) public static int GetScalar (string SQL, params MySqlParameter [] values) {MySqlCommand cmd = new MySqlCommand (SQL, Connection ); cmd. parameters. addRange (values); int result = Convert. toInt32 (cmd. executeScalar (); return result;} // return a DataReader (query) public static MySqlDataReader GetReader (string safeSql) {MySqlCommand cmd = new MySqlCommand (safeSql, Connection ); mySqlDataReader reader = cmd. executeReader (); return reader;} public static MySqlDataReader GetReader (string SQL, params MySqlParameter [] values) {MySqlCommand cmd = new MySqlCommand (SQL, Connection); cmd. parameters. addRange (values); MySqlDataReader reader = cmd. executeReader (); return reader;} // returns a DataTable public static DataTable GetDataSet (string safeSql) {DataSet ds = new DataSet (); MySqlCommand cmd = new MySqlCommand (safeSql, connection); MySqlDataAdapter da = new MySqlDataAdapter (cmd); da. fill (ds); return ds. tables [0];} public static MySqlDataAdapter GetAdapter (string safeSql) {DataSet ds = new DataSet (); MySqlCommand cmd = new MySqlCommand (safeSql, Connection); // cmd. parameters. addRange (values); MySqlDataAdapter da = new MySqlDataAdapter (cmd); return da;} public static DataTable GetDataSet (string SQL, params MySqlParameter [] values) {DataSet ds = new DataSet (); MySqlCommand cmd = new MySqlCommand (SQL, Connection); cmd. parameters. addRange (values); MySqlDataAdapter da = new MySqlDataAdapter (cmd); da. fill (ds); return ds. tables [0] ;}}