Using System;
Using System.Configuration;
Using MySql.Data.MySqlClient;
<summary>
Summary description of Testdatebase
</summary>
public class Testdatebase
{
Public Testdatebase ()
{
//
TODO: Add constructor logic here
//
}
public static void Main (string[] args)
{
mysqlconnection MySQL = Getmysqlcon ();
Querying SQL
String sqlsearch = "SELECT * from student";
Insert SQL
String Sqlinsert = "INSERT into student values (12, ' Zhang San ', 25, ' College ')";
Modify SQL
String sqlupdate = "Update student set Name= ' John Doe ' where id= 3";
Delete SQL
String Sqldel = "Delete from student where id = 12";
Print SQL statements
Console.WriteLine (Sqldel);
Four types of statement objects
Mysqlcommand Mysqlcommand = Getsqlcommand (Sqlsearch, MySQL);
Mysqlcommand Mysqlcommand = Getsqlcommand (Sqlinsert, MySQL);
Mysqlcommand Mysqlcommand = Getsqlcommand (sqlupdate, MySQL);
Mysqlcommand Mysqlcommand = Getsqlcommand (Sqldel, MySQL);
Mysql. Open ();
Getresultset (Mysqlcommand);
Getinsert (Mysqlcommand);
GetUpdate (Mysqlcommand);
Getdel (Mysqlcommand);
Remember to close
Mysql. Close ();
String readLine = Console.ReadLine ();
}
<summary>
Build a MySQL database link
</summary>
<returns></returns>
public static mysqlconnection Getmysqlcon ()
{
String mysqlstr = "Database=test;data source=127.0.0.1; User Id=root; Password=root;pooling=false; charset=utf8;port=3306 ";
String Mysqlcon = configurationmanager.connectionstrings["Mysqlcon"]. ConnectionString;
mysqlconnection mysql = new mysqlconnection (MYSQLSTR);
return MySQL;
}
<summary>
Creating an Execute Command statement object
</summary>
<param name= "SQL" ></param>
<param name= "MySQL" ></param>
<returns></returns>
public static Mysqlcommand Getsqlcommand (String sql,mysqlconnection MySQL)
{
Mysqlcommand Mysqlcommand = new Mysqlcommand (sql, MySQL);
Mysqlcommand Mysqlcommand = new Mysqlcommand (SQL);
mysqlcommand.connection = MySQL;
return mysqlcommand;
}
<summary>
Query and get the result set and traverse
</summary>
<param name= "Mysqlcommand" ></param>
public static void Getresultset (Mysqlcommand mysqlcommand)
{
Mysqldatareader reader = Mysqlcommand.executereader ();
Try
{
while (reader. Read ())
{
if (reader. HasRows)
{
Console.WriteLine ("Number:" + reader.) GetInt32 (0) + "| Name:" + Reader. GetString (1) + "| Age:" + reader. GetInt32 (2) + "| Education:" + reader. GetString (3));
}
}
}
catch (Exception)
{
Console.WriteLine ("The query failed! ");
}
Finally
{
Reader. Close ();
}
}
<summary>
Add data
</summary>
<param name= "Mysqlcommand" ></param>
public static void Getinsert (Mysqlcommand mysqlcommand)
{
Try
{
Mysqlcommand.executenonquery ();
}
catch (Exception ex)
{
String message = ex. Message;
Console.WriteLine ("Insert data failed!") "+ message);
}
}
<summary>
modifying data
</summary>
<param name= "Mysqlcommand" ></param>
public static void GetUpdate (Mysqlcommand mysqlcommand)
{
Try
{
Mysqlcommand.executenonquery ();
}
catch (Exception ex)
{
String message = ex. Message;
Console.WriteLine ("Failed to modify the data!") "+ message);
}
}
<summary>
Delete data
</summary>
<param name= "Mysqlcommand" ></param>
public static void Getdel (Mysqlcommand mysqlcommand)
{
Try
{
Mysqlcommand.executenonquery ();
}
catch (Exception ex)
{
String message = ex. Message;
Console.WriteLine ("Delete data failed!") "+ message);
}
}
}
C # connecting MySQL instances