WPF PC 8 - C# and MySQL

ADO.NET

insert image description here

Database Connectivity

insert image description here
insert image description here

Data insertion, deletion, modification

insert image description here
insert image description here

data query

insert image description here
insert image description here

insert image description here

with a single parameter

insert image description here
insert image description here

with multiple parameters

insert image description here
insert image description here

      using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace WpfTest
{
    
    
    public class SqlHelper
    {
    
    
        private static string constr = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
        /// <summary>
        /// 连接数据库
        /// </summary>
        /// <returns></returns>
        public MySqlConnection? ConnectDatabase()
        {
    
    
            MySqlConnection connection = new MySqlConnection(constr);
            connection.Open();
            if (connection != null && connection.State == System.Data.ConnectionState.Open)
            {
    
    
                Console.WriteLine("数据库连接成功");
            }
            else
            {
    
    
                Console.WriteLine("数据库连接失败");
            }
            return connection;
        }
        /// <summary>
        /// 关闭数据库
        /// </summary>
        /// <param name="mySqlConnection"></param>
        public void CloseConnection(MySqlConnection mySqlConnection)
        {
    
    
            if (mySqlConnection != null && mySqlConnection.State == System.Data.ConnectionState.Open)
            {
    
    
                mySqlConnection.Close();
                mySqlConnection = null!;
            }
        }/// <summary>
        /// 执行数据DML(insert,update,delete)操作
        /// </summary>
        /// <param name="sql"></param>

        public void ExecuteUpdate(string? sql)
        {
    
    
            var connection = ConnectDatabase();
            MySqlCommand cmd = connection!.CreateCommand();
            cmd.CommandText = sql;
            int row = cmd.ExecuteNonQuery();
            if (row > 0)
            {
    
    
                Console.WriteLine("操作成功");
            }
            else
            {
    
    
                Console.WriteLine("操作失败");
            }
            CloseConnection(connection);
        }
        /// <summary>
        /// 数据查询的调用模板
        /// </summary>
        public void test()
        {
    
    
            MySqlParameter[] parameters = new MySqlParameter[]
            {
    
    
                new MySqlParameter("@height",30),
                new MySqlParameter("@width",50),
            };
            string sql = "select * from tb_name where height=@height and width=@width";
            ExecuteQuery(sql, parameters);

        }
        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="sql"></param>
        public void ExecuteQuery(string sql, MySqlParameter[] mySqlParameters)
        {
    
    
            var connection = ConnectDatabase();
            try
            {
    
    
                MySqlCommand mySqlCommand = new MySqlCommand(sql, connection);
                foreach (MySqlParameter param in mySqlParameters)
                {
    
    
                    mySqlCommand.Parameters.Add(param);
                }
                MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
                while (mySqlDataReader.Read())
                {
    
    
                    mySqlDataReader.GetInt32(0);     // 通过列的索引拿到数据
                    mySqlDataReader.GetString("表字段名1");  //通过列的名称拿到数据
                    mySqlDataReader.GetString("表字段名2");
                }
                mySqlDataReader.Close();
            }
            catch (Exception)
            {
    
    

                throw;
            }
            finally
            {
    
    
                CloseConnection(connection!);
            }


        }

    }
}
 

Based on characteristics + reflection + DAO general operation library

ORM Object Relational Mapping

insert image description here

The table in the database is mapped to an entity class

insert image description here
insert image description here

Call the new method provided in the ORM framework: build sql statement

create enumeration

insert image description here

Create sql statements based on enumeration types

insert image description here
insert image description here
insert image description here
insert image description here

transfer

insert image description here

Entity Qualification

insert image description here
insert image description here
insert image description here
insert image description here

generate parameter list

insert image description here

Generate all methods inserted

insert image description here
insert image description here

Using attributes to map table and field names

insert image description here
insert image description here

Get the real table name by attribute

insert image description here
insert image description here
insert image description here

When the field name is changed, an error will be reported, so add features to the field

insert image description here
insert image description here
insert image description here
insert image description here

modified method

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_42832272/article/details/132089844