using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
Using System. Linq;
Using System. The Text;
using System.Threading.Tasks;
Using System. Windows. Forms;
Using MySql. Data. MySqlClient;
The namespace project1
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}
Private void button1_Click (object sender, EventArgs e)
{
//MySql Data. MySqlClient. MySqlConnection conn1;
String connStr="server=128.0.0.1; User=root; The database=mytest; Port=3306; Password=root; ";
MySqlConnection conn=new MySqlConnection (connStr);
Task. Factory. StartNew (()=& gt;
{
Try
{
Conn. The Open ();
MessageBox. Show (" database connection success!" );
//int current=5;
//int vol=6;
////int temp=7;
//MySqlCommand CMD=new MySqlCommand (" insert into user set current='" + current + "' "+", vol='" + vol + "' ", conn);
String SQL="insert into analog (current, vol) values (' 2 ', '83')";//write
//string SQL="update analog to set the current='0', vol='0' where id='1'";//change
//string SQL="delete from analog to the where id='1'";//delete
MySqlCommand CMD=new MySqlCommand (SQL, conn);
CMD. ExecuteNonQuery ();
//Perform database operations
}
The catch (MySqlException ex)//(Exception ex)
{
MessageBox. Show (" the database connection failed!" );
The switch (ex. Number)
{
Case 0:
//the Console. WriteLine (" always connect to the server. Contact administrator ");
MessageBox. Show (" is always the connect to server. Contact the administrator ");
break;
Case: 1045
//the Console. WriteLine (" Invalid username/password, both please try again ");
MessageBox. Show (" Invalid username/password, both please try again ");
break;
}
}
Conn. Close ();
});
//try
//{
////conn. The Open ();
//MessageBox. Show (" database connection success!" );
////int current=5;
////int vol=6;
//////int temp=7.
////MySqlCommand CMD=new MySqlCommand (" insert into user set current='" + current + "' "+", vol='" + vol + "' ", conn);
//string SQL="insert into analog (current, vol) values (' 2 ', '83')";//write
////string SQL="update analog to set the current='0', vol='0' where id='1'";//change
////string SQL="delete from analog to the where id='1'";//delete
//MySqlCommand CMD=new MySqlCommand (SQL, conn);
//CMD ExecuteNonQuery ();
////Perform database operations
//}
//catch (MySqlException ex)//(Exception ex)
//{
////MessageBox. Show (" database connection failed!" );
//the switch (ex. Number)
//{
//case 0:
////the Console. WriteLine (" always connect to the server. Contact the administrator ");
//MessageBox. Show (" always connect to the server. Contact the administrator ");
//break;
//case, 1045:
////the Console. WriteLine (" Invalid username/password, both please try again ");
//MessageBox. Show (" Invalid username/password, both please try again ");
//break;
//}
//}
//conn. Close ();
}
Private async void button2_Click (object sender, EventArgs e)
{
String connStr="server=128.0.0.1; User=root; The database=mytest; Port=3306; Password=root; ";
MySqlConnection conn=new MySqlConnection (connStr);
Try
{
Await conn. OpenAsync ();
String SQL="select * from analog";
MySqlCommand CMD=new MySqlCommand (SQL, conn);
MySqlDataReader reader=CMD. ExecuteReader ();//execution ExecuteReader () returns a MySqlDataReader object
While (reader. The Read (),//the initial index is 1, Read the next row data, the return value is a bool
{
//the Console. WriteLine (reader [0]. The ToString () + reader [1]. The ToString () + reader [2]. The ToString ());
//the Console. WriteLine (reader. GetInt32 (0) + reader. Get string (1) + reader. Get string (2));
//the Console. WriteLine (reader. GetInt32 (" userid ") + reader. Get string (" username ") + reader. Get string (" password "));//"userid" is the corresponding database column names, recommend this way
TextBox1. AppendText (" id="+ reader. GetInt16 (" id") + "is the current=" + reader. GetInt16 (" current ") + "vol=" + reader. GetInt16 (" vol ") + "\ r \ n");
}
//Perform database operations
}
The catch (MySqlException ex)//(Exception ex)
{
//MessageBox. Show (" database connection failed!" );
The switch (ex. Number)
{
Case 0:
//the Console. WriteLine (" always connect to the server. Contact administrator ");
//MessageBox. Show (" always connect to the server. Contact the administrator ");
break;
Case: 1045
//the Console. WriteLine (" Invalid username/password, both please try again ");
//MessageBox. Show (" Invalid username/password, both please try again ");
break;
}
}
Conn. Close ();
}
}
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull