Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Linq to SQL DataContext Lifetime Management


Linq to SQL uses a DataContext to manage it's access to the database as well as tracking changes made to entities retrieved from the database. Linq to SQL has a persistent approach to managing its 'connection' to the database via this data context and it basically assumes that you use a single DataContext to make all of your data related access. This doesn't mean that it makes persistent connections to the database, but means that the DataContext instance maintains state about active result sets, which is especially important if change tracking is on which is the default.
This is somewhat contrary to other ORM tools which tend to have a static manager to which you pass entities or queries that are then returned. In that respect most other ORMs are stateless in their data connectivity and management object where LINQ to SQL clearly takes a connected approach where everything revolves around this single DataContext instance.  DataContext holds all the change management information and it makes it very difficult to transfer that context information to another DataContext. In short it's a very different approach and requires some thinking about how you create and manage the DataContext object.
BTW, the ADO.NET Entity framework too uses a similar connected approach with its ObjectContext object which also manages state persistently and requires that you keep the object around if you want to do things like track changes.
This raises some interesting questions on how to manage the lifetime of the DataContext object. There are a lot of options of how you can deal hanging on (or not) to the DataContext. Here are a few different approaches:
Create a new Context for each atomic operation (Application level management)
Create a global DataContext and handle all operations against this single DataContext object
Create a thread specific DataContext
Create a per business object DataContext
What doesn't work
The first thing to understand is if you are coming from an


Ïà¹ØÎĵµ£º

º½¿Õ¹«Ë¾¹ÜÀíϵͳ(VC++ ÓëSQL 2005)

ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
      ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃæ£¬ºóÃæ ......

Êý¾Ý¿â»ù±¾ SQLÓï¾ä´óÈ«

ѧ»áÊý¾Ý¿âÊǺÜʵÓÃD~~¼Ç¼һЩ³£ÓõÄsqlÓï¾ä...ÓÐÈëÃÅÓÐÌá¸ßÓмû¶¼Ã»¼û¹ýµÄ...ºÃÈ«...ÊÕ²ØÏÂ...
Æäʵһ°ãÓõľÍÊDzéѯ,²åÈë,ɾ³ýµÈÓï¾ä¶øÒÑ....µ«Ñ§Ñ§´æ´¢¹ý³ÌÊǺÃÊÂ...ÒÔºóÊý¾Ý·½ÃæµÄ¶«Î÷¾Í²»ÓÃÔÚ³ÌÐòÀï¸ãà¶..¶øÇÒ³ÌÐòÓëÊý¾Ý¿âÖ»ÒªÒ»¸öÀ´»ØÍ¨Ñ¶¾Í¿ÉÒԸ㶨ËùÓÐÊý¾ÝµÄ²Ù×÷....
Ò»¡¢»ù´¡
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
Create DAT ......

sql group by Óë havingµÄÓ÷¨

1. GROUP BY ÊÇ·Ö×é²éѯ, Ò»°ã GROUP BY ÊǺ;ۺϺ¯ÊýÅäºÏʹÓÃ
group by ÓÐÒ»¸öÔ­Ôò,¾ÍÊÇ select ºóÃæµÄËùÓÐÁÐÖÐ,ûÓÐʹÓþۺϺ¯ÊýµÄÁÐ,±ØÐë³öÏÖÔÚ group by ºóÃæ£¨ÖØÒª£©
ÀýÈç,ÓÐÈçÏÂÊý¾Ý¿â±í£º
A    B
1    abc
1    bcd
1    asdfg
 Èç¹ûÓÐÈ ......

SQLʱ¼äº¯ÊýÏê½â

1. µ±Ç°ÏµÍ³ÈÕÆÚ¡¢Ê±¼ä
select getdate() 
2. dateadd ÔÚÏòÖ¸¶¨ÈÕÆÚ¼ÓÉÏÒ»¶Îʱ¼äµÄ»ù´¡ÉÏ£¬·µ»ØÐµĠdatetime Öµ
ÀýÈ磺ÏòÈÕÆÚ¼ÓÉÏ2Ìì
select dateadd(day,2,'2004-10-15') --·µ»Ø£º2004-10-17 00:00:00.000
3. datediff ·µ»Ø¿çÁ½¸öÖ¸¶¨ÈÕÆÚµÄÈÕÆÚºÍʱ¼ä ......

SQL 2000 Insert·µ»Ø×Ô¶¯±àºÅidÈýÖÖ·½·¨±È½Ï


SQL Server 2000ÖУ¬insertÊý¾ÝµÄʱºò·µ»Ø×Ô¶¯±àºÅµÄid£¬ÓÐÈýÖÖ·½·¨ÊµÏÖSCOPE_IDENTITY¡¢IDENT_CURRENT ºÍ @@IDENTITY£¬ËüÃǶ¼·µ»Ø²åÈëµ½ IDENTITY ÁÐÖеÄÖµ¡£
IDENT_CURRENT £º·µ»ØÎªÈκλỰºÍÈκÎ×÷ÓÃÓòÖеÄÌØ¶¨±í×îºóÉú³ÉµÄ±êʶֵ¡£IDENT_CURRENT ²»ÊÜ×÷ÓÃÓòºÍ»á»°µÄÏÞÖÆ£¬¶øÊÜÏÞÓÚÖ¸¶¨µÄ±í¡£IDENT_CURRENT ·µ»ØÎªÈÎ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ