您的位置:首页 > 编程语言 > C#

用C#写托管的存储过程 (翻译一)

2007-03-21 23:56 239 查看
原文连接:
http://www.c-sharpcorner.com/UploadFile/pk_khuman/ManagedStoredProceduresUsingCSharp02182007232059PM/ ManagedStoredProceduresUsingCSharp.aspx

介绍

随着SQL Server 2005中集成了CLR,我们可以用现代面向对象语言例如VB.NET 和C# 来建立数据库对象.事实上,为了抽象出如计算,字符串逻辑分析等与数据库无关的存取代码,我们使用.NET来写SQL Server的对象.最好用托管代码来写存储过程.同样的为了访webservices,为OOP编程提供更好的可复用性和读取外部文件,托管的存储过程也是一个不错的选择.

This article is trying to explain the simple and required steps that are require starting the creation of Manage Stored Procedure using C# and using them.

这篇文章将会用简单而必需的步骤来说明如何使用C#来建立托管的存储过程,还有如何使用.

项目

我们将为托管的存储过程建立一个Visual Studio 2005的数据库项目

建立数据库项目:

打开微软的Visual Studio 2005建立一个SQL Server的项目

File->New->Project->Database




添加一个数据库引用

现在将会需要一个数据库的引用,添加一个



添加存储过程

右击项目添加一个存储过程





SPOne.cs文件

下面为SPOne.cs文件的代码.确保你的数据库里面存在Person表 或者用你数据库中的表替代Person表

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

public partial class StoredProcedures

{

[Microsoft.SqlServer.Server.SqlProcedure]

public static void SPOne()

{

SqlPipe p;

SqlCommand sCmd = new SqlCommand();

sCmd.CommandText = "Select * from Person";

p = SqlContext.Pipe;

p.ExecuteAndSend(sCmd);

}

};

部署存储过程

建立并部署项目



运行存储过程

用下面的SQL语句来验证CLR可以在你的SQ: Server中运行.

sp_configure 'clr enabled', 1;

GO

RECONFIGURE;
GO

Now execute the Stored Procedure and you will get an output of select statement.



Make your Life follows Procedures and Stored them safely! If possible, manage them!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: