I recently worked on a project in my company to build a system. I used C # development,. NET Framework at the front end, and MySQL database at the back end. I used SQL Server for my website, so I don't know much about MySQL, so I don't understand how to call MySql in. net.
I checked some information online. The general method is as follows:
1. Download: Connector/Net is a fully-managed ADO. Net driver for MySQL.
The website is: http://dev.mysql.com/downloads/connector/net/5.0.html
2. After downloading and installing, copy mysql. Data. dll of any version under c: \ Program Files \ mysql connector net 6.3.8 \ assemblies to the project directory and add references.
3.CodeAdd
Using mysql. Data. mysqlclient; using MySQL. Data. types;
Then you can use it.
One person in the company made a CS file interface. Others only need to call the namespace.
Mysqlhelper. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data; using MySQL. data. mysqlclient; using MySQL. data. types; namespace MySQL {public class mysqlhelper {public static int add (string SQL, string connectstring, int timeout) {If (string. isnullorempty (SQL) Return-1; SQL + = "select @ identity"; using (mysqlconnection connect = new mysqlconnection (connectstri NG) {using (mysqlcommand cmd = new mysqlcommand (SQL, connect) {try {connect. open (); cmd. commandtimeout = timeout; mysqldatareader reader = cmd. executereader (commandbehavior. closeconnection); int id =-1; if (reader. read () id = reader. getint32 ("@ identity"); reader. close (); If (ID = 0) id =-1; return ID;} catch {connect. close (); Return-1 ;}}} public static int execute (string SQL, STR Ing connectstring, int timeout) {If (string. isnullorempty (SQL) return 0; using (mysqlconnection connect = new mysqlconnection (connectstring) {using (mysqlcommand cmd = new mysqlcommand (SQL, connect) {try {connect. open (); cmd. commandtimeout = timeout; return cmd. executenonquery ();} catch {connect. close (); Return 0 ;}}} public static int execute (string SQL, string connectstring, int tim Eout, bool usetransaction) {If (string. isnullorempty (SQL) return 0; If (! Usetransaction) return execute (SQL, connectstring, timeout); else {using (mysqlconnection connect = new mysqlconnection (connectstring) {using (mysqlcommand cmd = new mysqlcommand (SQL, connect )) {mysqltransaction transaction = NULL; try {connect. open (); transaction = connect. begintransaction (isolationlevel. readcommitted); cmd. commandtimeout = timeout; cmd. transaction = Transaction; int result = Cmd. executenonquery (); transaction. commit (); return result;} catch {transaction. rollback (); Connect. close (); Return 0 ;}}}} public static bool execute (list <string> sqls, string connectstring, int timeout) {If (sqls. count = 0) return false; using (mysqlconnection connect = new mysqlconnection (connectstring) {mysqltransaction transaction = NULL; try {connect. open (); transaction = connec T. begintransaction (isolationlevel. readcommitted); mysqlcommand cmd = new mysqlcommand (); cmd. connection = connect; cmd. transaction = Transaction; cmd. commandtimeout = timeout; foreach (string SQL in sqls) {cmd. commandtext = SQL; cmd. executenonquery ();} transaction. commit ();} catch {If (transaction! = NULL) transaction. rollback (); Connect. close (); Return false ;}} return true;} public static mysqldatareader query (string SQL, string connectstring, int timeout) {If (string. isnullorempty (connectstring) return NULL; mysqlconnection connect = new mysqlconnection (connectstring); mysqlcommand cmd = new mysqlcommand (SQL, connect); try {connect. open (); cmd. commandtimeout = timeout; mysqldatareader datareader = cmd. executereader (commandbehavior. closeconnection); Return datareader;} catch (exception e) {system. console. writeline (E. message); Connect. close (); return NULL ;}}}}
When calling, add
Using MySQL
You don't need to talk about the rest.