±¾ÎĽéÉÜÁËÔÚ C++Builder 5»·¾³ÏÂÐÂÔöµÄADO¿Ø¼þµÄʹÓ÷½·¨£¬²¢¸ø³öÁËÒ»¸ö¼òµ¥µÄʵÀý¡£
Ò»¡¢ADO¸ÅÊö
ADO£¨Active Data Object£© ÊÇ΢Èí¹«Ë¾»ùÓÚOLE DBµÄÊý¾Ý¿âÄ£ÐÍ¡£ËüʵÏÖÁËһϵÁÐCOM½Ó¿Ú£¬Í¨¹ýÊý¾ÝÌṩÕߣ¨Data Provider£©ºÍÊý¾ÝʹÓÃÕߣ¨ Data Consumer£©À´ÊµÏÖ¹ãÒåµÄÊý¾Ý´æÈ¡¡£ADOÄ£ÐÍÒ»·½Ãæ¼ò»¯ÁËÊý¾Ý´æÈ¡£¬ÀýÈçʹÓÃJet OLE DB Provider¿ÉÒÔʵÏÖAccessÊý¾Ý¿âµÄÎÞDSNÁ¬½Ó£»ÁíÒ»·½Ã棬ÔÚASPµÈInternetÓ¦ÓÃÖÐÒ²µÃµ½Á˹㷺µÄʹÓá£
ÔÚC++Builder 5/Delphi 5ÖÐÖî¶àÐÂÔöÌØÐÔÖУ¬ADOÊý¾Ý¿Ø¼þµÄÒýÈëÊDZȽÏÒýÈËעĿµÄ£¬ËüʹADO±à³ÌºÍÔÚVisual Basic 6ÖÐÒ»ÑùÈÝÒס£ÓÉÓÚVCLÀà¿â¶ÔADOµÄ·â×°¸ü¼ÓºÏÀí£¬¾ÉµÄÊý¾Ý¿Ø¼þ¿ÉÒÔºÜÈÝÒ×µØÉý¼¶µ½ADOÖ§³Ö£¬¶ø²»ÏóÔÚVBÖÐijЩDAO»òRDO¿Ø¼þÎÞ·¨¼æÈÝ ADO¡£
¶þ¡¢Ê¹ÓÃADO¿Ø¼þ
ÔÚC++Builder 5ÖУ¬ADO¿Ø¼þʵÖÊÉÏÊÇÒ»ÖÖÊý¾Ý·ÃÎÊ×é¼þ¡£Èç¹û¶ÁÕßÓйýC++BuilderÏÈÇ°°æ±¾µÄÊý¾Ý¿â¿ª·¢¾Ñ飬¾Í»á·¢ÏÖÓëÊý¾Ý·ÃÎÊ×é¼þÏàÁ¬µÄÊý¾ÝÔ´×é¼þ£¨TdataSource£© ºÍÊý¾Ý¸ÐÖª×é¼þ£¨ÈçTDBGrid£©µÄʹÓ÷½·¨ºÍÒÔÇ°Ò»Ñù£¬Ö»Ðè°Ñ TdataSource Á¬µ½ADO¿Ø¼þÉϼ´¿É¡£
Èç¹ûҪʵÏÖ¼òµ¥µÄ±í²Ù×÷»ò²éѯ£¬¿ÉÒÔʹÓÃTADOTable»òTADOQuery¡£¸üÒ»°ãµØ£¬¿ÉÒÔʹÓÃTADODataSet¿Ø¼þ£¬Æä»ù±¾ÊôÐÔµÄÉèÖ÷½·¨ÈçÏ£º
£¨1£©ConnectionStringÊôÐÔ£ºµã»÷ÊôÐÔÀ¸ÖеÄÊ¡ÂԺţ¬³öÏÖÅäÖô°¿Ú£¬Ñ¡"Use Connection String"£¬°´Build°´Å¥¡£ÔÚ½ÓÏÂÀ´µÄ´°¿ÚÖÐÑ¡ÔñÊý¾ÝÌṩÕߣ¬ÒÔ΢ÈíµÄNorthwindʾÀýAccessÊý¾Ý¿âΪÀý£¬Ñ¡Microsoft Jet 4.0 OLE DB Provider, °´Next°´Å¥£¬Ñ¡ÔñÊý¾Ý¿âµÄ·¾¶ºÍÎļþÃû¡£µã»÷Test Connection°´Å¥¿ÉÒÔ²âÊÔÊý¾Ý¿âÁ¬½Ó¡£µã»÷"È·¶¨"¡£
£¨2£©CommandTextÊôÐÔ£º¿ÉÒÔʹÓÃCommandText EditorÉú³ÉSQLÓï¾ä»òShapeÓï¾ä¡£
×¢Ò⣬TADODataSet²»Ö§³Ö²»·µ»Ø½á¹û¼¯µÄDMLÓï¾ä£¬ÈçDELETE£¬INSERT£¬UPDATE¡£Èç¹ûҪʹÓÃÕâЩÓï¾ä£¬Ñ¡ÔñTADOCommand»òTADOQuery¡£
ÕâЩADO¿Ø¼þ×ÔÉíÒÑÄÜʵÏÖÊý¾Ý¿âÁ¬½Ó£¬Ò²¿ÉÒÔÉèÖÃÆäConnectionÊôÐÔΪһ¸öTADOConnection¿Ø¼þÃû£¬¶øÔÚ TADOConnection¿Ø¼þÖÐÉèÖà ConnectionStringÊôÐÔ¡£ËùÓÐÊôÐÔÒ²¿ÉÒÔÓôúÂëÔÚÔËÐÐÆÚ¼äÉèÖá£
Èý¡¢Ò»¸öMaster/DetailʵÀý
ÏÂÃæÒÔÒ»¸ö¼òµ¥µÄʵÀý˵Ã÷ADO¿Ø¼þµÄÓ¦Óá£ÕâÀïÎÒÃÇÓÃShapeÓï¾äʵÏÖMaster/Detail¹Øϵ¡£¹ØÓÚShapeÓï¾äµÄÏêϸ½âÊÍ£¬Çë²Î¿¼MSDNÎĵµ¡£
1. н¨Ò»¸öÏîÄ¿£¬²¢ÔÚ´°ÌåÖзÅÖÃÏÂÁпؼþ£¨´øÊ¡ÂԺŵÄÊôÐÔÖµÏȲ»ÉèÖã©£º
Ãû³Æ ¿Ø¼þÀàÐÍ ÊôÐÔ ÊôÐÔÖµ
ADOConnection1 TADOConnection ConnectionString ¡
ADODataSet1 TADODataSet Connection ADOConnection1 CommandText¡
DataSource1 TDataSource DataSet ADODataSet1
DBGrid1 TDBGrid DataSource DataSource1
ADODataSet2 TADODataSet DataSetField ¡
DataSource2 TDataSource DataSet ADODataSet2
DBGrid2 TDBGrid DataSource DataSource2
2. ÉèÖà ADOConnection1µÄConnectionStringÖµ
Ñ¡ÔñData ProviderΪMSDataShape£¬²¢ÉèÖÃNorthwindÊý¾Ý¿âµÄ·¾¶ºÍÃû³Æ¡£
3. ÉèÖà ADODataSet1µÄCommandTextÖµÈçÏ£º
SHAPE {select * from Orders}
APPEND ({select * from [Order Details]} AS Details
RELATE OrderID TO OrderID)
ÕâÀﶨÒåÁËÖ÷±íOrdersºÍ×Ó±íOrder Detailsͨ¹ýOrderIDµÄÁ¬½Ó¡£
4. ÔÚADODataSet1¿Ø¼þÉÏÓÒ»÷£¬Ñ¡Fields Editor¡->ÓÒ»÷->Ñ¡Add fields¡ »òAdd all fields£¬Îñ±ØÑ¡ÖÐ"Details"ÏÕâÊÇÎÒÃÇÔÚShapeÓï¾äÖж¨ÒåµÄÓÃÓÚ×Ó±íÁ¬½ÓÖ÷±íµÄÊý¾Ý¼¯¡£
5. ÉèÖÃ
ADODataSet2µÄDataSetFieldÖµ£¬Õâʱ¿É¹©Ñ¡ÔñµÄֵΪADODataSet1Details£¬Ñ¡ÖÐËü£¬±íÃ÷ÓëADODataSet1µÄDetailsÁÐÏàÁ¬¡£
6. ³öÏÖÔÚ DBGrid1ÖÐµÄ Details ÁУ¬Æäÿ¸ö¼Ç¼¶¼ÊÇDataSetÀàÐ͵ġ£¿ÉÒÔÔÚDBGrid1ÖÐÒþ²Ø DetailsÁеÄÏÔʾ£ºÑ¡Ôñ DBGrid1µÄColumnsÊôÐÔ£¬ÔÚµ¯³ö´°¿ÚµÄ¹¤¾ßÌõÉϵã»÷ Add all Fields£¬Ñ¡ÖÐ Details£¬µã»÷Delete Selected.
7. ÔËÐиóÌÐò£¬µ±Êý¾ÝÖ¸ÕëÔÚÖ÷±íOrdersÖÐÒƶ¯Ê±£¬×Ó±íOrder Details×Ô¶¯¸üÐÂÒÔÏÔʾ²»Í¬¶©µ¥µÄÃ÷ϸÌõÄ¿¡£
´ÓÒÔÉÏʵÀý¿´³ö£¬ C++Builder 5ÍêÈ«¾ß±¸¿ª·¢¸´ÔÓµÄADOÓ¦ÓóÌÐòµÄÄÜÁ¦£¬ÆäÇ¿´óµÄVCL¿Ø¼þʹ¿ª·¢ÕßÄܼ¯ÖÐ×¢ÒâÁ¦ÓÚÊý¾Ý¿âµÄÂß¼Ó빦ÄÜÉè¼Æ£¬¶ø²»±Ø¹ØÐÄ·±¸´µÄ³£¹æ´úÂëºÍ²Ù×÷¡£