zoukankan      html  css  js  c++  java
  • Dapper基本用法--MySql

    NuGet:

    • Dapper 2.0.35
    • MySql.Data
    • System.Data

    实体(Entity)

    1    public class student
    2     {
    3         public int Id { get; set; }
    4         public string RealName { get; set; }
    5         public DateTime EnrollmenDate { get; set; }
    6     }

    配置文件数据库连接

      <connectionStrings>
        <add name="mysqlConn" connectionString="server=127.0.0.1;database=数据库;User ID=root;password=密码"/>
      </connectionStrings>

    MySql连接字符串

    1 private static string connStr = string.Empty;
    2         public MySqlOperation()
    3         {
    4             connStr = System.Configuration.ConfigurationManager.ConnectionStrings["mysqlConn"].ConnectionString;
    5         }

    查询语句

     1         /// <summary>
     2         /// 查询所有
     3         /// </summary>
     4         /// <returns>lists</returns>
     5         public List<student> Query()
     6         {
     7             if (string.IsNullOrEmpty(connStr))
     8             {
     9                 return new List<student>(); ;
    10             }
    11             try
    12             {
    13                 using (IDbConnection db = new MySqlConnection(connStr))
    14                 {
    15                     db.Open();//打开数据库
    16                     string mysqlStr = "select * from  student";
    17                     List<student> lists = db.Query<student>(mysqlStr).ToList();
    18                     db.Close();//关闭数据库
    19                     return lists;
    20                 }
    21             }
    22             catch (Exception)
    23             {
    24                 return new List<student>();
    25             }
    26         }
    27         /// <summary>
    28         /// 根据Id查询
    29         /// </summary>
    30         /// <param name="id"></param>
    31         /// <returns>lists</returns>
    32         public List<student> Query(string id)
    33         {
    34             if (string.IsNullOrEmpty(connStr))
    35             {
    36                 return new List<student>();
    37             }
    38             try
    39             {
    40                 using (IDbConnection db = new MySqlConnection(connStr))
    41                 {
    42                     db.Open();//打开数据库
    43                     string mysqlStr = "select * from  student where Id=@id";
    44                     List<student> lists = db.Query<student>(mysqlStr, new { Id = id }).ToList();
    45                     db.Close();//关闭数据库
    46                     return lists;
    47                 }
    48             }
    49             catch (Exception)
    50             {
    51                 return new List<student>();
    52             }
    53         }

    插入语句

     1         /// <summary>
     2         /// 插入数据
     3         /// </summary>
     4         /// <param name="student">student</param>
     5         /// <returns>-1:连接字符串为空,-2:新增失败</returns>
     6         public int Insert(student student)
     7         {
     8             if (string.IsNullOrEmpty(connStr))
     9             {
    10                 return -1;
    11             }
    12             try
    13             {
    14                 using (IDbConnection db = new MySqlConnection(connStr))
    15                 {
    16                     db.Open();//打开数据库
    17                     string mysqlStr = "insert into student(Id,RealName,EnrollmenDate) values(@Id,@RealName,@EnrollmenDate)";
    18                     int list = db.Execute(mysqlStr, student);
    19                     db.Close();//关闭数据库
    20                     return list;
    21                 }
    22             }
    23             catch (Exception)
    24             {
    25                 return -2;
    26             }
    27         }

    删除数据

     1         /// <summary>
     2         /// 删除数据
     3         /// </summary>
     4         /// <param name="id"></param>
     5         /// <returns></returns>
     6         public int Delect(string id)
     7         {
     8             if (string.IsNullOrEmpty(connStr))
     9             {
    10                 return -1;
    11             }
    12             try
    13             {
    14                 using (IDbConnection db = new MySqlConnection(connStr))
    15                 {
    16                     db.Open();//打开数据库
    17                     string mysqlStr = "delete from student where Id=@id";
    18                     int list = db.Execute(mysqlStr, new { Id = id });
    19                     db.Close();//关闭数据库
    20                     return list;
    21                 }
    22             }
    23             catch (Exception)
    24             {
    25                 return -2;
    26             }
    27         }

    更新数据

     1         /// <summary>
     2         /// 更新信息
     3         /// </summary>
     4         /// <param name="student"></param>
     5         /// <returns>-1:连接字符串为空,-2:更新失败</returns>
     6         public int Update(student student)
     7         {
     8             if (string.IsNullOrEmpty(connStr))
     9             {
    10                 return -1;
    11             }
    12             try
    13             {
    14                 using (IDbConnection db = new MySqlConnection(connStr))
    15                 {
    16                     db.Open();//打开数据库
    17                     string mysqlStr = "update student set RealName=@RealName where Id=@Id";
    18                     int list = db.Execute(mysqlStr, student);
    19                     db.Close();//关闭数据库
    20                     return list;
    21                 }
    22             }
    23             catch (Exception)
    24             {
    25                 return -2;
    26             }
    27         }
  • 相关阅读:
    C#中设置窗口在最前显示而其他窗口不能使用
    C#中关闭子窗口而不释放子窗口对象的方法
    C#窗体越界时鼠标还能回到初始坐标位置
    C#程序实现软件开机自动启动的两种常用方法
    C# 只开启一个程序,如果第二次打开则自动将第一个程序显示到桌面
    图标库网址收藏
    C# Winform打包部署时添加注册表信息实现开机自启动
    C# winform程序怎么打包成安装项目(VS2010图解)
    数据库的三级范式,涉及范式的问题
    基数排序
  • 原文地址:https://www.cnblogs.com/HYJ0201/p/12905719.html
Copyright © 2011-2022 走看看