. Net DBhelper implementation based on mysql _ MySQL

Source: Internet
Author: User
. 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] ;}}

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.