简体   繁体   English

如何在 C# 中使用本地数据库?

[英]How to use a local database in c#?

I've made a local database for a C# project:我为 C# 项目创建了一个本地数据库:这是我的意思的本地数据库。

I know basic SQL commands, but haven't worked with databases in C#.我知道基本的 SQL 命令,但没有使用 C# 中的数据库。 What I'd like to know specifically is:我想具体了解的是:

  • How to read from the database (query)如何从数据库中读取(查询)
  • How to add and update rows如何添加和更新行

The database only consists of 3 tables, so I don't think anything fancy is needed.该数据库仅包含 3 个表,因此我认为不需要任何花哨的东西。

First, you should learn a bit about various technologies and APIs for connecting with a database. 首先,您应该了解一些用于连接数据库的各种技术和API。

The more traditional method is ADO.NET, which allows you to define connections and execute SQL queries or stored procedures very easily. 更传统的方法是ADO.NET,它允许您非常轻松地定义连接并执行SQL查询或存储过程。 I recommend digging up a basic tutorial on ADO.NET using Google, which may differ depending on what type of project you're creating (web app, console, WinForms, etc). 我建议使用Google挖掘ADO.NET的基础教程,根据您正在创建的项目类型(Web应用程序,控制台,WinForms等),这可能会有所不同。

Now days, ORMs are becoming increasingly popular. 现在,ORM越来越受欢迎。 They allow you to define your object model in code (such as every database table would be a class, and columns would be properties on that class) and bind to an existing database. 它们允许您在代码中定义对象模型(例如,每个数据库表都是一个类,列将是该类的属性)并绑定到现有数据库。 To add a new row to a table, you'd just create an instance of a class and call a "Save" method when you're done. 要向表中添加新行,您只需创建一个类的实例,并在完成后调用“保存”方法。

The .NET framework has LINQ to SQL and the Entity Framework for this sort of pattern, both of which have plenty of tutorials online. .NET框架具有LINQ to SQL和用于此类模式的实体框架,这两种模式都有大量的在线教程。 An open source project I really like is Castle Active Record, which is built on top of NHibernate. 我非常喜欢的一个开源项目是Castle Active Record,它建立在NHibernate之上。 It makes defining ORMs quite easy. 它使定义ORM变得非常容易。

If you have specific questions about any of the above, don't hesitate to post a new question with more specific inquiries. 如果您对上述任何一个问题有任何疑问,请随时通过更具体的问题发布新问题。 Good luck! 祝好运!

Update: 更新:

I thought I'd also put in one last reference as it seems you might be interested in working with local database stores rather than building a client/server app. 我以为我还会提到最后一个参考,因为您似乎对使用本地数据库存储而不是构建客户端/服务器应用程序感兴趣。 SQLite allows you to interact with local stores on the file system through SQL code. SQLite允许您通过SQL代码与文件系统上的本地存储进行交互。 There's also a .NET binding maintained by the SQLite guys (which would in theory allow you to work with the other platforms I mentioned): http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki 还有一个由SQLite人员维护的.NET绑定(理论上允许你使用我提到的其他平台): http//system.data.sqlite.org/index.html/doc/trunk/www/ index.wiki

You can use SQLCE. 您可以使用SQLCE。

This blog will give you a good start. 这个博客将为您提供一个良好的开端。

http://weblogs.asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx http://weblogs.asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx

您可以像这样使用它: services.AddDbContext(options => options.UseSqlite("Filename=data.db"));

Here is a small tutorial that should be helpful to you. 这是一个应该对您有所帮助的小教程。

You can make use of the SqlDataReader to read data 您可以使用SqlDataReader来读取数据

and the SqlCommand to Insert Update Delete rows from your tables. 和SqlCommand插入更新从表中删除行。

http://www.dotnetperls.com/sqlclient http://www.dotnetperls.com/sqlclient

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

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