LINQ to SQL¸üÐÂÊý¾Ý¿â²Ù×÷
ʹÓÃLINQ to SQL½¨Ä£NorthwindÊý¾Ý¿â
ÔÚÕâ֮ǰһÆðѧ¹ýLINQ to SQLÉè¼ÆÆ÷µÄʹÓã¬ÏÂÃæ¾ÍʹÓÃÈçϵÄÊý¾ÝÄ£ÐÍ£º
µ±Ê¹ÓÃLINQ to
SQLÉè¼ÆÆ÷Éè¼ÆÒÔÉ϶¨ÒåµÄÎå¸öÀࣨProduct£¬Category£¬Customer£¬OrderºÍOrderDetail£©µÄʱºò£¬Ã¿¸öÀàÖеÄÊôÐÔ
¶¼Ó³ÉäÁËÏàÓ¦Êý¾Ý¿âÖбíµÄÁУ¬Ã¿¸öÀàµÄʵÀýÔò´ú±íÁËÊý¾Ý¿â±íÖеÄÒ»Ìõ¼Ç¼¡£ÁíÍ⣬µ±¶¨ÒåÊý¾ÝÄ£ÐÍʱ£¬LINQ to
SQLÉè¼ÆÆ÷ͬÑù»á´´½¨Ò»¸ö×Ô¶¨ÒåDataContextÀ࣬À´×÷ΪÊý¾Ý¿â²éѯºÍÓ¦ÓøüÐÂ/±ä»¯µÄÖ÷ÒªÇþµÀ¡£ÒÔÉÏÊý¾ÝÄ£ÐÍÖж¨ÒåµÄDataContext
ÀàÃüÃûΪ“NorthwindDataContext”¡£¸ÃÀàÖаüº¬ÁË´ú±íÿ¸ö½¨Ä£Êý¾Ý¿â±íµÄÊôÐÔ¡£
ʹÓÃLINQÓï·¨±í´ïʽ¿ÉÒÔÊ®·Ö¼òµ¥µÄʹÓÃNorthwindDataContextÀàÀ´²éѯºÍ¼ìË÷Êý¾Ý¿âÖеÄÊý¾Ý¡£LINQ to
SQL»áÔÚÔËÐÐʱ×Ô¶¯µÄת»»LINQ±í´ïʽµ½Êʵ±µÄSQL´úÂëÀ´Ö´ÐС£ÀýÈ磬±àдÒÔÏÂLINQ±í´ïʽÀ´¸ù¾ÝProduct
Name¼ìË÷µ¥¸öProduct¶ÔÏó£º
»¹¿ÉÒÔʹÓÃLINQ±í´ïʽÀ´¼ìË÷ËùÓв»´æÔÚÓÚOrder DetailsÖеģ¬²¢ÇÒUnitPrice´óÓÚ100µÄËùÒÔProduct£º
±ä»¯¸ú×ÙºÍDataContext.SubmitChanges£¨£©
µ±Ö´ÐвéѯºÍ¼ìË÷ÏñProductʵÀýÕâÑùµÄ¶ÔÏóʱ£¬LINQ to SQL»á×Ô¶¯±£³Ö¶ÔÕâЩ¶ÔÏóÈκα仯»ò¸üеĸú×Ù¡£ÎÒÃÇ¿ÉÒÔ½øÐÐÈÎÒâ´ÎÊýµÄ²éѯ£¬ÒÔ¼°Ê¹ÓÃLINQ to SQLµÄDataContextÀà×÷³ö¸üУ¬¶øÕâЩ±ä»¯¶¼»á±»È«²¿¸ú×Ù¡£
×¢Ò⣺LINQ to SQLµÄ±ä»¯¸ú×Ù·¢ÉúÓÚµ÷ÓÃÕ߶˗—¶ø²»ÊÇÔÚÊý¾Ý¿âÖС£Õâ¾ÍÒâζ×ÅʹÓøú×Ù²»»áÏúºÄÈκÎÊý¾Ý¿â×ÊÔ´£¬Ò²²»ÐèÒªÔÚÊý¾Ý¿âÖиıä/°²×°ÈκÎ×é¼þÄ£¿é¡£
µ±¶Ô´ÓLINQ to
SQLÖмìË÷µÄ¶ÔÏó×÷³ö¸ü¸ÄÖ®ºó£¬ÎÒÃÇ¿ÉÒÔÑ¡Ôñµ÷ÓÃDataContextÉϵÄSubmitChange£¨£©·½·¨À´Ó¦Óñ仯·µ»Øµ½Êý¾Ý¿â¡£Õ⽫»áµ¼ÖÂ
LINQ to SQL¶¯Ì¬¼ÆËã²¢Ö´ÐÐÊʵ±µÄSQL´úÂëÀ´¸üÐÂÊý¾Ý¿â¡£ÀýÈ磬±àдÒÔÏ´úÂë¸üÐÂÊý¾Ý¿âÖÐProduct
NameΪ“Chai”µÄProductÉϵÄUnitPriceºÍUnitsInStock£º
µ±ÔÚÒÔÉÏ´úÂëÖе÷ÓÃnorthwind.SubmitChanges£¨£©·½·¨Ê±£¬LINQ to SQL»á¶¯Ì¬¹¹½¨²¢Ö´ÐÐÒ»¸ö¸üÐÂÕâÁ½¸öProductÊôÐÔÖµµÄSQL“UPDATE”´úÂëÄ£¿é¡£
ÔÚÏÂÃæ´úÂëÖÐÎÒÃÇÀ´±éÀú²»Á÷Ðеģ¬°º¹óµÄProduct£¬²¢°ÑËüÃǵÄReorderLevelÊôÐÔÉèΪ0£º
µ±ÔÚÒÔÉÏ´úÂëÖе÷ÓÃnorthwind.SubmitChanges£¨£©·½·¨Ê±£¬LINQ to SQL»á¼ÆËã²¢Ö´ÐÐÒ»×éÊʵ±µÄUPDATE´úÂëÄ£¿éÀ´ÐÞ¸ÄRecorderLevelÊôÐÔÒѱ仯µÄProduct¡£
×¢Ò⣬Èç¹ûÒ»¸öProductµÄÊôÐÔûÓÐͨ¹ýÊôÐÔÖ¸¶¨¶ø·¢Éú±ä»¯£¬Ôò¸Ã¶ÔÏ󲻻ᱻÈÏΪÊÇ·¢Éú±ä»¯µÄ£¬²¢ÇÒLINQ to
SQLÒ²²»
Ïà¹ØÎĵµ£º
ÎÒÔÚѧϰ¹ØÓÚjava±à³Ì£¬ÏÖÔÚÉæ¼°µ½µÄÊÇsqlµÄÎÊÌ⣬±ÈÈçÈí¼þÑ¡Ôñ¡¢»·¾³µÄÁ¬½ÓÉèÖõÈÎÊÌ⣬¶ÔÓÚÕâЩÎÊÌâÎÒÕæÊÇÊøÊÖÎ޲߰¡£¡ÇëÇóJava±à³Ì¸ßÊִͽ̣º
1¡¢ÎÒÓõÄJavaÊÇj2sdk-1_4_2_13-nb-5_0£¬ÎÒÓÃÁËsql server2000£¬ÎÒÓÃjdbc3.0ÐÐÂð£¿ËùÓÐÁ¬½ÓÉèÖøÃÔõôÑùÉèÖã¿
&nb ......
ÎÒÃÇÒª×öµ½²»µ«»áдSQL,»¹Òª×öµ½Ð´³öÐÔÄÜÓÅÁ¼µÄSQL,ÒÔÏÂΪ±ÊÕßѧϰ¡¢ÕªÂ¼¡¢²¢»ã×ܲ¿·Ö×ÊÁÏÓë´ó¼Ò·ÖÏí£¡
£¨1£© Ñ¡Ôñ×îÓÐЧÂʵıíÃû˳Ðò(Ö»ÔÚ»ùÓÚ¹æÔòµÄÓÅ»¯Æ÷ÖÐÓÐЧ)£º
ORACLE µÄ½âÎöÆ÷°´ÕÕ´ÓÓÒµ½×óµÄ˳Ðò´¦Àífrom×Ó¾äÖеıíÃû£¬from×Ó¾äÖÐдÔÚ×îºóµÄ±í(»ù´¡±í driving table)½«±»×îÏÈ´¦À ......
Insert Into Êý¾Ý±íÃû³Æ(×Ö¶ÎÃû³Æ1,×Ö¶ÎÃû³Æ2,...) values(×Ö¶ÎÖµ1,×Ö¶ÎÖµ2,...)
insert into user(username,password,age) values('ÀîÀÏËÄ','6666',45)
Update Êý¾Ý±íÃû³Æ Set ×Ö¶ÎÃû³Æ=×Ö¶ÎÖµ,×Ö¶ÎÃû³Æ=×Ö¶ÎÖµ,...[Where Ìõ¼þ]
Delete from Êý¾Ý±í
ÏÂÁвéѯ·µ»ØÔÚLONDON£¨Â׶أ©»òSEATTLE£¨Î÷ÑÅͼ£©µÄËùÓйÍÔ±£º
S ......
1.ÔÚmssql´´½¨´æ´¢¹ý³Ì,ÔÚdb->´æ´¢¹ý³Ì->н¨´æ´¢¹ý³Ì(ÓÒ¼ü)
CREATE PROCEDURE user_logon_check @username varchar(45),@userpsw varchar(45)
AS
select * from users where UserName = @username and PasswordCode = @userpsw
return 10
GO
2.ÔÚmysql´´½¨´æ´¢¹ý³Ì,ʹÓÃMysql Query Browser,ÔÚdb->±í ......