简体   繁体   中英

Need help with an update SP and renaming files in the directory

There's a table Commodity in my Database and there's a separate table CommodityImages . There's field CommodityCaption in the Commodity table. I am saving images in the CommodityImages table using the value in this field as follows:

Commodity Table:-

CommodityID | CommodityName | CommodityCaption | IsActive
__________________________________________________________
1           | NameA         |NameA-CaptionA     |True
2           | NameB         |NameB-CaptionB     |True

CommodityImages Table looks like :

CommodityImageID  | CommodityID  | CommodityImageName |

1                 | 1            |NameA-CaptionA_100X200.bmp  
2                 | 1            |NameA-CaptionA_300X500.bmp   
3                 | 2            |NameB-CaptionB_100X200.bmp  
4                 | 2            |NameB-CaptionB_300X500.bmp   

Now when the Caption field is updated in the Commodity table , say, caption for ID=1 is changed from NameA-CaptionA to NameAAA-CaptionAXYZ , I want the CommodityIamges Table to be updated as well.

Like I want the Image names against ID=1 to be updated too. Now when the caption has been changed to NameAAA-CaptionAXYZ , the image names in the second table should become:

NameAAA-CaptionAXYZ_100X200.bmp and NameAAA-CaptionAXYZ_300X500.bmp

How do I do this in my Update SP which is being used to update Commodity Table?

Also , I need to rename the image files in my directory to the latest Commodity Caption names. I know about File.Move(oldFileName, newFileName); but how will I search for these image files in my directory and rename them? All I have ever done is save files.

How to search and then rename?

Update SP :-

-- Description: INSERT PROCEDURE TO INSERT & UPDATE IN Commodity TABLE          
-- =============================================          
ALTER PROC [SAN].[InsertUpdateCommodity]          
(           
@CommodityID BIGINT,  
@CommodityName nvarchar(100),  
@CommodityCaption nvarchar(100),
@Active BIT
)          
AS          
BEGIN   

 IF(@CommodityID =-1)   
  BEGIN          
   INSERT INTO  SAN.Commodity          
   (          
   CommodityName,   
   CommodityCaption,  
   Active
   )          
   VALUES          
   (          
   @CommodityName,   
   @CommodityCaption,  
   @Active
   )          
   SELECT SCOPE_IDENTITY()          
  END   

 ELSE          
  BEGIN          
   UPDATE SAN.Commodity  SET           
   CommodityName=@CommodityName,   
   CommodityCaption=@CommodityCaption,  
   Active=@Active 
   WHERE  CommodityID=@CommodityID    

  SELECT @CommodityID           
 END 

END   

Here is a simplistic pseudocode:

void UpdateCaption(int Id, string oldName, string newName) {
    db.StartTran();
    db.Exec(string.format("update Commodity set CommodityCaption = '{0}' 
            where commodityId = {1}", oldName, Id));

    List<CommodityImage> images = CommodityImages.Where(ci => ci.CommodityID = Id);
    foreach (var img in images) 
       img.CommodityImageName = img.CommodityImageName.Replace(oldName, newName);

    db.SubmitChanges();
    db.CommitTran();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM