简体   繁体   English

在不使用数据库的情况下设置变量的永久值

[英]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. 我不知道如何在标题中完整地描述它,但是一旦进程返回true,我需要设置一个变量/标志的永久值,并且可能在程序本身中将一些标志设置为该值,而不是将其保存到数据库中。 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. 我正在使用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. 您只需在文件中使用二进制/ XML序列化即可通过程序保存该变量的状态。 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 您可以查看此示例-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. 1)例如,您可以使用xml文件,并使用强密码压缩其副本。 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 每次更新第一个xml时,您还将更新加密的压缩xml。您可以使用FileSystemWatcher捕获任何文件更改,因此,如果某人/某人更改了文件,则只需从zip文件中获取一个新副本即可。

2)You can store the value in the DB and add a trigger to prevent delete/update 2)您可以将值存储在数据库中并添加触发器以防止删除/更新

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 *您也可以使用THROW而不是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. 最后,我决定使用.NET提供的功能设置:它易于使用和维护,到目前为止,它给了我很好的效果。

Yo can see here what I am talking about: Best practice to save application settings in a Windows Forms Application 您可以在这里看到我在说什么: 在Windows窗体应用程序中保存应用程序设置的最佳实践

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: 该线程引用了C#,但是很容易适用于VB.NET:我只需要遵循相同的步骤即可添加设置文件:

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 Visual Studio创建文件Settings.settings和Settings.Designer.settings,这些文件包含从ApplicationSettingsBase继承的单例类Settings。

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. 下次检索参数LastSuccessfulExecution时,它将具有更新的值。

One more remark, as stated in the post that I linked above: 正如我上面链接的帖子中所述,还有另外一句话:

Note that you need to set the scope property of your settings. 请注意,您需要设置设置的scope属性。 If you select Application scope then Settings.Default.< your property > will be read-only 如果选择“应用程序作用域”,则“ Settings.Default。<您的属性>”将为只读

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. 根据这篇文章,参数的实际值存储在Application Data用户文件夹中。 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. 或者,如果您的应用程序仅在Windows上运行,则可以使用Windows注册表;否则,您必须将值/对象写入文件并从那里读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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