在WPF应用程序中本地保存数据的最佳方法是什么?

时间:2022-02-22 16:58:11

I'm building some WPF application to manage my time. I'd like to save time spans for work time on project for a calendar day.

我正在构建一些WPF应用程序来管理我的时间。我想在一个日历日的项目上节省工作时间。

So my question is what's the best choice on how to save the data? I figured I could use an XML file, an Access database file or maybe a property. I´d like to save the data locally, so no SQL Server action in this case. :)

所以我的问题是如何保存数据的最佳选择?我想我可以使用XML文件,Access数据库文件或属性。我想在本地保存数据,因此在这种情况下没有SQL Server操作。 :)

What do you think, which way to go?

你怎么看,走哪条路?

4 个解决方案

#1


5  

I would grab SQLite either in its pure or a .NET-friendly form (doing a Google search for 'sqlite .net' will give you a few options there). It's super-portable and, in my opinion, easier to set up and distribute than SQL Server compact.

我会以纯粹或.NET友好的形式获取SQLite(在Google上搜索'sqlite .net'会给你一些选项)。它是超级便携的,在我看来,比SQL Server compact更容易设置和分发。

The important thing is to make sure you are not too tightly coupled to your persistence mechanism in your code, so in the future you could easily substitute any storage strategy you want.

重要的是确保您与代码中的持久性机制没有太紧密的联系,因此将来您可以轻松替换您想要的任何存储策略。

#2


6  

I know you said no SQL Server, but I am reading that to mean you want no "server", and you want to store your data on the client. I also assume you probably wouldn't mind some manageability of your data. You know, things like backup. And transactions are always nice, so your data can remain consistent. So, while you could use XML (please banish all thoughts of Access from your mind), you would end up rolling your own persistence, when this is a solved problem.

我知道你说没有SQL Server,但我读到这意味着你不想要“服务器”,并且你想将数据存储在客户端上。我还假设您可能不介意数据的某些可管理性。你知道,比如备份。事务总是很好,因此您的数据可以保持一致。所以,虽然你可以使用XML(请从头脑中消除所有关于Access的想法),但当这是一个已解决的问题时,你最终会滚动自己的持久性。

So, please check out the free SQL Server Compact edition. It is lightweight, designed to run on a desktop or mobile device, and is easily deployable if your app ever needs to do that. And all the common persistence frameworks support it. And have I mentioned it is free (as in costs nothing)?

所以,请查看免费的SQL Server Compact版本。它非常轻巧,可以在桌面或移动设备上运行,并且如果您的应用程序需要这样做,则可以轻松部署。所有常见的持久性框架都支持它。我提到它是免费的(如没有成本)?

#3


4  

I'd suggest picking the easiest datasource possible and decoupling it appropriately so that you can drop in a new, different datasource at a later time when you figure out what's appropriate for your purposes. To that end, you may find something like XML or even plaintext to be the simplest thing that could possibly work.

我建议选择最简单的数据源并将其适当地解耦,这样当你找出适合你的目的的时候,你可以在以后放入一个新的,不同的数据源。为此,您可能会发现像XML甚至纯文本这样的东西可能是最简单的东西。

Once you determine the characteristics that you'll need from your datasource, based on your actual usage, choose the appropriate backing store.

根据实际使用情况确定数据源所需的特征后,请选择适当的后备存储。

I don't think it's critical to make this decision up front because this is a personal project, not a commercial one.

我不认为做出这个决定是至关重要的,因为这是一个个人项目,而不是商业项目。

#4


1  

One dead simple approach I've used in the past is your "xml file" idea. Simply create an object that describes the data you care about, and then serialize it to xml.

我过去使用的一种简单的方法是你的“xml文件”的想法。只需创建一个描述您关注的数据的对象,然后将其序列化为xml。

Greg is absolutely correct when he says make sure your datasource is decoupled properly so you can switch it out if your requirements change.

Greg绝对是正确的,他说确保您的数据源正确解耦,以便在需求发生变化时将其切换出来。

#1


5  

I would grab SQLite either in its pure or a .NET-friendly form (doing a Google search for 'sqlite .net' will give you a few options there). It's super-portable and, in my opinion, easier to set up and distribute than SQL Server compact.

我会以纯粹或.NET友好的形式获取SQLite(在Google上搜索'sqlite .net'会给你一些选项)。它是超级便携的,在我看来,比SQL Server compact更容易设置和分发。

The important thing is to make sure you are not too tightly coupled to your persistence mechanism in your code, so in the future you could easily substitute any storage strategy you want.

重要的是确保您与代码中的持久性机制没有太紧密的联系,因此将来您可以轻松替换您想要的任何存储策略。

#2


6  

I know you said no SQL Server, but I am reading that to mean you want no "server", and you want to store your data on the client. I also assume you probably wouldn't mind some manageability of your data. You know, things like backup. And transactions are always nice, so your data can remain consistent. So, while you could use XML (please banish all thoughts of Access from your mind), you would end up rolling your own persistence, when this is a solved problem.

我知道你说没有SQL Server,但我读到这意味着你不想要“服务器”,并且你想将数据存储在客户端上。我还假设您可能不介意数据的某些可管理性。你知道,比如备份。事务总是很好,因此您的数据可以保持一致。所以,虽然你可以使用XML(请从头脑中消除所有关于Access的想法),但当这是一个已解决的问题时,你最终会滚动自己的持久性。

So, please check out the free SQL Server Compact edition. It is lightweight, designed to run on a desktop or mobile device, and is easily deployable if your app ever needs to do that. And all the common persistence frameworks support it. And have I mentioned it is free (as in costs nothing)?

所以,请查看免费的SQL Server Compact版本。它非常轻巧,可以在桌面或移动设备上运行,并且如果您的应用程序需要这样做,则可以轻松部署。所有常见的持久性框架都支持它。我提到它是免费的(如没有成本)?

#3


4  

I'd suggest picking the easiest datasource possible and decoupling it appropriately so that you can drop in a new, different datasource at a later time when you figure out what's appropriate for your purposes. To that end, you may find something like XML or even plaintext to be the simplest thing that could possibly work.

我建议选择最简单的数据源并将其适当地解耦,这样当你找出适合你的目的的时候,你可以在以后放入一个新的,不同的数据源。为此,您可能会发现像XML甚至纯文本这样的东西可能是最简单的东西。

Once you determine the characteristics that you'll need from your datasource, based on your actual usage, choose the appropriate backing store.

根据实际使用情况确定数据源所需的特征后,请选择适当的后备存储。

I don't think it's critical to make this decision up front because this is a personal project, not a commercial one.

我不认为做出这个决定是至关重要的,因为这是一个个人项目,而不是商业项目。

#4


1  

One dead simple approach I've used in the past is your "xml file" idea. Simply create an object that describes the data you care about, and then serialize it to xml.

我过去使用的一种简单的方法是你的“xml文件”的想法。只需创建一个描述您关注的数据的对象,然后将其序列化为xml。

Greg is absolutely correct when he says make sure your datasource is decoupled properly so you can switch it out if your requirements change.

Greg绝对是正确的,他说确保您的数据源正确解耦,以便在需求发生变化时将其切换出来。