从 Windows 窗体应用程序进行 C# 轻量级数据访问

发布于 2024-09-09 02:38:15 字数 106 浏览 0 评论 0原文

我有一个简单的 C# 应用程序,需要访问保留两个变量的管理表。

有人可以告诉我与数据库交互以检索和更新表的最快(就开发时间而言)的方法是什么。我不想创建整个数据层只是为了访问单个表?

I have a simple C# Application that requires access to an admin table which persists two variables.

Can someone tell me what is the fastest(in terms of dev time) method to interact with the database to retrieve and update the table. I don't wish to create an entire data layer just to gain access to a single table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

皇甫轩 2024-09-16 02:38:15

普通的旧 ADO.NET 应该没问题:

using (var conn = new SqlConnection("SOME CONNECTION STRING"))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = "UPDATE mytable SET foo = 'bar'";
    cmd.ExecuteNonQuery();
}

Plain old ADO.NET should be fine:

using (var conn = new SqlConnection("SOME CONNECTION STRING"))
using (var cmd = conn.CreateCommand())
{
    conn.Open();
    cmd.CommandText = "UPDATE mytable SET foo = 'bar'";
    cmd.ExecuteNonQuery();
}
三寸金莲 2024-09-16 02:38:15

Linq To SQL 是最佳选择。您从数据库中拖放表,生成类,然后您可以访问您的表,就好像它只是程序中的变量一样。我喜欢这个教程: http://www.thereforesystems.com/linq-to- sql-tutorial/ 但还有很多其他的,只是谷歌。

Linq To SQL is the best choice. You drag-and-drop your table from DB, get class generated then you can access your table as if it is just a variable in your program. I liked this tutorial: http://www.thereforesystems.com/linq-to-sql-tutorial/ but there are many others, just google.

歌枕肩 2024-09-16 02:38:15

您应该使用现有的 ORM(类似)框架之一,例如:

  • LINQ-to-Entities(首选,您可以在 VS.NET 中对其进行开箱即用的支持)
  • LINQ-to-SQL(不那么首选) ,MS 的支持正在减弱,仅支持 SQL Server)
  • NHibernate(您将需要第三方工具来支持 VS.NET 中的设计)

其中大多数(如果不是全部)将允许您指定数据源并只需拖动表即可进行设计将为您创建数据传输对象的表面,以及查询并将它们保留回数据存储的机制。

You should use one of the existing ORM(-like) frameworks out there, such as:

  • LINQ-to-Entities (preferred, you have support for it out of the box in VS.NET)
  • LINQ-to-SQL (not so preferred, support from MS is waning and only supports SQL Server)
  • NHibernate (you will need third party tools for design support in VS.NET)

Most (if not all) of these will allow you to specify your data source and simply drag tables to design surfaces at which point your Data Transfer Objects will be created for you, as well as the mechanisms to query and persist them back to the data store.

拿命拼未来 2024-09-16 02:38:15

您使用什么版本的 .NET?如果是 3.5 或更高版本,请使用使用 http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.aspx 命名空间。

What version of .NET do you use? If 3.5 or later, use LINQ2SQL marked up with attributes from the http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.aspx namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文