ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ72²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generated¡¡code¡¡is¡¡a¡¡thin¡¡functional¡¡layer¡¡on¡¡top¡¡of¡¡the¡¡ADO¡¡code¡¡that¡¡maps¡¡directly¡¡to¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tables¡¡that¡¡you¡¡are¡¡manipulating¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡you¡¡are¡¡developing¡¡an¡¡application¡¡that¡¡accesses¡¡a¡¡relational¡¡database£»¡¡you¡¡need¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡consider¡¡the¡¡following¡¡issues£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ADO¡¡drivers¡¡£º¡¡When¡¡you¡¡write¡¡ADO¡¡code£»¡¡you¡¡will¡¡need¡¡an¡¡ADO¡¡driver¡¡for¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡each¡¡database¡£¡¡Thus£»¡¡if¡¡you¡¡wish¡¡to¡¡use¡¡MySQL£»¡¡you¡¡will¡¡need¡¡a¡¡MySQL¡¡driver¡£¡¡However£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡for¡¡the¡¡most¡¡part£»¡¡the¡¡code¡¡that¡¡you¡¡write¡¡in¡¡your¡¡application¡¡will¡¡remain¡¡identical£»¡¡regard
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡less¡¡of¡¡which¡¡type¡¡of¡¡database¡¡you¡¡are¡¡using¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Abstraction¡¡£º¡¡There¡¡will¡¡always¡¡be¡¡differences¡¡in¡¡the¡¡code¡¡used¡¡to¡¡access¡¡different¡¡relational¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡databases¡£¡¡Be¡¡prepared¡¡to¡¡abstract¡¡your¡¡database¡¡code¡¡if¡¡you¡¡access¡¡the¡¡database¡¡directly¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡using¡¡ADO¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡399¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡R¡¡E¡¡L¡¡AT¡¡IO¡¡N¡¡A¡¡L¡¡¡¡¡¡D¡¡AT¡¡AB¡¡A¡¡SE¡¡¡¡¡¡D¡¡A¡¡TA¡¡377¡¡
Designing¡¡a¡¡Database¡¡Using¡¡Visual¡¡Basic¡¡Express¡¡
Visual¡¡Basic¡¡Express¡¡is¡¡very¡¡helpful¡¡when¡¡designing¡¡and¡¡building¡¡database¡¡applications¡£¡¡It¡¡provides¡¡¡¡
interface¡based¡¡tools¡¡for¡¡designing¡¡the¡¡database£»¡¡managing¡¡connections£»¡¡and¡¡managing¡¡data¡¡¡¡
access¡£¡¡
¡¡¡¡¡¡¡¡¡¡With¡¡Visual¡¡Basic¡¡Express£»¡¡you¡¡can¡¡directly¡¡integrate¡¡the¡¡ADO¡¡drivers¡¡for¡¡only¡¡Microsoft¡¡¡¡
SQL¡¡Server¡£¡¡£¨This¡¡does¡¡not¡¡mean¡¡you¡¡cannot¡¡use¡¡a¡¡different¡¡ADO¡¡driver¡¡using¡¡code¡££©¡¡If¡¡you¡¡¡¡
want¡¡to¡¡use¡¡the¡¡GUI¡¡tools¡¡for¡¡a¡¡database¡¡driver¡¡other¡¡than¡¡for¡¡SQL¡¡Server£»¡¡you¡¡will¡¡need¡¡to¡¡upgrade¡¡¡¡
your¡¡Visual¡¡Studio¡¡edition¡£¡¡For¡¡this¡¡chapter¡¯s¡¡examples£»¡¡we¡¯ll¡¡use¡¡the¡¡SQL¡¡Server¡¡Express¡¡Edition¡¡¡¡
driver¡¡£¨http£º//msdn¡£microsoft¡£/vstudio/express/sql/£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡You¡¡can¡¡add¡¡the¡¡GUI¡¡tool¡based¡¡database¡¡support¡¡to¡¡any¡¡Visual¡¡Basic¡¡project¡¡type¡£¡¡For¡¡this¡¡¡¡
chapter£»¡¡we¡¯ll¡¡use¡¡a¡¡console¡¡project¡¡called¡¡DatabaseConsoleEx¡£¡¡
¡¡¡¡¡¡¡¡¡¡After¡¡you¡¡have¡¡created¡¡the¡¡¡¡DatabaseConsoleEx¡¡console¡¡project¡¡in¡¡Visual¡¡Basic¡¡Express¡¡¡¡
£¨using¡¡the¡¡procedure¡¡outlined¡¡in¡¡Chapter¡¡1£©£»¡¡you¡¡can¡¡set¡¡up¡¡the¡¡database£»¡¡and¡¡then¡¡add¡¡tables¡¡¡¡
to¡¡your¡¡new¡¡database¡£¡¡
Configuring¡¡the¡¡Data¡¡Source¡¡
Using¡¡the¡¡Visual¡¡Basic¡¡Express¡¡Data¡¡Source¡¡Configuration¡¡Wizard£»¡¡you¡¡can¡¡add¡¡a¡¡database¡¡as¡¡¡¡
your¡¡data¡¡source£»¡¡set¡¡up¡¡the¡¡database¡¡connection£»¡¡and¡¡select¡¡database¡¡objects¡¡to¡¡include¡¡in¡¡the¡¡¡¡
project¡£¡¡Follow¡¡these¡¡steps¡¡to¡¡use¡¡the¡¡wizard£º¡¡
¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Select¡¡Data¡¡¡¡Add¡¡New¡¡Data¡¡Source¡¡to¡¡start¡¡the¡¡Data¡¡Source¡¡Configuration¡¡Wizard¡£¡¡
¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Choose¡¡Database¡¡as¡¡the¡¡data¡¡source¡¡and¡¡click¡¡Next¡£¡¡
¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡You¡¯re¡¡asked¡¡to¡¡choose¡¡a¡¡data¡¡connection¡£¡¡When¡¡choosing¡¡the¡¡data¡¡connection£»¡¡you¡¡are¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡defining¡¡the¡¡connection¡¡settings¡¡to¡¡your¡¡relational¡¡database¡£¡¡Since¡¡we¡¡are¡¡creating¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡new¡¡database¡¡in¡¡this¡¡example£»¡¡click¡¡the¡¡New¡¡Connection¡¡button¡£¡¡
¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Choose¡¡Microsoft¡¡SQL¡¡Server¡¡Database¡¡File¡¡and¡¡click¡¡Continue¡£¡¡
¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡In¡¡the¡¡Add¡¡Connection¡¡dialog¡¡box£»¡¡fill¡¡in¡¡the¡¡database¡¡file¡¡name¡£¡¡For¡¡this¡¡example£»¡¡enter¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡name¡¡lottery¡£¡¡Visual¡¡Basic¡¡Express¡¡will¡¡automatically¡¡add¡¡an¡¡¡£mdf¡¡extension¡¡to¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡file¡¡name¡¡to¡¡indicate¡¡that¡¡it¡¡is¡¡a¡¡SQL¡¡Server¡¡file¡¡and¡¡save¡¡it¡¡in¡¡your¡¡My¡¡Documents¡¡folder¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡by¡¡default¡£¡¡Click¡¡OK¡¡to¡¡add¡¡the¡¡connection¡¡and¡¡select¡¡Yes¡¡when¡¡asked¡¡to¡¡choose¡¡to¡¡create¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡file¡¡if¡¡it¡¡doesn¡¯t¡¡exist¡£¡¡
¡¡¡¡¡¡¡¡¡¡6¡£¡¡¡¡The¡¡Choose¡¡Your¡¡Data¡¡Connection¡¡screen¡¡reappears¡¡with¡¡your¡¡data¡¡connection¡¡filled¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡£¡¡Click¡¡Next¡£¡¡
¡¡¡¡¡¡¡¡¡¡7¡£¡¡¡¡Since¡¡we¡¯re¡¡using¡¡a¡¡SQL¡¡Server¡¡Express¡¡Edition¡¡driver¡¡for¡¡this¡¡example£»¡¡the¡¡dialog¡¡box¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡shown¡¡in¡¡Figure¡¡14¡2¡¡appears¡£¡¡It¡¡asks¡¡if¡¡the¡¡database¡¡file¡¡can¡¡be¡¡copied¡¡into¡¡the¡¡project¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Click¡¡Yes¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡8¡£¡¡¡¡You¡¡are¡¡asked¡¡if¡¡you¡¡want¡¡the¡¡application¡¡configuration¡¡information¡¡added¡¡to¡¡the¡¡project¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Click¡¡Next¡¡to¡¡add¡¡the¡¡information¡£¡¡
¡¡¡¡¡¡¡¡¡¡9¡£¡¡¡¡The¡¡Choose¡¡Your¡¡Database¡¡Objects¡¡screen¡¡appears¡£¡¡Since¡¡this¡¡is¡¡a¡¡SQL¡¡Server¡¡Express¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Edition¡¡file£»¡¡it¡¡doesn¡¯t¡¡have¡¡any¡¡tables¡£¡¡If¡¡the¡¡database¡¡connection¡¡referenced¡¡a¡¡relational¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡database¡¡that¡¡already¡¡existed£»¡¡database¡¡objects¡¡would¡¡be¡¡available¡£¡¡Click¡¡Finish¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡400¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
378¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡R¡¡E¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡AL¡¡¡¡¡¡DA¡¡TA¡¡B¡¡AS¡¡E¡¡¡¡¡¡D¡¡AT¡¡A¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡14¡2¡£¡¡Adding¡¡the¡¡SQL¡¡Server¡¡Express¡¡Edition¡¡file¡¡to¡¡the¡¡local¡¡project¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡Express¡¡will¡¡rebuild¡¡your¡¡project¡£¡¡When¡¡it¡¡is¡¡finished£»¡¡the¡¡result¡¡will¡¡be¡¡similar¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡the¡¡project¡¡structure¡¡shown¡¡in¡¡Figure¡¡14¡3¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡14¡3¡£¡¡Modifications¡¡made¡¡to¡¡the¡¡Visual¡¡Basic¡¡Express¡¡project¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Visual¡¡Basic¡¡Express¡¡project¡¡contains¡¡a¡¡reference¡¡to¡¡a¡¡file¡¡that¡¡is¡¡provided¡¡by¡¡Microsoft¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡SQL¡¡Server¡¡Express¡¡Edition¡£¡¡The¡¡diagram¡¡shown¡¡earlier¡¡in¡¡Figure¡¡14¡1¡¡indicates¡¡that¡¡a¡¡database¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡server¡¡is¡¡another¡¡process¡¡that¡¡you¡¡access¡¡using¡¡a¡¡client¡¡library¡£¡¡In¡¡about¡¡80£¥¡¡of¡¡the¡¡cases£»¡¡this¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡true£»¡¡but¡¡some¡¡database¡¡servers¡¡are¡¡file¡based¡£¡¡These¡¡types¡¡of¡¡databases¡¡are¡¡used¡¡in¡¡simpler¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡single¡user¡¡database¡¡applications¡£¡¡From¡¡a¡¡programming¡¡perspective£»¡¡nothing¡¡changes£»¡¡and¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡source¡¡code¡¡should¡¡not¡¡even¡¡be¡¡aware¡¡of¡¡whether¡¡the¡¡database¡¡is¡¡a¡¡file¡¡or¡¡server¡¡process¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡the¡¡Tables¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡Visual¡¡Basic¡¡Express£»¡¡you¡¡can¡¡add¡¡tables¡¡to¡¡your¡¡database¡¡using¡¡the¡¡Database¡¡Explorer¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Through¡¡the¡¡Database¡¡Explorer£»¡¡you¡¡can¡¡modify¡¡all¡¡of¡¡the¡¡data¡¡objects¡¡available¡¡within¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡database¡£¡¡Here¡¡are¡¡the¡¡general¡¡steps¡¡for¡¡adding¡¡a¡¡table£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Right¡click¡¡the¡¡lottery¡£mdf¡¡file¡¡in¡¡the¡¡Solution¡¡Explorer¡¡and¡¡select¡¡Open¡¡to¡¡open¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Database¡¡Explorer¡£¡¡The¡¡Database¡¡Explorer¡¡displays¡¡information¡¡about¡¡the¡¡database¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Right¡click¡¡the¡¡Tables¡¡node¡¡and¡¡select¡¡Add¡¡New¡¡Table¡¡to¡¡open¡¡a¡¡window¡¡for¡¡creating¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡new¡¡table£»¡¡as¡¡shown¡¡in¡¡Figure¡¡14¡4¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡Specify¡¡the¡¡column¡¡name¡¡and¡¡type¡¡for¡¡each¡¡column¡¡in¡¡the¡¡table¡£¡¡You¡¡can¡¡also¡¡specify¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡details¡¡about¡¡each¡¡column£»¡¡such¡¡as¡¡its¡¡length¡¡and¡¡whether¡¡it¡¡must¡¡be¡¡unique¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Once¡¡you¡¡have¡¡entered¡¡the¡¡column¡¡information£»¡¡save¡¡the¡¡table¡¡£¨Ctrl£«S£©¡¡and¡¡give¡¡it¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡name¡£¡¡For¡¡this¡¡example£»¡¡we¡¡will¡¡add¡¡three¡¡tables£º¡¡draws£»¡¡persons£»¡¡and¡¡winners¡£¡¡The¡¡following¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sections¡¡describe¡¡the¡¡columns¡¡and¡¡types¡¡for¡¡these¡¡tables¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡401¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡R¡¡E¡¡L¡¡AT¡¡IO¡¡N¡¡A¡¡L¡¡¡¡¡¡D¡¡AT¡¡AB¡¡A¡¡SE¡¡¡¡¡¡D¡¡A¡¡TA¡¡379¡¡
Figure¡¡14¡4¡£¡¡Creating¡¡a¡¡new¡¡table¡¡
¡¡¡¡¡¡¡¡¡¡¡¡Each¡¡column¡¡of¡¡a¡¡table¡¡must¡¡have¡¡a¡¡name¡¡and¡¡type¡£¡¡Just¡¡as¡¡Visual¡¡Basic¡¡has¡¡types£»¡¡so¡¡does¡¡¡¡
a¡¡database¡£¡¡What¡¡is¡¡frustrating¡¡about¡¡database¡¡types¡¡is¡¡that¡¡they¡¡are¡¡similar¡¡but¡¡not¡¡identical¡¡to¡¡¡¡
Visual¡¡Basic¡¡types¡£¡¡To¡¡make¡¡things¡¡even¡¡more¡¡frustrating£»¡¡not¡¡all¡¡database¡¡implementations¡¡¡¡
have¡¡the¡¡exact¡¡same¡¡types¡£¡¡Fortunately£»¡¡if¡¡you¡¡use¡¡the¡¡Visual¡¡Studio¡¡tools£»¡¡the¡¡wizard¡¡will¡¡map¡¡¡¡
a¡¡specific¡¡database¡¡type¡¡to¡¡a¡¡Visual¡¡Basic¡¡type¡£¡¡
¡öTip¡¡¡¡The¡¡Microsoft¡¡Visual¡¡Studio¡¡documentation¡¡has¡¡an¡¡excellent¡¡reference¡¡on¡¡the¡¡various¡¡data¡¡types¡¡¡¡
and¡¡their¡¡accuracy¡£¡¡See¡¡the¡¡¡°Data¡¡Types¡±¡¡section¡¡of¡¡the¡¡Microsoft¡¡SQL¡¡Server¡¡Books¡¡Online¡¡documentation¡¡¡¡
£¨http£º//msdn2¡£microsoft¡£/en¡us/library/ms130214¡£aspx£©¡£¡¡
Draws¡¡Table¡¡
The¡¡draws¡¡table¡¡contains¡¡all¡¡of¡¡the¡¡drawn¡¡lottery¡¡numbers¡£¡¡Table¡¡14¡1¡¡shows¡¡the¡¡column¡¡names¡¡¡¡
and¡¡types¡¡for¡¡this¡¡table¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡402¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
380¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡R¡¡E¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡AL¡¡¡¡¡¡DA¡¡TA¡¡B¡¡AS¡¡E¡¡¡¡¡¡D¡¡AT¡¡A¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Table¡¡14¡1¡£¡¡Draws¡¡Table¡¡Columns¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Name¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Type¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡draw_date¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡datetime¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡first_number¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡second_number¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡third_number¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡fourth_number¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡fifth_number¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡sixth_number¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bonus¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡int¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡draw_date¡¡column¡¡holds¡¡the¡¡date¡¡of¡¡the¡¡draw¡£¡¡The¡¡declared¡¡type¡¡is¡¡datetime£»¡¡which¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡like¡¡the¡¡datetime¡¡type¡¡in¡¡¡£¡¡However£»¡¡you¡¡will¡¡need¡¡to¡¡be¡¡careful¡¡in¡¡mapping¡¡types£»¡¡as¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡explained¡¡in¡¡Chapter¡¡3¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡rest¡¡of¡¡the¡¡columns¡¡represent¡¡a¡¡number¡¡in¡¡the¡¡winning¡¡draw£»¡¡including¡¡the¡¡bonus¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡number¡£¡¡As¡¡in¡¡Visual¡¡Basic£»¡¡SQL¡¡Server¡¡includes¡¡various¡¡numeric¡¡types¡£¡¡The¡¡number¡¡columns¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡the¡¡draws¡¡table¡¡are¡¡declared¡¡as¡¡the¡¡¡¡int¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡one¡¡SQL¡¡Server¡¡numeric¡¡type¡¡that¡¡does¡¡not¡¡exist¡¡in¡¡¡¡is¡¡numeric¡£¡¡This¡¡type¡¡behaves¡¡like¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡decimal¡¡type¡¡in¡¡£»¡¡except¡¡for¡¡the¡¡precision¡£¡¡With¡¡numeric£»¡¡you¡¡can¡¡specify¡¡the¡¡number¡¡of¡¡digits¡¡before¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡after¡¡the¡¡decimal¡¡point¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Persons¡¡Table¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡persons¡¡table¡¡lists¡¡all¡¡of¡¡the¡¡people¡¡who¡¡have¡¡won¡¡a¡¡lottery¡¡drawing¡£¡¡Table¡¡14¡2¡¡shows¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡column¡¡names¡¡and¡¡types¡¡for¡¡the¡¡persons¡¡table¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Table¡¡14¡2¡£¡¡Persons¡¡Table¡¡Columns¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Name¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Type¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡id¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡uniqueidentifier¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡first_name¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡nvarchar£¨100£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡last_name¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡nvarchar£¨100£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡persons¡¡table¡¡is¡¡a¡¡collection¡¡of¡¡people¡¡with¡¡their¡¡first¡¡names¡¡and¡¡last¡¡names¡£¡¡The¡¡challenge¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡a¡¡relational¡¡database¡¡is¡¡uniquely¡¡identifying¡¡a¡¡user¡£¡¡Think¡¡of¡¡it¡¡as¡¡trying¡¡to¡¡define¡¡a¡¡unique¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡hash¡¡code¡£¡¡The¡¡solution¡¡most¡¡databases¡¡use¡¡is¡¡a¡¡number¡£¡¡When¡¡you¡¡have¡¡millions¡¡of¡¡records£»¡¡a¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡403¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡R¡¡E¡¡L¡¡AT¡¡IO¡¡N¡¡A¡¡L¡¡¡¡¡¡D¡¡AT¡¡AB¡¡A¡¡SE¡¡¡¡¡¡D¡¡A¡¡TA¡¡381¡¡
number¡¡might¡¡not¡¡be¡¡adequate¡¡as¡¡a¡¡unique¡¡identifier¡£¡¡In¡¡that¡¡case£»¡¡you¡¡can¡¡use¡¡the¡¡SQL¡¡Server¡¡¡¡
uniqueidentifier¡¡type£»¡¡as¡¡we¡¯re¡¡doing¡¡for¡¡the¡¡id¡¡column¡¡of¡¡the¡¡¡¡persons¡¡table¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡first_name¡¡and¡¡¡¡last_name¡¡columns¡¡both¡¡have¡¡the¡¡type¡¡nvarchar£¨100£©¡£¡¡A¡¡string¡¡in¡¡a¡¡¡¡
database¡¡behaves¡¡like¡¡a¡¡number¡¡type£»¡¡in¡¡that¡¡strings¡¡have¡¡length¡¡limits¡£¡¡In¡¡the¡¡example£»¡¡we¡¡use¡¡¡¡
the¡¡nvarchar¡¡type£»¡¡for¡¡a¡¡variable¡length¡¡string¡¡with¡¡a¡¡maximum¡¡length¡¡of¡¡100¡¡characters¡£¡¡In¡¡¡¡
contrast£»¡¡specifying¡¡char£¨100£©¡¡would¡¡give¡¡you¡¡a¡¡string¡¡100¡¡characters¡¡long£»¡¡regardless¡¡of¡¡how¡¡¡¡
many¡¡bytes¡¡contain¡¡letters¡£¡¡If¡¡the¡¡entry¡¡in¡¡a¡¡char¡¡column¡¡has¡¡fewer¡¡characters¡¡than¡¡specified£»¡¡¡¡
the¡¡remainder¡¡of¡¡the¡¡char¡¡string¡¡is¡¡filled¡¡with¡¡space¡¡characters£»¡¡by¡¡default¡£¡¡
Winners¡¡Table¡¡
The¡¡winners¡¡table¡¡matches¡¡the¡¡winning¡¡people¡¡to¡¡their¡¡lottery¡¡drawing¡£¡¡Table¡¡14¡3¡¡shows¡¡the¡¡¡¡
column¡¡names¡¡and¡¡types¡¡for¡¡the¡¡winners¡¡table¡£¡¡
Table¡¡14¡3¡£¡¡¡¡Winners¡¡Table¡¡Columns¡¡
Name¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Type¡¡
id¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡uniqueidentifier¡¡
draw_date¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡datetime¡¡
¡¡¡¡¡¡¡¡¡¡Both¡¡columns¡¡are¡¡the¡¡types¡¡of¡¡the¡¡specific¡¡columns¡¡in¡¡the¡¡tables¡¡being¡¡referenced¡£¡¡The¡¡¡¡
idea¡¡is¡¡to¡¡use¡¡the¡¡winners¡¡table¡¡in¡¡conjunction¡¡with¡¡the¡¡persons¡¡table¡¡and¡¡the¡¡draws¡¡table¡¡to¡¡¡¡
create¡¡the¡¡list¡¡that¡¡shows¡¡who¡¡won¡¡which¡¡lottery¡¡drawing¡¡and¡¡their¡¡numbers¡£¡¡
¡¡¡¡¡¡¡¡¡¡After¡¡you¡¯ve¡¡created¡¡the¡¡three¡¡tables£»¡¡your¡¡Database¡¡Explorer¡¡will¡¡resemble¡¡Figure¡¡14¡5¡£¡¡
Figure¡¡14¡5¡£¡¡Modified¡¡database¡¡structure¡¡with¡¡the¡¡added¡¡tables¡¡
¡¡¡¡¡¡¡¡¡¡Now¡¡that¡¡we¡¡have¡¡a¡¡database¡¡with¡¡some¡¡tables£»¡¡let¡¯s¡¡see¡¡how¡¡to¡¡access¡¡that¡¡database¡¡¡¡
directly£»¡¡using¡¡ADO¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡404¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
382¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡R¡¡E¡¡L¡¡A¡¡TI¡¡O¡¡N¡¡AL¡¡¡¡¡¡DA¡¡TA¡¡B¡¡AS¡¡E¡¡¡¡¡¡D¡¡AT¡¡A¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Accessing¡¡the¡¡Database¡¡Using¡¡ADO¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Accessing¡¡the¡¡database¡¡directly¡¡using¡¡ADO¡¡involves¡¡using¡¡the¡¡ADO¡¡interfaces¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡first¡¡step¡¡is¡¡to¡¡define¡¡a¡¡connection¡£¡¡Once¡¡the¡¡connection¡¡has¡¡been¡¡established£»¡¡you¡¡can¡¡manip
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ulate¡¡the¡¡tables¡¡in¡¡the¡¡database¡ªto¡¡add£»¡¡remove£»¡¡and¡¡update¡¡records¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡we¡¡will¡¡continue¡¡with¡¡the¡¡sample¡¡lottery¡¡database¡¡created¡¡in¡¡the¡¡previous¡¡section¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡We¡¯ll¡¡write¡¡code¡¡to¡¡add£»¡¡select£»¡¡and¡¡delete¡¡records¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Connecting¡¡to¡¡a¡¡Database¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Define¡¡a¡¡database¡¡connection¡¡with¡¡the¡¡following¡¡code£»¡¡which¡¡illustrates¡¡a¡¡general¡¡approach¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨added¡¡to¡¡the¡¡DatabaseConsoleEx¡¡application£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Imports¡¡System¡£Data¡£SqlClient¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¡¡¡£¡¡¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡connection¡¡As¡¡IDbConnection¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡SqlConnection£¨DatabaseConsoleEx¡£My¡£Settings¡£Default¡£lotteryConnectionString£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡We¡¡need¡¡to¡¡import¡¡the¡¡System¡£Data¡£SqlClient¡¡namespace¡¡because¡¡it¡¡contains¡¡the¡¡classes¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡access¡¡and¡¡use¡¡SQL¡¡Server¡¡£¨that¡¡is£»¡¡the¡¡ADO¡¡driver£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡variable¡¡connection¡¡is¡¡a¡¡SqlConnection¡¡object£»¡¡which¡¡is¡¡specifically¡¡for¡¡accessing¡¡SQL¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Server¡£¡¡Think¡¡of¡¡it¡¡as¡¡picking¡¡up¡¡the¡¡telephone¡¡and¡¡hearing¡¡the¡¡telephone¡¡tone¡£¡¡The¡¡connection¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡requires¡¡a¡¡username£»¡¡password£»¡¡and¡¡the¡¡name¡¡of¡¡the¡¡database¡¡to¡¡which¡¡you¡¡want¡¡to¡¡connect£»¡¡which¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡bundled¡¡into¡¡a¡¡connection¡¡string¡£¡¡That¡¡information¡¡is¡¡stored¡¡in¡¡lotteryConnectionString£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡which¡¡was¡¡defined¡¡when¡¡you¡¡configured¡¡the¡¡data¡¡source¡¡in¡¡Visual¡¡Basic¡¡Express¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Once¡¡you¡¡have¡¡a¡¡connection¡¡instance£»¡¡you¡¡can¡¡create¡¡a¡¡live¡¡connection£»¡¡which¡¡is¡¡akin¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡dialing¡¡a¡¡telephone¡¡number¡¡and¡¡hearing¡¡that¡¡telephone¡¡ring¡£¡¡Here¡¡is¡¡the¡¡code¡¡for¡¡opening¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡connection£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connection¡£Open£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Now¡¡you¡¡can¡¡work¡¡with¡¡the¡¡tables¡¡in¡¡the¡¡database¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Closing¡¡a¡¡Database¡¡Connection¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡After¡¡having¡¡processed¡¡your¡¡SQL¡¡statements£»¡¡you¡¡should¡¡close¡¡the¡¡connection¡¡to¡¡indicate¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡are¡¡finished¡¡using¡¡the¡¡database¡£¡¡Here¡¯s¡¡how£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡connection¡£Close£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡It¡¡is¡¡good¡¡practice¡¡to¡¡open¡¡and¡¡close¡¡database¡¡connections¡¡as¡¡quickly¡¡as¡¡possible¡£¡¡You¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡should¡¡open¡¡a¡¡connection¡¡only¡¡when¡¡you¡¡are¡¡absolutely¡¡ready£»¡¡and¡¡close¡¡it¡¡as¡¡soon¡¡as¡¡you¡¡are¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡finished¡¡with¡¡it¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Adding¡¡Table¡¡Data¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡draws¡¡database¡¡table¡¡you¡¡created¡¡earlier¡¡is¡¡empty¡£¡¡Now¡¡we¡¡will¡¡add¡¡some¡¡content¡£¡¡To¡¡add¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡¡to¡¡a¡¡database¡¡using¡¡SQL£»¡¡use¡¡the¡¡SQL¡¡INSERT¡¡mand£»¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡405¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡C¡¡HA¡¡P¡¡TE¡¡R¡¡¡¡¡¡1¡¡4¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡R¡¡E¡¡L¡¡AT¡¡IO¡¡N¡¡A¡¡L¡¡¡¡¡¡D¡¡AT¡¡AB¡¡A¡¡SE¡¡¡¡¡¡D¡¡A¡¡TA¡¡383¡¡
Imports¡¡System¡£Data¡£SqlClient¡¡
¡£¡¡¡£¡¡¡£¡¡
¡¡¡¡¡¡¡¡Dim¡¡cmd¡¡As¡¡IDbmand¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡New¡¡Sqlmand£¨¡¨INSERT¡¡INTO¡¡draws¡¡£¨draw_date£»¡¡first_number£»¡¡second_number£»¡¡¡¨¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡