EXTNET ALinq 【转】

什么是 ALinq


ALinq是一个企业级的database LINQ provider,它稳定、高效、容易使用。使用 ALinq 与Linq To SQL一样,如果你对Linq To SQL很熟悉,那么很快就能学会ALinq。

 

ALinq入门


引用 ALinq 库

1, ALinq库的路径是 <install path>/bin/。

2, Linq to Access 数据库,你需要引用 ALinq.dll、ALinq.Access.dll和System.Data.dll。

3, Linq to SQLite 数据库,你需要引用 ALinq.dll、ALinq.SQLite.dll和System.Data.SQLite.dll。

4, 其他数据库。连接 Oracle,需要引用 ALinq.Oracle.dll 或 ALinq.Oracle.Odp.dll。ALinq.Oracle.dll 中 OracleProvider 类,使用 System.Data.OracleClient 命名空间的 OracleConnection 类连接Oracle数据库;ALinq.Oracle.Odp.dll 中 OracleProvider,使用 Oracle 提供 Oracle.DataAccess 命名空间的 Connection 连接Oracle。

创建 DataContext

AccessDataContext
//Use connection string initialize,and specify the sql provider.
var context = new ALinq.DataContext("C:/Northwind.mdb",
                                     typeof(ALinq.Access.AccessDbProvider));
 
//or use file name initialize the datacontext, 
//the datacontext will specify the sql provider by file extension name.
context = new ALinq.DataContext("C:/Northwind.mdb");
 
//Use connection initialize.
var builder = new OleDbConnectionStringBuilder
{
    DataSource = "C:/Northwind.mdb",
    Provider = "Microsoft.Jet.OLEDB.4.0"
};
var conn = new OleDbConnection(builder.ConnectionString);
context = new ALinq.DataContext(conn, typeof(ALinq.Access.AccessDbProvider));

 

SQLiteDataContext
//Use connection string initialize.
var context = new ALinq.DataContext("C:/Northwind.db",
                                     typeof(ALinq.SQLite.SQLiteProvider));
 
//or use file name initialize the datacontext, 
//the datacontext will specify the sql provider by file extension name.
context = new ALinq.DataContext("C:/Northwind.db");
 
//Use connection initialize.
var builder = new SQLiteConnectionStringBuilder
{
    DataSource = "C:/Northwind.db"
};
var connection = new SQLiteConnection(builder.ToString());
context = new ALinq.DataContext(connection,
                                typeof(ALinq.SQLite.SQLiteProvider));

 

MySqlDataContext
var builder = new MySqlConnectionStringBuilder()
{
    Server = "localhost",
    Port = 3306,
    UserID = "root",
    Password = "test",
    Database = "Northwind"
};
var conn = new MySqlConnection(builder.ToString());
var context = new ALinq.DataContext(conn,
                            typeof(ALinq.MySQL.MySqlProvider));

 

OracleDataContext
var strcon = "Data Source=localhost;Persist Security Info=True;User ID=Northwind;Password=test";
 
//Use connection string initialize,and specify the sql provider.
var context = new ALinq.DataContext(strcon, typeof(ALinq.Oracle.OracleProvider));
 
//or
context = new ALinq.DataContext(strcon, typeof(ALinq.Oracle.Odp.OracleProvider));
 
//Use connection string initialize,and specify the sql provider.
context = new ALinq.DataContext(new System.Data.OracleClient.OracleConnection(strcon),
                                    typeof(ALinq.Oracle.OracleProvider));
//or
context = new ALinq.DataContext(new Oracle.DataAccess.Client.OracleConnection(strcon),
                                    typeof(ALinq.Oracle.Odp.OracleProvider));

 

SqlDataContext

MSSQL 2000、MSSQL 2005 DataContext 初始化。

//MSSQL 2000
var conn = @"Data Source=NOTEBOOK;Initial Catalog=Northwind1;
            Integrated Security=True";
var db = new ALinq.DataContext(new SqlConnection(conn),
                        typeof(ALinq.SqlClient.Sql2000Provider));
Console.WriteLine(db.DatabaseExists());
 
//MSSQL 2005
conn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Northwind.mdf;
             Integrated Security=True;Connect Timeout=30;User Instance=True";
db = new ALinq.DataContext(new SqlConnection(conn),
                     typeof(ALinq.SqlClient.Sql2005Provider));
Console.WriteLine(db.DatabaseExists()); 

 

映射数据库

ALinq 映射与 DLinq一样。

AttributeMapping
[ALinq.Mapping.Table(Name=”Customers”)]
public class Customer
{
    [ALinq.Mapping.Column]
    public string CustomerID;
            
    [ALinq.Mapping.Column]
    public string CompanyName;
            
    [ALinq.Mapping.Column]
    public string ContactName;
            
    [ALinq.Mapping.Column]
    public string City;
}

 

XmlMapping
<?xml version="1.0" encoding="utf-8"?>
<Database Name="NorthwindDatabase" Provider="ALinq.Access.AccessDbProvider" 
          xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
  <Table Name="Customers" Member="Customers">
    <Type Name="ALinqDocument.Customer">
        <Column Member="CustomerID"/>
        <Column Member="CompanyName"/>
        <Column Member="ContactName"/>
        <Column Member="ContactTitle"/>
        <Column Member="Address"/>
        <Column Member="City"/>
        <Column Member="Region"/>
        <Column Member="PostalCode"/>
        <Column Member="Country"/>
        <Column Member="Phone"/>
        <Column Member="Fax"/>
    </Type>
  </Table>
</Database> 

 

查询数据库

var db = new ALinq.DataContext(@"C:/Northwind.mdb");
 
var companyNameQuery = from cust in db.GetTable<Customer>()
                       where cust.City == "London"
                       select cust.CompanyName;
 
foreach (var customer in companyNameQuery)
    Console.WriteLine(customer);
 
//use XmlMappingSource
var xmlMapping = ALinq.Mapping.XmlMappingSource.FromUrl("C:/Northwind.map");
db = new ALinq.DataContext(@"C:/Northwind.mdb", xmlMapping);
 
companyNameQuery = from cust in db.GetTable<Customer>()
                   where cust.City == "London"
                   select cust.CompanyName;
 
foreach (var customer in companyNameQuery)
    Console.WriteLine(customer);

 

为什使用 ALinq


1, 实现Linq to SQL 的所有特色。Linq toSQL 中的所有特色在ALinq中都能找到。

2, 完全与Linq to SQL 兼容。ALinq的API与Linq to SQL 一样,你所知道的Linqto SQL 知识可以直接应用于 ALinq。

3, 支持大多数商业数据库,如Access、MSSQL、Oracle、SQLite、MySQL、Firebird、DB2、PostgreSQL,之后还会支持更多的数据库。

4, 可以运行在 Mono 下。

5, 提供强大ORDesigner。ORDesigner是ALinq 一个实体可视化设计器。它集成在VS2008和VS2010中,你可以更快、更容易地创建丰富的领域驱动模型(domain-driven models)。ORDesigner的主要特点是:支持多个数据库,如Access、SQLite、MySql、Oracle、SQL2000、SQL2005;支持从数据库中拖拽创建实体;创建 Xml 映射文件,更容易写一个独立于数据库应用程序;在ORDesigner资源管理,双击类导航到图表中的实体模型,更容易地在图表中找到实体模型;自定义实体属性(property)的属性(Attribute),调整视图属性(property)的顺序;支持 T4 文本模版。可以自定义生成的代码;图表视图(Diagram View)。可视化一个对象模型有时有点困难。当数据模型比较大时,查看类之间的关系很难。因此,创建图表视图是一个好的解决方法;更新数据类(data class)到数据库。当你修改数据类时,如添加一个成员,可以直接从图表更新映射表。请参考 http://www.alinq.org/ORDesigner.aspx

posted @ 2012-05-24 16:27  Chaoa  阅读(1095)  评论(0编辑  收藏  举报