[C #] [to] access with MySQL connection

Source: Internet
Author: User

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 #] [to] access with MySQL connection

Related Article

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.