C # (. net) MySql database link Tool

Source: Internet
Author: User

C # (. net) MySql database link Tool

C # (. net) MySql database link Tool

 

Download and install MySQLDriverCS first
Http://sourceforge.net/projects/mysqldrivercs/

Find MySQLDriver. dll in the installation folder, and add the MySQLDriver. dll to the project.

Application configuration file:

App. config:

 

 

SqlHelper. cs content:

 

Using System; using System. collections. generic; using System. linq; using System. text; using System. configuration; using MySQLDriverCS; using System. data; namespace Demo {class SqlHelper {private static string server = ConfigurationManager. connectionStrings [server]. connectionString; private static string database = ConfigurationManager. connectionStrings [database]. connectionString; private static string login = ConfigurationManager. connectionStrings [login]. connectionString; private static string password = ConfigurationManager. connectionStrings [password]. connectionString; public static int ExecuteNoQuery (String SQL, MySQLParameter [] parameters) {using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (server, database, login, password ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); // connection statement and SQL MySQLCommand cmd = new MySQLCommand (SQL, conn); // Add the cmd parameter. parameters. addRange (parameters); // return the execution result return cmd. executeNonQuery () ;}} public static object ExecuteScalar (String SQL, MySQLParameter [] parameters) {using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (server, database, login, password ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); MySQLCommand cmd = new MySQLCommand (SQL, conn); // Add the cmd parameter. parameters. addRange (parameters); return cmd. executeNonQuery () ;}}// public static DataTable ExecuteReaderEx (String SQL, MySQLParameter [] parameters) {using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (server, database, login, password ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); MySQLCommand cmd = new MySQLCommand (SQL, conn); // Add the cmd parameter. parameters. addRange (parameters); MySQLDataAdapter mda = new MySQLDataAdapter (cmd); // The queried data exists in the DataTable, which can be understood as a virtual table, one row in the DataTable table is a record, and one column is a database Field DataTable dt = new DataTable (); mda. fill (dt); return dt ;}} public static DataSet ExecuteReaderEx2 (String SQL, MySQLParameter [] parameters) {using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (server, database, login, password ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); MySQLCommand cmd = new MySQLCommand (SQL, conn); // Add the cmd parameter. parameters. addRange (parameters); MySQLDataAdapter mda = new MySQLDataAdapter (cmd); // The queried data exists in the DataTable, which can be understood as a virtual table, one row in the DataTable is a record, and the first column is a database Field DataSet ds = new DataSet (); mda. fill (ds); return ds ;}}}}

 

 

Example:

 

 

// SQL statement string SQL = update tbl_sysuser set isActived = @ isActived where id = @ id; int number = SqlHelper. executeNoQuery (SQL, new MySQLParameter [] {new MySQLParameter (@ isActived, YES), new MySQLParameter (@ id, 2)}); Console. writeLine (number of affected rows: + number );


 

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.