C # (. net) MySQL database link tool class
Download and install Mysqldrivercs First
http://sourceforge.net/projects/mysqldrivercs/
found under the installation folder MySQLDriver.dll , and then the MySQLDriver.dll Adding references to the project
Application configuration file:
App. Config:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <connectionStrings> <add name= "Server" connectionstring= "localhost" ></add> <add name= "database" connectionstring= "housing" > </add> <add name= "Login" connectionstring= "root" ></add> <add name= "Password" connectionstring= "root" ></add> </connectionStrings></configuration>
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 = Configuratio nmanager.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 Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn. ExecuteNonQuery (); Connection statements and SQL Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd. Parameters.addrange (Parameters); Returns the execution result of the 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 Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn. ExecuteNonQuery (); Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd. Parameters.addrange (Parameters); return CMD. ExecuteNonQuery (); }}//Less public static DataTable Executereaderex (StringSQL, mysqlparameter[] parameters) {using (mysqlconnection conn = new Mysqlconnection (New Mysqlconnectio Nstring (server, database, login, password). asstring)) {Conn. Open (); Prevent garbled Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn. ExecuteNonQuery (); Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd. Parameters.addrange (Parameters); Mysqldataadapter MDA = new Mysqldataadapter (cmd); The data that is queried is present in a DataTable, and a DataTable can be understood as a virtual table, a behavior in a DataTable, a record, a database field datatable dt = new DataTable () ; Mda. Fill (DT); return DT; }} public static DataSet ExecuteReaderEx2 (String sql, mysqlparameter[] parameters) {Usin G (Mysqlconnection conn = new Mysqlconnection (new mysqlconnectionstring (server, database, login, password). TheTring)) {Conn. Open (); Prevent garbled Mysqlcommand commn = new Mysqlcommand ("Set names gb2312", conn); Commn. ExecuteNonQuery (); Mysqlcommand cmd = new Mysqlcommand (SQL, conn); Add the parameter cmd. Parameters.addrange (Parameters); Mysqldataadapter MDA = new Mysqldataadapter (cmd); The data that is queried is present in a DataTable, and a DataTable can be understood as a virtual table, a behavior in a DataTable, a record, a database field DataSet ds = new DataSet (); Mda. Fill (DS); return DS; } } }}
Examples of Use:
SQL statement String sql = "Update tbl_sysuser set [email protected] where [email protected]"; int number = Sqlhelper.executenoquery (sql, new mysqlparameter[] { new Mysqlparameter ("@isActived", "yes"), new Mysqlparameter ("@id", 2) }); Console.WriteLine ("Rows Affected:" + number);
C # (. net) MySQL database link tool class