½ÓÉÏÒ»½Ú£¬ÎÒÃÇÕâÒ»½ÚÖ÷Òª¼ÆÂÛÈçºÎʹÓÃDataSet£¬ÔÚÊý¾Ý¿âÖÐÔö¼Ó¡¢Ð޸ġ¢É¾³ýÒ»¸öÊý¾Ý¡£ Ê×ÏÈÎÒÃÇÐèÒª´ò¿ªÒ»¸öÁª½á£¬ÎÒÃǵÄÊý¾Ý¿â»¹ÊÇÓÃÉÏÒ»½ÚµÄ°É£º£© string MyConnString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:/test/test.mdb;"; string strComm = "select * from UserList"; ADOConnection MyConnection = new ADOConnection(MyConnString); ADODataSetCommand MyComm = new ADODataSetCommand(strComm,MyConnection); ÕâÀïÎÒÃÇΪÁ˽²½â·½±ã£¬Ö»ÔÚDataSet´æÈëÒ»¸ö±íµÄÄÚÈÝ: DataSet MyDataSet = new DataSet(); MyComm.FillDataSet(MyDataSet,"UserList"); ´ËʱÎÒÃǾͻñµÃÁËÒ»¸öÓµÓÐUserList±íÊý¾ÝµÄDataSet¡£ÔÚ½²½âDataSet֮ǰ£¬ÎÒÃÇ»¹ÐèÒªÁ˽âDataSetµÄ½á¹¹ÏÂÃæÊÇDataSetµÄ½á¹¹Ê÷ DataSet RelationsCollection ExtendedProperties TablesCollection ¡¡ DataTables ¡¡ Rows Columns ÆäËü ÓÉÓÚÎÒÃÇÑо¿µÄÊÇDataTable£¬ÆäËüµÄÎÒÃÇÔÝʱ²»¹ÜËûÃÇ¡£Ò»¸öDataSetÖаüº¬¶à¸öDataTable,Ò»¸öDataTableÓÖ°üº¬Óжà¸öRow£¬Õâ¾ÍÊÇÎÒÃDzÙ×÷DataSetµÄ»ù´¡À²£º£© Ìí¼ÓÊý¾Ý Ìí¼ÓÒ»Êý¾Ý£¬´ÓÉÏÃæµÄÁбíÖÐÎÒÃÇ¿ÉÒÔ¿´³ö£¬Æäʵ¾ÍÊÇÌí¼ÓÒ»ÐÐRow£¬ÕâÀïÎÒÃÇÒ²À´ÑÝʾһÏÂÈçºÎÌí¼ÓÒ»ÐÐÊý¾Ý£¬ÎÒÃdzÌÐòÒ»ÇÐÒÔDataSetΪ¶¥µã£¬ºÇºÇ£¬Èç¹ûTablesCollection,RowsCollectionÏÂÈ¥µÄ»°£¬»¹ÓÐһЩ·³È˵Ädt.AcceptChangesÕâЩ·½·¨µÄµ÷Ó㬺ܷ³ÈË£¬»¹ÊÇÒ»´Î¸ã¶¨°É¡£ DataRow dr=MyDataSet.Tables["UserList"].NewRow(); dr["UserName"] = "ÖÜѶ"; dr["ReMark"] = "100"; dr["Comment"] = "ƯÁÁMM"£» MyDataSet.Tables.Rows.Add(dr); µÚÒ»ÐУ¬ÎÒÃǽ¨Á¢Ò»¸öеÄÊý¾ÝÐУ¬ËüÓÃÀ´´¢´æÎÒÃÇмÓÈëµÄÊý¾Ý¡£È»ºóÎÒÃÇÔÚÕâ¸öÊý¾ÝÐÐÖмÓÈëÎÒÃÇÐèÒªµÄÊý¾Ý¡£dr["UserName"]±íÃ÷ÊǶÔUserName×ֶνøÐÐÌí¼Ó£¬Äã¿ÉÒÔʹÓÃdr[1]À´Ìí¼ÓÐÅÏ¢£¬µ«ÊÇÕâÐèÒªÎÒÃÇÊÂÏÈÖªµÀ×Ö¶ÎÔÚÊý¾Ý±íµÄλÖ㬶øÇÒÔÚ²»ÖªµÀÊý¾Ý±í½á¹¹µÄÇé¿öºÜÄÑÖªµÀÎÒÃÇÌí¼ÓµÄÊý¾ÝµÄ¶ÔÓ¦Çé¿ö£¬ËùÒÔ»¹ÊÇÓÃ×Ö¶ÎÃûΪºÃ¡£ ×îºóÎÒÃÇʹÓÃRowsCollectionµÄAdd·½·¨£¬½«ÎÒÃÇн¨ÕâÒ»ÐмÓÈëµ½Êý¾Ý±íÖС£ ÐÞ¸ÄÊý¾Ý ÖªµÀÈçºÎÌí¼ÓÊý¾Ýºó£¬ÐÞ¸ÄÊý¾ÝÒ²ÊǺܼòµ¥µÄÊÂÇéÁË. MyDataSet.Tables["UserList"].Rows[0]["UserName"]="·Éµ¶´ó¸ç"; ÕâÑùÎÒÃǾÍÐÞ¸ÄÁ˵ÚÒ»ÐÐÊý¾ÝÖеÄUserName×ֶΡ£ ɾ³ýÊý¾Ý ɾ³ýÊý¾Ý£¬Ö÷ÒªÊÇʹÓÃRowsCollectionÌṩµÄDelete·½·¨£¬¿´ÏÂÃæµÄ³ÌÐòÒ²ÊǺܼòµ¥µÄÊÂÇéÀ²£º£© MyDataSet.Tables["UserList"],Rows[0].Delete(); ÕâÒ»ÐÐÊý¾Ý¾ÍÒѾ±»É¾³ýÁË¡£ »Ö¸´Êý¾Ý ÓÐʱºòÎÒÃÇÌí¼Ó/ÐÞ¸ÄÊý¾Ý»á³öÏÖ´íÎó£¬Õâʱºò£¬¾ÍÐèÒª»Ö¸´ÔÀ´µÄÊý¾Ý¡£ÏÂÃæµÄ³ÌÐò£¬ÏÔʾÈçºÎÅжÏÊÇ·ñÓдíÎó·¢Éú£º if(MyDataSet.HasErrors) { MyDataSet.RejectChanges(); } ¡¡ Ê×ÏÈÎÒÃǼì²éDataSetÖÐÊÇ·ñÓдíÎó·¢Éú£¬Èç¹ûÓоÍʹÓÃRejectChanges()·½·¨£¬»Ö¸´DataSetÖеÄÊý¾Ý¡£×¢ÒâÕâÀï»Ö¸´ÊÇÔÚDataSetÖÐËùÓбíÒÔ¼°±íÖÐDataRowÖеÄÊý¾Ý£¬Ò²¾ÍÊÇÔڴ˽»´Î²Ù×÷µÄÊý¾ÝÈ«²¿»Ö¸´¡£Èç¹ûÎÒÃÇÖ»ÐèÒª»Ö¸´²¿·ÖÄÚÈÝ£¬ÎÒÃÇ¿ÉÒÔʹÓÃDataTable»òDataRowµÄRejectChanges()£¬ÕâÀï¾Í²»Ïêϸ½²½âÁË£¬Ê¹Ó÷½·¨ºÍDataSetÒ»Ñù£¬Ö»ÊDzÙ×÷µÄ¶ÔÏñ²»Í¬¶øÒÑ¡£ ̽²âDataSetÊÇ·ñÓиĶ¯ ÎÒÃÇÔÚ½«DataSetËͽ»¸øÊý¾Ý¿âÈ¥±£´æÈ¥£¬ÎÒÃÇÐèÒª¿´¿´Õâ¸öDataSetÊÇ·ñÒѾ±»¸Ä¶¯ÁË¡£Èç¹ûûÓиĶ¯£¬ÎÒÃÇÒ²¾ÍûÓбØҪȥÐÞ¸ÄÊý¾Ý¿âÁË¡£ if(MyDataSet.HasChanges) { //±£´æ }else{ //²»½øÐÐÈκβÙ×÷ } ¸üÐÂÊý¾Ý¿â ÎÒÃÇÉÏÃæµÄ²Ù×÷£¬¶¼Ö»ÊÇÕë¶ÔDataSetµÄ£¬Ã»ÓвÙ×÷Êý¾Ý¿â£¬µ«ÊÇÎÒÃǵÄÄ¿µÄ»¹ÊÇÒª½«Êý¾Ý±£´æµ½Êý¾ÝÖÐÈ¥£¬ËùÒÔÎÒÃÇÕâÀï¾ÍÐèÒªµ÷ÓÃDataSetCommandµÄUpdate·½·¨¡£ÏÂÃæµÄ³ÌÐòÏÔʾÈçºÎ½«DataSetµÄÊý¾Ý½»¸øÊý¾Ý¿â¡£ MyComm.Update(MyDataSet); ºÜ¼òµ¥µÄÒ»¾ä£¬ºÇºÇ¡£ÕâÀïҪעÒ⣬Èç¹ûÒ»¸öDataSetÖаüº¬Óжà¸ö±í£¬¶øÎÒÃÇÖ»¸üÐÂÒ»¸ö£¬ÄÇÎÒÃǾͱØÐëдÃ÷¸üеÄÊý¾Ý±íÃû£º MyComm.Update(MyDataSet,"UserList"); µ±Update·½·¨±»µ÷Óúó£¬DataSetCommand»á½«Êý¾Ý¿âÖеÄÊý¾ÝÓëDataSetÖеÄÊý¾ÝÏà±È½Ï£¬¶Ô²»ÏàͬµÄµØ·½½øÐиüС£ ¶ÔÓÚDataSetµÄ²Ù×÷£¬ÎÒÃÇÕâÀïÖ»½²Õâô¶à£¬ÆäʵDataSetµÄ·½·¨ºÍÊôÐÔÓкܶ࣬¹¦ÄÜÒ²ºÜÈ«£¬ÎÒÏëÏÖÔÚÕâÀïµÄËù½²µÄ¹¦ÄÜ£¬¶ÔÒ»°ãµÄ²Ù×÷ÒѾ×ã¹»ÁË¡£ ¡¡ |