演练:使用托管代码创建存储过程

浏览:44日期:2023-04-30

SQL Server 2005 数据库的存储过程现在可以使用 .NET Framework 语言(如 Visual Basic、C# 和 C++)在托管代码中进行编写。使用托管代码编写的存储过程称为 CLR 存储过程。

通过将“存储过程”项添加到 SQL Server 项目,可以创建 SQL 存储过程。成功部署到 SQL Server 之后,可通过与任何其他存储过程相同的方式调用和执行在托管代码中创建的存储过程。

本演练演示如下任务:

创建新的“Windows 应用程序”项目。

在托管代码中创建存储过程。

将此存储过程部署到 SQL Server 2005 数据库。

创建在数据库上测试存储过程的脚本。

查询数据库中的数据,以确认是否正确执行了存储过程。

先决条件 若要完成本演练,您需要:

到运行在 SQL Server 2005 之上的 AdventureWorks 示例数据库的连接。有关更多信息,请参见 如何:安装示例数据库。

创建项目 创建新的 SQL Server 项目从“文件”菜单创建一个新的项目。

选择“SQL Server 项目”,将项目命名为 SQLCLRStoredProcedure 并单击“确定”。有关更多信息,请参见 如何:创建 SQL Server 项目。

连接到 SQL Server 2005 数据库 本演练需要到运行在 SQL Server 2005 之上的 AdventureWorks 示例数据库的连接。如果到 AdventureWorks 示例数据库的连接在“服务器资源管理器”中可用,则该连接会在 “添加数据库引用”对话框 中列出。

注意; 在默认情况下,Microsoft SQL Server 中关闭了公共语言运行库 (CLR) 集成功能。必须启用该功能才能使用 SQL Server 项目项。若要启用 CLR 集成,请使用 sp_configure 存储过程的 clr enabled 选项。有关更多信息,请参见 启用 CLR 集成。

连接到 AdventureWorks 示例数据库完成“添加连接”对话框,从而连接到包含在 SQL Server 2005 中的 AdventureWorks 示例数据库。有关更多信息,请参见 添加/修改连接 (Microsoft SQL Server)。

- 或 -

在“添加数据库引用”对话框中选择到 AdventureWorks 示例数据库的一个现有连接。有关更多信息,请参见 “添加数据库引用”对话框。

创建 SQL Server 存储过程 创建了 SQL Server 项目后,向其中添加一个存储过程。

创建 SQL Server 存储过程从“项目”菜单中选择“添加新项”。

在 “添加新项”对话框 中选择“存储过程”。

键入 InsertCurrency 作为新存储过程的“名称”。

单击“添加”。

使用以下代码替换代码编辑器中的代码:

注意; C++ 示例在编译时必须使用 /clr:safe 编译器选项。Visual Basic 复制代码Imports SystemImports System.DataImports System.Data.SqlClientImports System.Data.SqlTypesImports Microsoft.SqlServer.Server

Partial Public Class StoredProcedures

<SqlProcedure()> _ Public Shared Sub InsertCurrency( _ ByVal currencyCode As SqlString, ByVal name As SqlString)

Using conn As New SqlConnection('context connection=true')

Dim InsertCurrencyCommand As New SqlCommand() Dim currencyCodeParam As New SqlParameter('@CurrencyCode', SqlDbType.NVarChar) Dim nameParam As New SqlParameter('@Name', SqlDbType.NVarChar)

currencyCodeParam.Value = currencyCode nameParam.Value = name

InsertCurrencyCommand.CommandText = _ 'INSERT Sales.Currency (CurrencyCode, Name, ModifiedDate)' & _ ' VALUES(@CurrencyCode, @Name)'

InsertCurrencyCommand.Connection = conn

conn.Open() InsertCurrencyCommand.ExecuteNonQuery() conn.Close() End Using End SubEnd ClassC# 复制代码using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;

public partial class StoredProcedures{ [SqlProcedure()] public static void InsertCurrency_CS( SqlString currencyCode, SqlString name) { using (SqlConnection conn = new SqlConnection('context connection=true')) { SqlCommand InsertCurrencyCommand = new SqlCommand(); SqlParameter currencyCodeParam = new SqlParameter('@CurrencyCode', SqlDbType.NVarChar); SqlParameter nameParam = new SqlParameter('@Name', SqlDbType.NVarChar);

InsertCurrencyCommand.CommandText = 'INSERT Sales.Currency (CurrencyCode, Name, ModifiedDate)' + ' VALUES(@CurrencyCode, @Name)';

InsertCurrencyCommand.Connection = conn;

conn.Open(); InsertCurrencyCommand.ExecuteNonQuery(); conn.Close(); } }}C++ 复制代码#include 'stdafx.h'

#using <System.dll>#using <System.Data.dll>#using <System.Xml.dll>

using namespace System;using namespace System::Data;using namespace System::Data::Sql;using namespace System::Data::SqlClient;using namespace System::Data::SqlTypes;using namespace Microsoft::SqlServer::Server;

// In order to debug your Stored Procedure, add the following to your debug.sql file://// EXEC InsertCurrency_CPP 'AAA', 'Currency Test'// SELECT * FROM Sales.Currency WHERE CurrencyCode = 'AAA'

public ref class StoredProcedures{public: [SqlProcedure] static void InsertCurrency_CPP(SqlString currencyCode, SqlString name) { SqlConnection ^conn = gcnew SqlConnection('context connection=true');

SqlCommand ^insertCurrencyCommand = gcnew SqlCommand(); SqlParameter ^currencyCodeParam = gcnew SqlParameter('@CurrencyCode', SqlDbType::NVarChar); SqlParameter ^nameParam = gcnew SqlParameter('@Name', SqlDbType::NVarChar);

insertCurrencyCommand->CommandText = 'insert Sales.Currency(CurrencyCode, Name, ModifiedDate)' + ' values(@CurrencyCode, @Name)'; insertCurrencyCommand->Connection = conn;

conn->Open(); insertCurrencyCommand->ExecuteNonQuery();

conn->Close(); }};

部署、执行和调试存储过程 创建新的存储过程后,可以通过按 F5 生成该存储过程,将其部署到 SQL Server 并进行调试。首先,在位于项目的“TestScripts”文件夹中的“Test.sql”文件中,添加执行和测试存储过程的代码。在 Visual C++ 中,此文件名为“debug.sql”。有关创建测试脚本的更多信息,请参见 如何:编辑 Test.sql 脚本以运行 SQL 对象。

有关调试 SQL 的更多信息,请参见 调试 SQL 数据库对象。

部署并运行 InsertCurrency 存储过程对于 Visual Basic 和 Visual C#,在“解决方案资源管理器”中,展开“TestScripts”文件夹,再双击“Test.sql”文件。

对于 Visual C++,在“解决方案资源管理器”中,双击“debug.sql”文件。

使用以下代码替换“Test.sql”(在 Visual C++ 中为“debug.sql”)文件中的代码:

复制代码EXEC InsertCurrency 'AAA', 'Currency Test'SELECT * from Sales.Currency where CurrencyCode = 'AAA'按 F5 生成、部署并调试该存储过程。有关不进行调试直接部署的信息,请参见 如何:将 SQL Server 项目项部署到 SQL Server 中。

在“输出窗口”中查看结果,然后选择“从此处显示输出: 数据库输出”。

相关文章: