ADO.NET
Database Connectivity
Data insertion, deletion, modification
data query
with a single parameter
with multiple parameters
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
The table in the database is mapped to an entity class
Call the new method provided in the ORM framework: build sql statement
create enumeration
Create sql statements based on enumeration types
transfer
Entity Qualification
generate parameter list
Generate all methods inserted
Using attributes to map table and field names
Get the real table name by attribute
When the field name is changed, an error will be reported, so add features to the field
modified method