开发者

File save in asp.net and save information in DB

I am creating the user ,using asp.net + C#.there is the form (UI) who collects the information and send data to Business layer. now, while collecting the information from UI I want to also save the image of user, at file sy开发者_StackOverflow中文版stem in application domain.

There is function in the business layer as SaveImage, which returns the path where it is saving. Scenario: 1) Save image first and then save other information in DB. 2) Save Information first in DB and then uploads the image.

Which will best scenario. If scenario 1 is better then what if the save image get crashes ?


Option 2 is better as now you are sure that the file is saved on root.

if you save the record first and then save image file and it fail, you need to rollback or delete record from database.

I hope it can help you


my suggestion is better to put both in the Transaction scope.if one fails both should rollback


First one is more suitable then second because if you fails to store file then you have to go for another DB access or think if you in the same time loose connection with DB then you have a junk information in DB which do not have file. which may cause further errors in future

So better to use

  1. Save image first and then save other information in DB in Transaction


I agree with @anishmarokey that it seems you should have this in some form of transaction that regardless of the order of saving, the data is only retained if all goes well...and appropriate feedback otherwise

that being said, do users or administrators (or whoever) have the ability to change a user's image? Because it may well make more sense that your screen to create users, creates users and saves them and then to have another screen or function that allows the edit of the user's image. And that function can be re-used when a user wants to the change the image.

So your application might have a "Default" picture (the classic vector head outline) and then once a user uploads a new image or changes the image, it is then used instead of the default.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜