zoukankan      html  css  js  c++  java
  • Linq入门实例

     1.数据定义

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;

    namespace QuickStart
    {
        [Table]
        public class Cat
        {
           
            private int catId;

            private string name;
            private char sex;
            private float weight;

            public Cat()
            {
            }
            [Column(IsDbGenerated=true,AutoSync=AutoSync.OnInsert,IsPrimaryKey = true)]       

            public virtual int CatId
            {
                get { return catId; }
                set { catId = value; }
            }
            [Column] 
            public virtual string Name
            {
                get { return name; }
                set { name = value; }
            }
            [Column] 
            public virtual char Sex
            {
                get { return sex; }
                set { sex = value; }
            }
            [Column] 
            public virtual float Weight
            {
                get { return weight; }
                set { weight = value; }
            }

        }
    }

     2.数据访问

    string conn = "Server=(local);initial catalog=Test;uid=sa;pwd=abc123_";
    protected void Page_Load(object sender, EventArgs e)
    {
      DataContext db = new DataContext(conn);
      Table<Cat> catTable = db.GetTable<Cat>();
       Cat c1 = catTable.Where(t => t.CatId == 3).FirstOrDefault();
      Response.Write(c1.Name);       

    }

    删除,修改,查询

     public DataSet GetAll()
    {
      DataSet ds = new DataSet();
      DataContext db = new DataContext(conn);
      Table<Cat> catTable = db.GetTable<Cat>();
        string sql = "select * from cat";
      SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
      adapter.Fill(ds);
      return ds;
    }

    public int DeleteCat(int CatId)
    {
      DataContext db = new DataContext(conn);
      Table<Cat> catTable = db.GetTable<Cat>();
      Cat c1 = catTable.Where(t => t.CatId == CatId).FirstOrDefault();
      catTable.DeleteOnSubmit(c1);
      db.SubmitChanges();
      return 1;
    }

    public int UpdateCat(int CatId,string Name,char Sex,float Weight,int CustomerId)
    {
      DataContext db = new DataContext(conn);
      Table<Cat> catTable = db.GetTable<Cat>();
      Cat c1 = catTable.Where(t => t.CatId == CatId).FirstOrDefault();
      c1.Name = Name;
      c1.Sex = Sex;
      c1.Weight = Weight;
      db.SubmitChanges();
      return 1;

    }

  • 相关阅读:
    [复变函数]第07堂课 2.2 初等解析函数
    [家里蹲大学数学杂志]第237期Euler公式的美
    [家里蹲大学数学杂志]第287期复变函数讲义
    [家里蹲大学数学杂志]第253期实变函数讲义
    模仿王者荣耀的实时阴影
    Android 识别身份证号码(图片识别)
    基于swiper的移动端H5页面,丰富的动画效果
    基于skitter的轮播图炫酷效果,幻灯片的体验
    基于canvas的原生JS时钟效果
    .net core 实现简单爬虫—抓取博客园的博文列表
  • 原文地址:https://www.cnblogs.com/kenny999/p/2300357.html
Copyright © 2011-2022 走看看