How to call MySQL database under. net

Source: Internet
Author: User

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.

 

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.