LINQ用于数据库访问的基本方法示例
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
namespace LINQ_TO_SQL
{
[Table(Name = "t_guestinfo")]
public class GuestInfo
{
[Column(IsPrimaryKey = true, Name = "ab_id")]
private string id;
public string ID
{
get { return this.id; }
set { this.id = value; }
}
private string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
}
class Program
{
static void Main(string[] args)
{
DataContext db = new DataContext("c:\\DbAddressBook.mdf");
Table<GuestInfo> guestinfo = db.GetTable<GuestInfo>();
IQueryable<GuestInfo> query =
from g in guestinfo
where g.Name == "xiao"
select g;
foreach (GuestInfo guest in query)
{
Console.WriteLine("ID={0},Name ={1}", guest.ID, guest.Name);
}
}
}
}