以前做过一个NHibernate练习,这里用DLINQ重新来过
The LINQ Project
http://msdn.microsoft.com/netframework/future/linq/
- 生成数据库
use master
go
create database DLINQ
go
use DLINQ
go
CREATE TABLE users (
LogonID nvarchar(20) NOT NULL default ‘0’,
Name nvarchar(40) default NULL,
Password nvarchar(20) default NULL,
EmailAddress nvarchar(40) default NULL,
LastLogon datetime default NULL,
PRIMARY KEY (LogonID)
)
go
- 使用SqlMetal生成Entity Class: User.cs
SqlMetal /server:(local) /database:DLINQ /delayfetch /pluralize /namespace:LINQ.Examples.QuickStart /code:User.cs
//——————————————————————————
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50215.44
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//——————————————————————————
namespace LINQ.Examples.QuickStart {
using System;
using System.Collections.Generic;
using System.Data.DLinq;
public partial class DLINQ : DataContext {
public Table<User> Users;
public DLINQ() {
}
public DLINQ(string connection) :
base(connection) {
}
}
[Table(Name=”users”)]
public partial class User : System.Data.DLinq.IChangeNotifier {
private string _LogonID;
private string _Name;
private string _Password;
private string _EmailAddress;
private System.Nullable<System.DateTime> _LastLogon;
public User() {
}
[Column(Storage="_LogonID", DbType="NVarChar(20) NOT NULL", Id=true)]
public string LogonID {
get {
return this._LogonID;
}
set {
if ((this._LogonID != value)) {
this.OnChanging();
this._LogonID = value;
}
}
}
[Column(Storage="_Name", DbType="NVarChar(40)")]
public string Name {
get {
return this._Name;
}
set {
if ((this._Name != value)) {
this.OnChanging();
this._Name = value;
}
}
}
[Column(Storage="_Password", DbType="NVarChar(20)")]
public string Password {
get {
return this._Password;
}
set {
if ((this._Password != value)) {
this.OnChanging();
this._Password = value;
}
}
}
[Column(Storage="_EmailAddress", DbType="NVarChar(40)")]
public string EmailAddress {
get {
return this._EmailAddress;
}
set {
if ((this._EmailAddress != value)) {
this.OnChanging();
this._EmailAddress = value;
}
}
}
[Column(Storage="_LastLogon", DbType="DateTime")]
public System.Nullable<System.DateTime> LastLogon {
get {
return this._LastLogon;
}
set {
if ((this._LastLogon != value)) {
this.OnChanging();
this._LastLogon = value;
}
}
}
public event System.Data.DLinq.ObjectChangingEventHandler ObjectChanging;
private void OnChanging() {
if ((this.ObjectChanging != null)) {
this.ObjectChanging(this, System.EventArgs.Empty);
}
}
}
}
- 生成测试程序:TestUser.cs
using System;
using System.Collections.Generic;
using System.Data.DLinq;
using System.Query;
using LINQ.Examples.QuickStart;
class TestUser
{
public static void Main()
{
DLINQ db = new DLINQ("server=(local);database=DLINQ;uid=sa;pwd=;");
User newUser = new User();
newUser.LogonID = "joe_cool";
newUser.Name = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
newUser.LastLogon = DateTime.Now;
db.Users.Add(newUser);
db.SubmitChanges();
Console.WriteLine("go check in the database, enter return to continue....");
Console.ReadLine();
var userExpr = from u in db.Users
where u.LogonID == "joe_cool"
select u;
User joeCool = userExpr.First();
joeCool.LastLogon = DateTime.Now;
db.SubmitChanges();
userExpr = from u in db.Users
select u;
foreach(var user in userExpr)
{
Console.WriteLine(user.LogonID + " last logged in at " + user.LastLogon);
}
userExpr = from u in db.Users
where u.LastLogon > new DateTime(2004, 03, 14, 20, 0, 0)
select u;
foreach(var user in userExpr)
{
Console.WriteLine(user.LogonID + " last logged in at " + user.LastLogon);
}
}
}
- compile TestUser.cs:
D:\Program Files\LINQ Preview\Bin>.\csc /r:.\System.Query.dll;.\System.Data.DLinq.dll TestUser.cs User.cs
- run TestUser.exe
D:\Program Files\LINQ Preview\Bin>TestUser
go check in the database, enter return to continue….
joe_cool last logged in at 2005-9-16 10:45:102005-9-16 10:45:10
joe_cool last logged in at 2005-9-16 10:45:102005-9-16 10:45:10
想试的话,也许需要改动连接字符串,可惜上面的程序在8月份的CTP上不工作。出錯信息是
Unhandled Exception: System.TypeLoadException: Could not load type ‘System.INullableValue’ from assembly ‘mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.
at System.Data.DLinq.SqlClient.QueryConverter.IsConstNull(ConstantExpression expr)
at System.Data.DLinq.SqlClient.QueryConverter.VisitConstant(ConstantExpression cons) in c:\PdcBuild\query\DLinq\Engine\Converter.cs:line 500
at System.Data.DLinq.SqlClient.QueryConverter.VisitInner(Expression node) in c:\PdcBuild\query\DLinq\Engine\Converter.cs:line 102……