如何在 Visual Studio 中创建和开发新的数据库项目?

发布于 2024-07-05 05:14:03 字数 52 浏览 8 评论 0原文

我想找到一种在 Visual Studio 中快速开发数据库项目的方法。 有任何想法吗?

I want to find a way to develop database projects quickly in Visual Studio. Any ideas?

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

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

发布评论

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

评论(3

半窗疏影 2024-07-12 05:14:03

我有一种在 Visual Studio 2005 中创建和更新数据库项目的方法,我认为这是常识。 在询问了一些同事是否知道如何使用这种方法更新他们的数据库项目并得到答案后,我想我应该在博客上介绍它并传递一些有用的提示和最佳实践。

我经常使用数据库,尤其是为与业务逻辑/数据访问 .NET 框架一起使用而构建的存储过程。 我喜欢使用数据库,并且总是创建数据库项目来与我的 .NET 项目一起使用。 我对保持数据库项目保持最新状态感到疯狂。 在我年轻的时候,我已经浪费了太多时间,我需要创建一个已删除或与使用数据库的应用程序不同步的存储过程。

在 Visual Studio 2005 中创建数据库项目后,如下所示:

alt text http://www. cloudsocket.com/images/image-thumb16.png

在项目中创建 3 个新目录:表、存储过程和函数。 我通常只为我的项目存储这些。

替代文本 http://www.cloudsocket.com/images/image-thumb17.png< /a>

现在,我在 Visual Studio 中打开服务器资源管理器并创建与所需数据库的新连接。 我以 Northwind 为例。 我不会逐步介绍此示例的连接创建过程。

替代文本 http://www.cloudsocket.com/images/image-thumb18.png< /a>

我将使用存储过程作为如何更新数据库项目的示例。 首先,我在 Server Explorer 中展开 Northwind 数据库的“存储过程”目录。 我选择一个存储过程。

替代文本 http://www.cloudsocket.com/images/image-thumb19.png< /a>

我将存储过程拖到解决方案资源管理器中的“存储过程”目录中并将其放下。

替代文本 http://www.cloudsocket.com/images/image-thumb20.png< /a>

替代文本 http://www.cloudsocket.com/images/image-thumb21 .png

如果打开所拖动的存储过程的文件,您会发现 IDE 创建了如下脚本:

/****** Object:  StoredProcedure [dbo].[CustOrdersOrders]    Script Date: 08/25/2007 15:22:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CustOrdersOrders]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CustOrdersOrders]
GO
/****** Object:  StoredProcedure [dbo].[CustOrdersOrders]    Script Date: 08/25/2007 15:22:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CustOrdersOrders]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE CustOrdersOrders @CustomerID nchar(5)
AS
SELECT OrderID,
    OrderDate,
    RequiredDate,
    ShippedDate
FROM Orders
WHERE CustomerID = @CustomerID
ORDER BY OrderID
'
END
GO

您现在可以从数据库中拖动所有表、函数和剩余的存储过程。 您还可以右键单击解决方案资源管理器中的每个脚本,然后在数据库项目引用的数据库上运行脚本。

I have a method of creating and updating database projects in Visual Studio 2005 that I thought was common knowledge. After asking a few coworkers if they knew how to update their database projects with this method and receiving no's, I thought I would blog about it and pass along some helpful hints and best practices.

I work a lot with databases and especially stored procedures that are built to be used with business logic/data access .NET framework. I enjoy working with databases and always create database projects to live with my .NET projects. I am psychotic about keeping database projects up to date. I have been burned too many time in my younger years where I needed to create a stored procedure that was deleted or was out of sync with the application using the database.

After creating your database project in Visual Studio 2005 as shown:

alt text http://www.cloudsocket.com/images/image-thumb16.png

Create 3 new directories in the projects: Tables, Stored Procedures and Functions. I usually only stored these for my projects.

alt text http://www.cloudsocket.com/images/image-thumb17.png

I now open the Server Explorer in Visual Studio and create a new connection to my desired database. I am using Northwind as my example. I am not going to walk through the creation of the connection for this example.

alt text http://www.cloudsocket.com/images/image-thumb18.png

I will use a stored procedure as my example on how to update the database project. First I expand the "Stored Procedures" directory in the Server Explorer for the Northwind database. I select a stored procedure.

alt text http://www.cloudsocket.com/images/image-thumb19.png

I drag the stored procedure to the "Stored Procedures" directory in the Solution Explorer and drop it.

alt text http://www.cloudsocket.com/images/image-thumb20.png

alt text http://www.cloudsocket.com/images/image-thumb21.png

If you open the file for the dragged stored procedures you will find that the IDE created the script as followed:

/****** Object:  StoredProcedure [dbo].[CustOrdersOrders]    Script Date: 08/25/2007 15:22:59 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CustOrdersOrders]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CustOrdersOrders]
GO
/****** Object:  StoredProcedure [dbo].[CustOrdersOrders]    Script Date: 08/25/2007 15:22:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CustOrdersOrders]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE CustOrdersOrders @CustomerID nchar(5)
AS
SELECT OrderID,
    OrderDate,
    RequiredDate,
    ShippedDate
FROM Orders
WHERE CustomerID = @CustomerID
ORDER BY OrderID
'
END
GO

You can now drag over all the tables, functions and remaining stored procedures from your database. You can also right click on each script in the Solution Explorer and run the scripts on your database project's referenced database.

吐个泡泡 2024-07-12 05:14:03

嘿,Chris,我也用同样的方式来保存数据库项目,唯一的问题是,你经常对存储过程进行更改,有时你会忘记更改了哪些存储过程,因此你可能会拖一个而忘记另一个。
您是否知道一种将数据库项目与数据库同步的方法,或者在第一次通过拖动添加存储过程后导入项目中存储过程的最新脚本的方法。

Hey Chris, I also use the same way for keeping a database project, the only problem, is that you often make changes to stored procedures, and sometimes you forget which ones you changed, so you might drag one and forget the other.
Do you know of a way to synchronize the database project with the database, or a way to import latest script for stored procs in your project, after they have been added by dragging the first time.

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