zoukankan      html  css  js  c++  java
  • C#------对SQLServer进行简单的增,删,改,查

    EF中的操作
    转载:
    http://www.cnblogs.com/mcgrady/archive/2015/03/21/4355282.html

    PSContext db = new PSContext(); //遍历所有数据,根据id排序(查) var time = db.Timetables.AsNoTracking().OrderBy(p => p.id).ToList(); foreach (var t in time) {   Console.WriteLine("{0},{1}",t.id,t.time); }

    //遍历所有数据,根据ID倒序排序(查) var student = db.Students.AsNoTracking().OrderByDescending(p => p.ID).ToList(); foreach (var s in student) {   Console.WriteLine("{0},{1}", s.ID, s.Name); } //从ID为5的数据开始,往后遍历4个数据(查) var student = db.Students.OrderBy(p => p.ID).Where(p => p.ID > 4).Skip(4).Take(4).ToList(); foreach (var s in student) {   Console.WriteLine("{0},{1}", s.ID, s.Name); } //往数据库添加数据(增) var teacher = new Teacher(); teacher.ID = 2432; teacher.Name = "绿豆"; db.Teachers.Add(teacher); db.SaveChanges();
    //对Name为qwe的项进行修改(改) var teacher = db.Teachers.Single(u => u.Name == "菠萝"); teacher.Name = "苹果"; db.SaveChanges();
    //对指定列进行删除(删) Student student = db.Students.Find(5); Console.WriteLine("{0},{1}",student.ID,student.Name); db.Students.Attach(student); db.Students.Remove(student); db.SaveChanges();
  • 相关阅读:
    手机网络制式常识
    合并两个有序数组a和b到c
    N皇后问题
    数独求解
    ARM处理器模式
    ARM异常中断处理
    国风·召南·野有死麕
    八大排序算法
    1045 Favorite Color Stripe (最长不下降子序列 LIS 或最长公共子序列 LCS)
    1066 Root of AVL Tree (模拟AVL建树)
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/5717618.html
Copyright © 2011-2022 走看看