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