zoukankan      html  css  js  c++  java
  • Dapper的应用

    Dapper是一个轻型的开源ORM类,代码就一个SqlMapper.cs文件

    using Dapper;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    
    namespace WcfServiceTest
    {
    /// <summary>
    /// Dapper案例
    /// </summary>
    public class StudentDB
    {
    //获取web.config里的连接字符串
    public static string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectstring"];
    
    /// <summary>
    /// 查询指定数据
    /// </summary>
    /// <param name="person"></param>
    /// <returns></returns>
    public static List<Student> Query(string name)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    string strsql = "select * from Student where 1=1 ";
    if (!string.IsNullOrEmpty(name))
    {
    strsql += "and Name like '%@Name%'";
    }
    return connection.Query<Student>(strsql, name).ToList();
    }
    }
    
    /// <summary>
    /// 反填
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static Student QueryById(int id)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Query<Student>("select * from Student where Id=@Id", id).SingleOrDefault();
    }
    }
    
    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="student"></param>
    /// <returns></returns>
    public static int Insert(Student student)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("insert into Student(Name,Age) values(@Name,@Age)", student);
    }
    }
    
    public static int Update(Student student)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("update Studentset Name = @Name,Age= @Age where Id=@Id", student);
    }
    }
    
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="student"></param>
    /// <returns></returns>
    public static int Delete(int Id)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("delete from Student where Id=@Id", Id);
    }
    }
    
    /// <summary>
    /// 批量删除
    /// </summary>
    /// <param name="students"></param>
    /// <returns></returns>
    public static int Delete(List<Student> students)
    {
    using (IDbConnection connection = new SqlConnection(connectionString))
    {
    return connection.Execute("delete from Student where Id=@Id", students);
    }
    }
    
    }
    }
  • 相关阅读:
    606. Construct String from Binary Tree
    557. Reverse Words in a String III
    551. Student Attendance Record I
    541. Reverse String II
    521. Longest Uncommon Subsequence I
    520. Detect Capital
    459. Repeated Substring Pattern
    人脸检测源码facedetection
    人脸检测的model类facemodel
    人脸检测解析json的工具类face_test
  • 原文地址:https://www.cnblogs.com/dujian123/p/10722644.html
Copyright © 2011-2022 走看看