前言:
前面说了数据库的连接查询,现在说数据库的增删改。这里引入一个数据库的实体类,就是将当前数据库的某一个表里面所有字段写成实体类,如下:

1.数据库的实体类:

需要项目里下载Chloe.dll和Chloe.Mysql.dll,如下:

例如有表如下:

它的实体类就是:



using System;
using Chloe.Entity;
using Chloe.Annotations;
namespace WpfApp1
{
/// <summary>
/// 实体类tb_a。(属性说明自动提取数据库字段的描述信息)
/// </summary>
[TableAttribute("tb_a")]
[Serializable]
public partial class Tb_A
{
#region Model
private int _ID;
private string _Name;
private int? _Age;
private int? _Sex; /// <summary>
/// ID
/// </summary>
[ColumnAttribute(IsPrimaryKey = true)]
public int ID
{
get { return _ID; }
set
{
this._ID = value;
}
}
/// <summary>
/// 名字
/// </summary>
public string Name
{
get { return _Name; }
set
{
this._Name = value;
}
}
/// <summary>
/// 年龄
/// </summary>
public int? Age
{
get { return _Age; }
set
{
this._Age = value;
}
}
/// <summary>
/// 性别
/// </summary>
public int? Sex
{
get { return _Sex; }
set
{
this._Sex = value;
}
}
#endregion
}
}

2.增加一行数据:

封装一下IDbConnectionFactory。

        public class MySqlConnectionFactory : IDbConnectionFactory
{
string _connString = null;
public MySqlConnectionFactory(string connString)
{
this._connString = connString;
}
public IDbConnection CreateConnection()
{
IDbConnection conn = new MySqlConnection(this._connString);
return conn;
}
}

插入一行数据方法:

        internal static bool InsertNewData(Tb_A Model, out long lID)
{
MySqlContext context = new MySqlContext(new MySqlConnectionFactory("host = 数据库IP; Port = 数据库端口; Database = 数据库名; uid = 数据库账户; pwd = 数据库密码; Charset = utf8; Allow User Variables = true"));
using (MySqlContext dx = context)
{
IQuery<Tb_A> q = dx.Query<Tb_A>();
lID = dx.Insert<Tb_A>(Model).ID;
return true;
}
}

调用:

        private void Button_Click(object sender, RoutedEventArgs e)
{
Tb_A Model = new Tb_A()
{
Age = 18,
Name = "张三",
Sex = 1,
};
long ID = 0;
InsertNewData(Model,out ID);
}

结果:

3.修改某一行数据:

方法:

        internal static bool UpdateName(string name,long ID)
{
try
{
MySqlContext context = new MySqlContext(new MySqlConnectionFactory("host = 数据库IP; Port = 数据库端口; Database = 数据库名; uid = 数据库账户; pwd = 数据库密码; Charset = utf8; Allow User Variables = true"));
using (MySqlContext dx = context)
{
return dx.Update<Tb_A>(a => a.ID == ID, a => new Tb_A()
{
Name = name, }) >= 0;
}
}
catch (Exception)
{
return false;
}
}

调用:

        private void Button_Click(object sender, RoutedEventArgs e)
{
Tb_A Model = new Tb_A()
{
Age = 18,
Name = "张三",
Sex = 1,
};
long ID = 1;
UpdateName("李四" , ID);
}

结果:

最新文章

  1. 数百个 HTML5 例子学习 HT 图形组件 – WebGL 3D 篇
  2. Saltstack之Syndic(十)
  3. struts2框架 初始别
  4. VC++ 限制窗口的大小范围的方法
  5. JavaScript中的String对象
  6. Reference in the manifest does not match the identity of the downloaded assembly
  7. progressBar 自定义
  8. 基于matlab的GUI界面开发软件
  9. 浏览器的重绘(repaints)与重排(reflows)
  10. Js中Array数组学习总结
  11. Linux 使用 cp 命令强制覆盖功能
  12. 搭建vue环境
  13. codeforces570C
  14. 数字证书及CA的扫盲介绍
  15. html之间传递参数
  16. DB2的进程/线程解析(转)
  17. posix信号量与互斥锁
  18. MySQL测试报告
  19. Splunk Web页面的登录密码忘记了怎么办
  20. php iconv() : Detected an illegal character in input string

热门文章

  1. 题目集7-9总结性Blog
  2. Linux 第四节(shell脚本,IF,do,for)
  3. GNN学习(一):基础知识
  4. 【服务器数据恢复】HP EVA存储多块硬盘离线的数据恢复案例
  5. P5737 闰年展示
  6. php不缓存直接输出
  7. 查找数组中某个元素出现的次数,例如数组arr=[1,2,3,4,3,4,5,3]中target=3出现的次数
  8. Oracle函数NULLIF
  9. window操作
  10. navicat图形工具和pymysql模块的使用