C operates on code instances of the mysql database

  • 2020-05-24 06:00:25
  • OfStack

Just look at the code


using System;
using System.Configuration;
using MySql.Data.MySqlClient;
/// <summary>
/// TestDatebase  Summary of 
/// </summary>
public class TestDatebase
{
    public TestDatebase()
    {
        //
        // TODO:  Add the constructor logic here 
        //
    }
    public static void Main(String[] args)
    {
        MySqlConnection mysql = getMySqlCon();
        // The query sql
        String sqlSearch = "select * from student";
        // insert sql
        String sqlInsert = "insert into student values (12,' zhang 3',25,' college ')";
        // Modify the sql
        String sqlUpdate = "update student set name=' li 4' where id= 3";
        // delete sql
        String sqlDel = "delete from student where id = 12";
        // print SQL statements 
        Console.WriteLine(sqlDel);
        //4 Type statement object 
        //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 the 
        mysql.Close();
       String readLine = Console.ReadLine();
    }
    /// <summary>
    ///  To establish 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>
    ///  Creates an execution 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 it 
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getResultset(MySqlCommand mySqlCommand)
    {
        MySqlDataReader reader = mySqlCommand.ExecuteReader();
        try
        {
            while (reader.Read())
            {
                if (reader.HasRows)
                {
                    Console.WriteLine(" Serial number :" + reader.GetInt32(0) + "| The name :" + reader.GetString(1) + "| age :" + reader.GetInt32(2) + "| Record of formal schooling :" + 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>
    ///  Modify the data 
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getUpdate(MySqlCommand mySqlCommand)
    {
        try
        {
            mySqlCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String message = ex.Message;
            Console.WriteLine(" Data modification failed! " + message);
        }
    }
    /// <summary>
    ///  Delete the data 
    /// </summary>
    /// <param name="mySqlCommand"></param>
    public static void getDel(MySqlCommand mySqlCommand)
    {
        try
        {
            mySqlCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            String message = ex.Message;
            Console.WriteLine(" Data deletion failed! " + message);
        }
    }
}


Related articles: