All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET2.0中实现图像转换过滤效果_.net资料_编程技术
营销型网站具体优化流程_优化技巧_seo学堂
PHP开发中接收复选框信息的方法_php资料_编程技术
Weblogic,Portal中实现AJAX编程之架构_[Asp.Net教程]
SQL中的五种数据类型_[SQL,Server教程]
技巧:PHP的优化,缓冲,压缩实际的解决方案_php资料_编程技术
按指定排列顺序获取数据的sql语句_[SQL Server教程]
如何紧急恢复SQL Server的主数据库_[SQL Server教程]
delphi获取System目录
javascript函数,判断数字的合法性_JavaScript技术_编程技术
Reading number is top 10 pictures
西方气质的东方美女1
Seductive beauty of crime2
狗狗与主人神同步1
In 2013 hercules Arnold classic1
赵惟依写真2
西方气质的东方美女3
一万二一支的万珂,用得真心肉疼。
分手的感悟
这张图有两句话,你看出来了吗?
The real super beauty11
Download software ranking
Boxer's Top ten classic battle5
Unix video tutorial4
apache-tomcat-6.0.33
Tram sex maniac 2 (H) rar bag14
Be there or be square
jBuilder2006
网页特效实例大全
Call Of Duty5
The cock of the Grosvenor LTD handsome
ASP.NET.2.0.XML.高级编程(第3版)
delv published in(发表于) 2013/12/30 4:36:37 Edit(编辑)
SQL,Server,CLR全功略之二---CLR存储过程_mssql学习_编程技术

SQL,Server,CLR全功略之二---CLR存储过程_mssql学习_编程技术

SQL Server CLR全功略之二---CLR存储过程_mssql学习_编程技术-你的首页-uuhomepage.com
从这一节开始呢,我们就要开始CLR的编程之旅了。在这之前,我先把本节中需要了解的两个新类SqlDataRecord和SqlMetaData,及五个新方法SqlContext.Pipe.SendResultsStart,SqlContext.Pipe.SendResultsRow,SqlContext.Pipe.SendResultsEnd,SqlContext.Pipe.Send和SqlContext.Pipe.ExecuteAndSend进行一下必要的说明,方便大家阅读后续的代码。
首先SqlDataRecord和SqlMetaData是数据集合和原数据的意思。可以简单的把SqlDataRecord理解成DataTable,把SqlMetaData理解成DataColumn。我们再向SqlDataRecord里面填充数据之前要先执行SqlContext.Pipe.SendResultStart()方法,告诉数据库下面开始填充数据,使用SqlContext.Pipe.SendResultRow方法来填充数据,填充结束后使用SqlContext.Pipe.SendResultEnd方法来结束填充。这些都是基本流程,没什么好解释的,只要照着去做就可以了。
SqlContext.Pipe.Send是向客户端发送一条结果,SqlContext.Pipe.ExecuteAndSend是执行一条语句。
下面我将用几个实际的简单例子来说明如何使用这几个方法。
1.使用SqlContext.Pipe.Send构建无参无返回值的存储过程
[Microsoft.SqlServer.Server.SqlProcedure]
public static void USP_SayHello()
{
SqlContext.Pipe.Send("USP:Hello TJVictor!");
}
2.使用SqlContext.Pipe.Send构建带参无返回值的存储过程
[Microsoft.SqlServer.Server.SqlProcedure]
public static void USP_SayHelloByParameter(SqlString msg)
{
SqlContext.Pipe.Send(msg.ToString());
}
3.使用SqlContext.Pipe.Send构建带参有返回值的存储过程
[Microsoft.SqlServer.Server.SqlProcedure]
public static SqlInt32 USP_SayHelloByReturn(SqlString msg)
{
return msg.ToString().Length;
}
4.使用SqlCommand来执行语句,注意这里使用了SQL Server自带的pubs数据库
[Microsoft.SqlServer.Server.SqlProcedure]
public static void USP_ExecuteBySqlCommand(SqlString stor_id, SqlString stor_name)
{
//由于程序是在SQL Server内执行,所以连接字符串写成"context connection=true"即可
using (SqlConnection con = new SqlConnection("context connection=true"))
{
con.Open();
SqlCommand com = new SqlCommand(
string.Format("insert into stores values('{0}','{1}')", stor_id, stor_name), con);
com.ExecuteNonQuery();
}
}
5.使用ExecuteAndSend来执行语句,注意这里使用了SQL Server自带的pubs数据库
[Microsoft.SqlServer.Server.SqlProcedure]
public static void USP_ExecuteByExecuteAndSend(SqlString stor_id, SqlString stor_name)
{
//由于程序是在SQL Server内执行,所以连接字符串写成"context connection=true"即可
using (SqlConnection con = new SqlConnection("context connection=true"))
{
con.Open();
SqlCommand com = new SqlCommand(
string.Format("insert into stores values('{0}','{1}')", stor_id, stor_name), con);
SqlContext.Pipe.ExecuteAndSend(com);
}
}
4和5的执行结果一样,但是在CLR中推荐使用方式5,这是将结果返回到客户端的最高效方法,因为数据不必复制到托管内存即传输到网络缓冲区。
6.使用PipeSend来发送单条记录
[Microsoft.SqlServer.Server.SqlProcedure]
public static void UPS_PipeSendSqlDataRecord()
{
//像构造Table一样来构造SqlDataRecord,其中SqlMetaData类似DataColumn
SqlDataRecord dataRecord = new SqlDataRecord(new SqlMetaData[] {
new SqlMetaData("Col1", SqlDbType.NVarChar,100),
new SqlMetaData("Col2", SqlDbType.Int)
});
for (int count = 1; count < 5; count++)
{
//SqlDataRecord.SetString类似DataRow的功能,像Table中填充值
dataRecord.SetString(0, count.ToString());
dataRecord.SetInt32(1, count);
//通过Send来发送
SqlContext.Pipe.Send(dataRecord);
}
}
7.使用PipeSendResult来发送结果集
[Microsoft.SqlServer.Server.SqlProcedure]
public static void UPS_PipeSendResultSqlDataRecord()
{
//像构造Table一样来构造SqlDataRecord,其中SqlMetaData类似DataColumn
SqlDataRecord dataRecord = new SqlDataRecord(new SqlMetaData[] {
new SqlMetaData("Col1", SqlDbType.NVarChar,100),
new SqlMetaData("Col2", SqlDbType.Int)
});
//开始填充
SqlContext.Pipe.SendResultsStart(dataRecord);
for (int count = 0; count < 5; count++)
{
//SqlDataRecord.SetString类似DataRow的功能,像Table中填充值
dataRecord.SetString(0, count.ToString());
dataRecord.SetInt32(1, count);
//通过SendResultsRow把数据填充到Table,相关于Table.Rows.Add(DataRow);
SqlContext.Pipe.SendResultsRow(dataRecord);
}
//填充结束,返回结果集
SqlContext.Pipe.SendResultsEnd();
}
其中6与7的不同在于:6返回5个结果集,其中每个结果集只有一条数据。7返回一个结果集,里面有5条数据。
最后说一下CLR存储过程的部署:
Create proc 存储过程名 as EXTERNAL NAME 数据库中Assembly名称.程序集中Assembly名称.程序方法名。
注意,如果你的程序中有命名空间的话,要这样写:
Create proc 存储过程名 as EXTERNAL NAME 数据库中Assembly名称.“程序命名空间.程序集中Assembly名称”.程序方法名。
下面的SQL是创建CLR程序集和CLR存储过程的SQL语句,假设我们编译好的dll位于C:\CLRDemo.dll,我们的程序没有命名空间(默认情况下,新建的SQL工程都没有命名空间,请注意)
use pubs
go
create assembly CLRDemoAssemly
from 'c:\CLRDemo.dll'
go
create proc USP_SayHello as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.USP_SayHello
go
create proc USP_SayHelloByParameter (@Msg nvarchar(128)) as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.USP_SayHelloByParameter
go
create proc USP_SayHelloByReturn (@Msg nvarchar(128)) as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.USP_SayHelloByReturn
go
create proc USP_ExecuteBySqlCommand (@Id nvarchar(4),@Name nvarchar(32)) as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.USP_ExecuteBySqlCommand
go
create proc USP_ExecuteByExecuteAndSend (@Id nvarchar(4),@Name nvarchar(32)) as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.USP_ExecuteByExecuteAndSend
go
create proc UPS_PipeSendSqlDataRecord as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.UPS_PipeSendSqlDataRecord
go
create proc UPS_PipeSendResultSqlDataRecord as EXTERNAL NAME CLRDemoAssemly.StoredProcedures.UPS_PipeSendResultSqlDataRecord
go
调用方式:
exec USP_SayHello
go
exec USP_SayHelloByParameter 'Hello,TJVictor again'
go
declare @Result int
exec @Result=USP_SayHelloByReturn 'Hello,TJVictor again'
select @Result
go
exec USP_ExecuteBySqlCommand '1234','Test USP_ExecuteBySqlCommand'
go
exec USP_ExecuteByExecuteAndSend '5678','Test USP_ExecuteByExecuteAndSend'
go
exec UPS_PipeSendSqlDataRecord
go
exec UPS_PipeSendResultSqlDataRecord
go
删除方式:注意删除Assembly时,一定要先把引用此Assembly的所有东西删除。
drop proc USP_SayHello
drop proc USP_SayHelloByParameter
drop proc USP_SayHelloByReturn
drop proc USP_ExecuteBySqlCommand
drop proc USP_ExecuteByExecuteAndSend
drop proc UPS_PipeSendSqlDataRecord
drop proc UPS_PipeSendResultSqlDataRecord
go
drop assembly CLRDemoAssemly
下面附带完整程序源代码




添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.