码迷,mamicode.com
首页 > 数据库 > 详细

.NET环境下,通过LINQ操作SQLite数据库

时间:2014-06-06 07:34:05      阅读:413      评论:0      收藏:0      [点我收藏+]

标签:c   class   a   ext   get   数据   

//对应数据库中的某个表

[Table(Name = "main.Student")]
    public class Student
    {
        [Column(Name = "StuName", DbType = "varchar(50)")]
        public string StuName { get; set; }

        [Column(Name = "StuNo", DbType = "varchar(20)", IsPrimaryKey = true)]
        public string StuNo { get; set; }

    }

 

  public class SqliteDataContext : DataContext
    {
        public Table<Student> Students
        {
            get
            {
                return GetTable<Student>();
            }
        }

        public SqliteDataContext(IDbConnection connection) :
            base(connection)
        {
        }
    }

 

      #region LINQ操作SQLITE数据库
            SqliteDataContext context = new SqliteDataContext(sqlite.Connecttion);

            /*
             * 查询及插入数据
             */
            var stu = context.Students.SingleOrDefault(item => item.StuName.Equals("飞飞"));
            if (stu == null)
            {
                context.Students.InsertOnSubmit(new Student() {StuNo = "110525",StuName = "飞飞" });
                context.SubmitChanges();
            }
       

            /*
             * 查询及删除数据
             */
            stu = context.Students.Single(item => item.StuName.Equals("飞飞"));
            if (stu != null)
            {
                context.Students.DeleteOnSubmit(stu);
                context.SubmitChanges();
            }
          

            /*
             * 查询及修改数据
             */
            stu = context.Students.Single(item => item.StuName.Equals("飞飞"));
            if (stu != null)
            {
                stu.StuName = "吴高飞";
                context.SubmitChanges();
            }
            

            Console.ReadKey();

            #endregion

.NET环境下,通过LINQ操作SQLite数据库,布布扣,bubuko.com

.NET环境下,通过LINQ操作SQLite数据库

标签:c   class   a   ext   get   数据   

原文地址:http://www.cnblogs.com/JustYong/p/3767605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!