C # how to connect to MySQL

Source: Internet
Author: User

The following functions must be implemented:

The gateway sends data to the PC through the serial port. The PC collects data and parses the data and saves it to MySQL. Then, the JSP page reads and displays the data in MySQL.

Therefore, using C # To connect to MySQL DATA becomes a necessary process, which is described in detail here.

1. Download the desired file mysqldrivercs, for: http://sourceforge.net/projects/mysqldrivercs

2, Installation File: MySQLDriverCS-n-EasyQueryTools-4.0.1-DotNet2.0.exe, In the installation process may prompt an error, prohibit writing a DLL file, you can directly ignore it.

3. Add the mysqldriver. dll file under the installation file to the reference of the C # project.

4. Write the public processing class as follows:

(1) first, you must reference the namespace:

Using mysqldrivercs; <br/> using system. Data; <br/> using system. Data. ODBC;

(2) Write Public processing class

Public class sqlhelper <br/>{< br/> mysqlconnection conn = NULL; <br/> mysqlcommand commn = NULL; </P> <p> Public mysqlconnection getconnect () <br/> {<br/> conn = new mysqlconnection (New mysqlconnectionstring ("localhost", "wWater", "root", "123 "). asstring); <br/> return conn; <br/>}</P> <p> Public int runmysql (string SQL) <br/>{< br/> int COUNT = 0; <br/> try <br/>{< br/> conn = getconnect (); <br/> Conn. open (); <br/> commn = new mysqlcommand (SQL, Conn); <br/> COUNT = commn. executenonquery (); <br/> return count; <br/>}< br/> catch (exception ex) <br/>{< br/> throw; <br/>}< br/> finally <br/> {<br/> Conn. close (); <br/>}</P> <p>}

(3) reference the runmysql method in the actual process to insert data.

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.