如何将.NET与sql数据库连接?
我目前有Visual Studio 2005和SQL Server 2005。我已经在SQL Server中安装了一个空数据库,但我不知道如何使用VS连接数据库。
我应该使用什么样的项目(我将在 Windows 应用程序中使用数据库)以及如何将其插入到项目中并使用它?
编辑:我在 Microsoft SQL Server Managament Studio 中有一个数据库,我想使用 c# 在 winForm 中使用它。我必须以某种方式连接这两个?
I currently have Visual Studio 2005 and SQL Server 2005. I have installed an empty database into SQL Server, but I don't have any idea how to use VS to connect with the database.
What kind of project should I use (I'm going to use the database in a windows application) and exactly how will I be able to insert it to the project and use it?
EDIT: I have a database in Microsoft SQL Server Managament Studio, and I want to use it in a winForm, using c#. I have to connect these two somehow ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以创建以下任一项目类型:
要连接到数据库,请使用下面的代码:
我建议您挑选一本关于 ADO.NET 的好书并通读它。你也可以在谷歌上搜索一些文章。
You can create either of the project type:
To connect to database, use the code below:
I suggest you pick up a good book on ADO.NET and read through it. You could search for some articles on Google too.
试试这个 ADO.NET 示例应用程序
再见
try this ADO.NET Sample Application
bye
最简单的方法是使用 ADO.Net ,没有特殊的项目可以使用,只需使用普通的 Winforms 项目(你说你已经在做了)。
以下是您可以遵循的一些基础知识 http://www.sitepoint.com/article /简介-ado-net/
The simplest way is using ADO.Net , there is no special project to use , just use a normal Winforms Project (which you said you are already doing).
Here are some basics that you can follow http://www.sitepoint.com/article/introduction-ado-net/
您有几个选择 - 最简单的(但性能最低、理想或推荐用于企业规模使用)是通过 Visual Studio 中的“服务器资源管理器”。通过查看...服务器资源管理器...进行访问并展开数据连接节点,然后浏览新创建的数据库。
从这里您可以将数据源等拖到您的 Windows 应用程序上。
但理想情况下,您应该直接使用 ADO、存储过程或 SQL。
You've a few options - the easiest (but least performant, ideal, or recommended for enterprise-scale use) is via the 'Server Explorer' in Visual Studio. Accessing via View...Server Explorer... and expand Data Connections node, and browse your newly created database.
From here you can drag data sources etc onto your Windows Application.
Ideally however you'd use ADO, stored procedure or SQL directly.
在 Visual Studio 中,您有一个数据菜单选项,可以打开数据源视图。添加数据连接并使用向导“定位”数据库。创建表的本地“表示”后,将该表从数据源选项卡拖动到 Windows 窗体应用程序。
查看所有自动创建的对象并尝试创建对象列表以及它们如何交互。您能找到连接字符串或表定义吗?
In visual studio you got a Data menu option that can open a Data Sources view. Add a data connection and use the wizard to 'target' the database. After creating a local 'representation' of a table, drag that table from the datasource tab to you windows form application.
Look at all the automatically created objects and try to create a list of objects and how they interact. Can you spot the connectionstring or the table definition?
您可能希望考虑 nHibernate 或类似的 ORM。 ORM 层不是本地使用 ADO.Net,而是可以帮助减少您正确的重复数据访问代码中容易出错的数量。
学习曲线比跳入 ADO.Net 更陡峭,但并不明显,并且您需要学习很多东西 - 关系、事务和“业务事务”、延迟加载,无论如何您都需要考虑 - 所以我表明被迫思考它们并不是坏事。
查看 www.nhforge.org 获取一些很棒的入门资源。
You may wish to consider nHibernate or a similiar ORM. Rather than using ADO.Net natively, the ORM layer can help reduce the amount of error prone an repetitive data access code that you right.
The learning curve is steeper than jumping into ADO.Net, but not significantly, and lots of the things you will need to learn about - relations, transactions and "business transactions", lazy loading, you will need to think about anyway - so I would suggest that being forced to think about them is no bad thing.
Checkout www.nhforge.org for some great getting started resources.
var connectionString = _dbContext.Database.GetDbConnection().ConnectionString;
var connectionString = _dbContext.Database.GetDbConnection().ConnectionString;