使用DLINQ查询数据库,定义实体类

DLNQ是LINQ的扩展形式,用于查询和操作数据库的内容,DLINQ是在ADO.NET的基础上建立起来的。DLINQ提供了一个高级的抽象,使你不再需要操心具体如何操作ADO.NET Command对象,不再需要 操心如何遍历有DataReader对象返回的结果集,也不再需要操心如何使用各种GetXXX()方法来逐列的获取数据。

 

总之,DLINQ为我们访问数据库提供了极大的方便。

 

使用DLINQ之前,我们需要创建自己定义的实体类,实体类必须是一个可枚举对象集合,这些类直接映射到数据库中的表,对应数据库中表的列,他们成为实体类。

 

先简单的看一个实体类的定义:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq.Mapping;namespace DataContextTest
{
    [Table(Name="student")]
    public class Student
    {
        [Column(IsPrimaryKey = false)]
        public string id { get; set; }

        [Column]
        public string name { get; set; }

        [Column]
        public int age { get; set; }

        [Column]
        public string mentor { get; set; }

        [Column]
        public string infor { get; set; }
    }
}


 

 

在实体类的定义中,通过一些由 [ ] 指定的属性来标识实体类的相关信息。在这个实体类类名上面,[Table(Name="student")] 这个属性指定了当前类属于一个实体类,这个实体类对应数据库中的表是“student”表, 然后每个属性前面还有属性声明,[Colunn]属性声明这个属性为对应数据库表中的列,注意看,这个对应的列是通过属性来标识的,属性可以设置 get,set,表示可以通过这个实体类对这个列进行的操作,get,set都有的话表示既可以读取,也可以修改,如果只有get声明,表示只能对这个列进行读取操作。

 

这样一个简单的实体类就定义好了,通过实体类,我们可以定义要操作的数据库中表的哪些列进行操作。比以前的 ADO.NET操作数据库要方便直观的多。

 

定义好实体类以后就可以通过DataContext来访问数据库了。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace DataContextTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "Integrated Security=true; Initial Catalog=test; Data Source=(local)";
            DataContext context = new DataContext(connectionString);

            Table<Student> students = context.GetTable<Student>();

            foreach (var student in students)
            {
                Console.WriteLine("");
                Console.WriteLine(student.id);
                Console.WriteLine(student.name);
                Console.WriteLine(student.age);
                Console.WriteLine(student.mentor);
                Console.WriteLine(student.infor);

                // pay attention here!!!!! foreach returns a read-only assembly, you can not change the data here, 
                // then this SubmitChanges() takes no effect.
                student.age = 111;
                context.SubmitChanges();
                
            }

            var temp = from s in students
                       where s.name == "test"
                       select s;
            foreach (var a in temp)
            {
                Console.WriteLine(a.name);
            }
            context.SubmitChanges();

            Table<teacher> teachers = context.GetTable<teacher>();
            foreach(var t in teachers)
            {
                Console.WriteLine("///");
                Console.WriteLine(t.id);
                Console.WriteLine(t.name);


            }

            var temp_ext = from p in teachers
                       where p.id == "10001"
                       select p;
            // foreach returns a read-only assembly. u can't change data in the foreach operation.

            teacher tt = teachers.Single(p=>p.id=="10001");
            tt.name = "WeiSONG";
            
            context.SubmitChanges();
            Console.WriteLine("job done!");
        }
    }
}


DataContext context = new DataContext(connectionString); 建立对数据库的连接。

Table<Student> students = context.GetTable<Student>(); 查询数据库,返回所有的和实体类对应的行。行中包含的列即为在实体类中定义的列。

 

特别需要注意的是,context通过调用SubmitChanges()可以对数据库进行修改,但是foreach返回的是一个可枚举的只读集合,所以在上面的代码中,你会注意到foreach中对数据库的修改都有发生作用。

 

teacher是我定义的另外一个实体类:

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

namespace DataContextTest
{
    [Table(Name="teacher_test")]
    public class teacher
    {
        [Column(IsPrimaryKey = true)]
        public string id { get; set; }

        [Column]
        public string name { get; set; }
    }
}


 

这里是为了测试通过DataContext对数据库进行修改。

teacher tt = teachers.Single(p=>p.id=="10001"); 这个操作返回的是一个单一的实体类对象,这样我们可以对数据库进行修改。

tt.name = "WeiSONG";                        

context.SubmitChanges();

 

简单的DLINQ就完成了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值