使用本地数据库的 WPF C# 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25176424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:18:01  来源:igfitidea点击:

WPF C# application using local database

c#sqlwpfdatabasesql-server-2014-express

提问by Quass1m

I am a novice when it comes to using a database in an application. My task (and sort of a goal) is to create an application in WPF using C# that can connect to/use local database stored on user's computer. The database 'file' should be easy to load and move between machines and, if possible, created on user's PC during the application's installation. The database needs to have an option to be modified from within the application as well as from a respective DB's management tool for updating a large portion of it's content. DB's file security is not an issue in the case of this project. I know that what I've described may have no sense at all and I could simply export applications settings to a file but it's not what I am looking for in this case. So far I have created a DB in Microsoft SQL Server 2014 Express but to be honest I don't know if it's possible to move this DB to another PC in the way I have described (at least without installing a new instance of SQL Server on this machine).

在应用程序中使用数据库时,我是新手。我的任务(以及某种目标)是使用 C# 在 WPF 中创建一个应用程序,该应用程序可以连接到/使用存储在用户计算机上的本地数据库。数据库“文件”应该很容易在机器之间加载和移动,如果可能,在应用程序安装期间在用户的 PC 上创建。数据库需要有一个选项,可以从应用程序内部以及从相应数据库的管理工具中进行修改,以更新其大部分内容。在本项目中,DB 的文件安全性不是问题。我知道我所描述的内容可能根本没有意义,我可以简单地将应用程序设置导出到文件,但这不是我在这种情况下要寻找的。

As I have mentioned in the beginning I am a novice regarding database's so any advice is appreciated.

正如我在开始时提到的,我是一个关于数据库的新手,所以任何建议都值得赞赏。

采纳答案by Jens H

If I understood you correctly you are looking for some kind of portable database, right? In this case there are multiple established solutions available that work well with C#.

如果我理解正确,您正在寻找某种便携式数据库,对吗?在这种情况下,有多种既定的解决方案可以很好地与 C# 配合使用。

These are a few that might be worth a look for you:

以下是一些可能值得您一看的内容:

Full grown databases that can be used as embedded version without a server running in the background.

可用作嵌入式版本的完整数据库,无需在后台运行服务器。

  • Firebird
  • MySql(You might first want to check the license restrictions for using this.)
  • 火鸟
  • MySql(您可能首先要检查使用它的许可证限制。)

My recommendation would be SQLitebecause it is very easy to get running with it. Additionally, there are many, many DB admin tools out there that are freely available. My personal favorite, however, is a paid product, Firebird Maestro. I found working with it quite intuitive and although it is a paid-for product, it is still quite affordable (they also have multiple promotions per year).

我的建议是SQLite,因为它很容易运行。此外,还有许多免费的数据库管理工具。然而,我个人最喜欢的是付费产品Firebird Maestro。我发现使用它非常直观,虽然它是付费产品,但它仍然相当实惠(他们每年也有多次促销活动)。

But still, there are plenty of other free tools to choose from. Your favorite internet search engine will be your friend. :-)

但是,仍有许多其他免费工具可供选择。您最喜欢的互联网搜索引擎将成为您的朋友。:-)

回答by Dujskan

I suggest you take a look at how to setup a code first database. It might need some research but its well worth it. Especially if you are comfortable with code and dont got a database setup yet. Here is a tutorial to get starterd: http://msdn.microsoft.com/en-us/data/jj193542.aspx

我建议您看看如何设置代码优先数据库。它可能需要一些研究,但非常值得。特别是如果您对代码感到满意并且还没有设置数据库。这是入门教程:http: //msdn.microsoft.com/en-us/data/jj193542.aspx

回答by mik61

I would recommend you to choose SQL Server Compact (CE) over SQLite for the following reason: if you are using Visual Studio Express edition, you will be able to use Entity Framework to SQL Server CE out of the box. SQLite will require you to have paid version of VS in such case.

我建议您选择 SQL Server Compact (CE) 而不是 SQLite,原因如下:如果您使用的是 Visual Studio Express 版本,您将能够开箱即用地使用 Entity Framework 到 SQL Server CE。在这种情况下,SQLite 将要求您拥有付费版本的 VS。

The lack of stored procedure support in SQL Server CE may be compensated by the power of L2E queries. The only considerable limit is database file size (4 Gb only). Plus, you can silently deploy SQL Server CE runtime with your application (called "private deployment").

SQL Server CE 中存储过程支持的缺乏可以通过 L2E 查询的能力来弥补。唯一相当大的限制是数据库文件大小(仅限 4 Gb)。此外,您可以使用您的应用程序静默部署 SQL Server CE 运行时(称为“私有部署”)。

回答by Savvas Kleanthous

There are a great many number of embeddable databases that you can use, each with its own benefits and drawbacks. You can use SQLite, MS SQL CE or any other of many choices.

您可以使用大量的可嵌入数据库,每个数据库都有自己的优点和缺点。您可以使用 SQLite、MS SQL CE 或众多选择中的任何其他选项。

Depending on your scenario you can also use XML or Json files as well.

根据您的场景,您也可以使用 XML 或 Json 文件。