如何在c#中使用本地数据库?
我为 C# 项目创建了一个本地数据库:
我了解基本的 SQL 命令,但还没有使用过 C# 中的数据库。 我想具体了解的是:
- 如何从数据库读取(查询)
- 如何添加和更新行
数据库仅由 3 个表组成,所以我认为不需要任何花哨的东西。
I've made a local database for a C# project:
I know basic SQL commands, but haven't worked with databases in 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您应该了解一些用于连接数据库的各种技术和 API。
更传统的方法是 ADO.NET,它允许您非常轻松地定义连接并执行 SQL 查询或存储过程。我建议使用 Google 挖掘有关 ADO.NET 的基本教程,该教程可能会有所不同,具体取决于您创建的项目类型(Web 应用程序、控制台、WinForms 等)。
如今,ORM 变得越来越流行。它们允许您在代码中定义对象模型(例如每个数据库表都是一个类,列将是该类的属性)并绑定到现有数据库。要向表中添加新行,您只需创建一个类的实例并在完成后调用“保存”方法。
.NET 框架有针对此类模式的 LINQ to SQL 和实体框架,这两者都有大量在线教程。我非常喜欢的一个开源项目是 Castle Active Record,它构建在 NHibernate 之上。它使得定义 ORM 变得非常容易。
如果您对上述任何问题有具体问题,请随时发布包含更具体查询的新问题。祝你好运!
更新:
我想我还应该添加最后一个参考,因为您似乎对使用本地数据库存储而不是构建客户端/服务器应用程序感兴趣。 SQLite 允许您通过 SQL 代码与文件系统上的本地存储进行交互。还有一个由 SQLite 人员维护的 .NET 绑定(理论上它允许您使用我提到的其他平台): http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
First, you should learn a bit about various technologies and APIs for connecting with a database.
The more traditional method is ADO.NET, which allows you to define connections and execute SQL queries or stored procedures very easily. 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).
Now days, ORMs are becoming increasingly popular. 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. An open source project I really like is Castle Active Record, which is built on top of NHibernate. It makes defining ORMs quite easy.
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. 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
您可以使用 SQLCE。
这个博客将为您提供一个良好的开始。
http://weblogs .asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx
You can use 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
这是一个小教程,应该对您有所帮助。
您可以使用 SqlDataReader 读取数据
,并使用 SqlCommand 插入、更新、删除表中的行。
http://www.dotnetperls.com/sqlclient
Here is a small tutorial that should be helpful to you.
You can make use of the SqlDataReader to read data
and the SqlCommand to Insert Update Delete rows from your tables.
http://www.dotnetperls.com/sqlclient
您可以通过将以下内容添加到 Startup.cs 来使用它
You could use it by adding following to your Startup.cs