简体   繁体   中英

Set a permanent value of a variable without using a database

I don't know how to describe it thoroughly in the title, but I need to set a permanent value of a variable/flag once a process has return true and maybe set some flag in the program itself the value rather than saving it to database. And once that variable/flag has already have that value then the program won't run the process again and just use the value. Is it possible? I'm using VB.Net. I can't use the database because database can be overridden and change values by using query. Thanks in advance!

You can simply use binary/XML serialization in a file to save the state of that variable through your program. Every time you restart your app you can access the value from that file to get its current state.

You can look at this example - http://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php

Basically, you will not save the value in the database but in a file. Anyways you need to persist the value somewhere.

Some ways below

You did not specify if you are afraid that your application or another one could change the value

How I would do it

My ideas below

1)You could use an xml file for example and zip a copy of it with a strong password. Every time you update the first xml you will update also the encrypted zipped xml.You can use a FileSystemWatcher and capture any file change, so if something/someone has changed the file you just get a new copy from the zip

2)You can store the value in the DB and add a trigger to prevent delete/update

for example

    -- delete trigger
CREATE TRIGGER Function_Value_Deleted
ON [dbo].[FunctionsValueTb]
AFTER DELETE 
AS
BEGIN
SET NOCOUNT ON;



 IF EXISTS (
        SELECT [Flag] FROM deleted
    )
    BEGIN
        ROLLBACK;

RAISERROR ('Record deletion  is not allowed...', 16, 1);
END
END

*You can use also use THROW rather than RAISERROR

**Do the same for the insert and update actions

***You can also store the value into a log table or send an email

I found myself in a situation quite similar to yours a couple of days ago.

In the end, I decided to use the settings functionaly provided by .NET: it is easy to use and maintain, and so far it has given me good results.

Yo can see here what I am talking about: Best practice to save application settings in a Windows Forms Application

That thread refers to C# but is easily applicable for VB.NET: I just had to follow the same steps in order to add the Settings file:

Right click on the project in Solution Explorer, choose Properties. Select the Settings tab, click on the hyperlink if settings doesn't exist. Use the Settings tab to create application settings. Visual Studio creates the files Settings.settings and Settings.Designer.settings that contain the singleton class Settings inherited from ApplicationSettingsBase

And then, from my code, I use the settings like this:

Dim lastExecDate As Date = My.Settings.LastSuccessfulExecution
lastExecDate = lastExecDate.AddDays(1)
// Perform my next execution and do other stuff
My.Settings.LastSuccessfulExecution = lastExecDate
My.Settings.Save()

Next time you retrieve the parameter LastSuccessfulExecution, it will have the updated value.

One more remark, as stated in the post that I linked above:

Note that you need to set the scope property of your settings. If you select Application scope then Settings.Default.< your property > will be read-only

Finally, I see that you are using this to store the expiration date of a product, so you don't want the user messing around with it. According to this post, the actual values of the parameters are stored in an Application Data user folder. It is somehow obfuscated since it is not that easy to find and besides it contains a hash on its name... I don't know if that is well hidden enough for you.

If you want the value only to exist in memory when the application is running then you can use the main thread of the application and use:

int slotData = randomGenerator.Next(1, 200);
//to set the data
Thread.SetData(Thread.GetNamedDataSlot("SomeDataKey"), slotData);
//to get the data
int newSlotData = (int)Thread.GetData(Thread.GetNamedDataSlot("SomeDataKey"));

Or you can use the Windows Registry if your app only runs on Windows, if not then you would have to write the value/object to a file and read it from there.

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