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 );