C # Connect to MySQL

Source: Internet
Author: User

How does C # manipulate MySQL repositories?

MySQL is a connector for. NET (Connector), in fact, some. NET DLLs, you can go to https://dev.mysql.com/downloads/connector/net/this address to find the corresponding. NET version of the connector.

Since the. NET version of my current computer is 4.5.0, I should download the 6.9.11 version of the connector:

The specific connector corresponds. NET version can be viewed by opening https://dev.mysql.com/doc/connector-net/en/connector-net-versions.html:

When you are done downloading and installing, go to the installation root of the connector

Add this DLL to your C # project as a reference and using the MySQL namespace:

Examples of use are as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;namespacemysqltest{ Public Partial classform1:form {Form2 FormMain=NewForm2 (); Mysqlconnection M_conn=Newmysqlconnection ();  PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {m_conn.connectionstring="Server=localhost; Port=3306;database=test; User=root; Password=root;"; }        Private voidButton1_Click (Objectsender, EventArgs e) {            Try            {                stringsql =""; using(Mysqlcommand cmd =NewMysqlcommand ()) {                    if(M_conn.state! =ConnectionState.Open) {m_conn.open (); } SQL="Select Name,pass from userinfo where [email protected] and [email protected]"; Cmd. Parameters.addwithvalue ("@NAME", TextBox1.Text.Trim ()); Cmd. Parameters.addwithvalue ("@PASSWD", TextBox2.Text.Trim ()); Cmd. Connection=M_conn; Cmd.commandtext=SQL; Mysqldatareader Reader=cmd.                    ExecuteReader (); if(reader. Read ()) {if(reader["name"]. ToString (). CompareTo (TextBox1.Text.Trim ()) = =0&& reader["Pass"]. ToString (). CompareTo (TextBox2.Text.Trim ()) = =0 )                        {                             This.                            Hide ();                            Formmain.showdialog ();  This.                        Show (); }                    }                    Else{MessageBox.Show ("account number or password error, please check! ","Warning", MessageBoxButtons.OK, messageboxicon.warning); } reader.                Close (); }            }            Catch(Exception) {Throw; }        }        Private voidButton2_Click (Objectsender, EventArgs e) {             This.        Close (); }        Private voidForm1_formclosed (Objectsender, Formclosedeventargs e) {            Try            {                if(M_conn.state! =connectionstate.closed) {m_conn.close (); }            }            Catch(Exception) {Throw; }        }        Private voidTextbox2_keypress (Objectsender, KeyPressEventArgs e) {            if(E.keychar = = -) {button1_click (sender, E); }        }    }}

C # Connect to MySQL

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.