LINQ操作Access数据库

摘要 : LINQ operations Access database



从理论上讲LINQ to SQL里面都是用的DbConnection,而不是SQLConnection,所以Linq支持所有的数据库类型的。

LinqToAccessDB主要包括一个实现IProvider的类,以支持Access数据库的查询。

ReflacLinq是一个从System.Data.Linq“反射出来的项目,里面的各个类与“System.Data.Linq”命名空间里的类相对应。

1、 先将Access 导入到SQLServer中,因为只能从SQLServer 生成实体模型。导过来之后索引,键都会流失,所以还要设置表的主键。

2、建立项目,引用LinqToAccessDB.dll,ReflectLinq.dll两个dll文件。在附件里下载。

3、创建dbml文件,在这里我们是以Northwind为例的,从SQLServer里,找出Northwind项目,然后直接把它拖到dbml的面板上去。如下图所示。



4、修改所生成的类。把生成的类的属性面板里,去掉“Source”属性中 dbo前缀,例如:dbo.Shippers。如果“Source”带有“[ ]”,还必须把“[ ] ”给去掉,在这里,我们要把dbo.[Order Detail]改成:Order Detail,同时,为了方便我们时行数据导入,把“Auto DbGeneratedValue”属性设为“False”, Auto-Sync”属性设为“Never”。



5、新建一个类NorthwindDataBase,并且继承ReflectLinq.DataContext (相当于 System.Data.Linq.DataContext),并且指定Provider为指定为“AccessDbProvider

[Provider(typeof(AccessDbProvider))]
public class
NorthwindDataBase : ReflectLinq.DataContext
{
}


6、建表。打开“Northwind.designer.cs”文件,把关于Table的属性都Copy过来,然后把“System.Data.Linq.Table”改成“RefectLinq.Table”。示例代码如下:

using System.Data;
using
System.Data.Common;
using
System.Data.Linq.Mapping;
using
System.Data.OleDb;
using
LinqToAccessDB;

namespace
NorthwindDemo
{
[Provider(typeof
(AccessDbProvider))]
public class
NorthwindDataBase :
{
public
NorthwindDataBase()
: base(CreateConnection(@"C:/Northwind4.mdb"
))
{
}

private static DbConnection CreateConnection(string
fileName)
{
var builder = new
OleDbConnectionStringBuilder
{
DataSource = fileName,
Provider = "Microsoft.Jet.OLEDB.4.0"

};
return new OleDbConnection(builder.ConnectionString);
}

public
ReflectLinq.Table<Category> Categories
{
get

{
return this.GetTable<Category>();
}
}

public
ReflectLinq.Table<Territory> Territories
{
get

{
return this.GetTable<Territory>();
}
}

public
ReflectLinq.Table<CustomerCustomerDemo> CustomerCustomerDemos
{
get

{
return this.GetTable<CustomerCustomerDemo>();
}
}

public
ReflectLinq.Table<CustomerDemographic> CustomerDemographics
{
get

{
return this.GetTable<CustomerDemographic>();
}
}

public
ReflectLinq.Table<Customer> Customers
{
get

{
return this.GetTable<Customer>();
}
}

public
ReflectLinq.Table<Employee> Employees
{
get

{
return this.GetTable<Employee>();
}
}

public
ReflectLinq.Table<EmployeeTerritory> EmployeeTerritories
{
get

{
return this.GetTable<EmployeeTerritory>();
}
}

public
ReflectLinq.Table<OrderDetail> Order_Details
{
get

{
return this.GetTable<OrderDetail>();
}
}

public
ReflectLinq.Table<Order> Orders
{
get

{
return this.GetTable<Order>();
}
}

public
ReflectLinq.Table<Product> Products
{
get

{
return this.GetTable<Product>();
}
}

public
ReflectLinq.Table<Region> Regions
{
get

{
return this.GetTable<Region>();
}
}

public
ReflectLinq.Table<Shipper> Shippers
{
get

{
return this.GetTable<Shipper>();
}
}

public
ReflectLinq.Table<Supplier> Suppliers
{
get

{
return this.GetTable<Supplier>();
}
}
}


}


7、建立数据库

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

namespace
NorthwindDemo
{
public class
Program
{
public static void
Main()
{
var dataBase = new
NorthwindDataBase() { Log = Console.Out };
if
(dataBase.DatabaseExists())
dataBase.DeleteDatabase();
dataBase.CreateDatabase();
}
}
}




8、导入数据,测试一下。

public static void Main()
{
var dataBase = new
NorthwindDataBase() { Log = Console.Out };
if
(dataBase.DatabaseExists())
dataBase.DeleteDatabase();
dataBase.CreateDatabase();
ImportData(dataBase);
Test(dataBase);
dataBase.Connection.Close();
}


其他的自己慢慢试。

接下来我们将要介绍Ado.netEntityFramework Provider for Access

上一篇 :C#Winfrom点击DataGridView单选整行
下一篇 :C#压缩和修复Access数据库