SQL Server CLR全功略之二
从这一节开始呢,我们就要开始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
相关文档:
1、要启用全文索引功能首先需要安装full text search全文索引服务
2、启动full text search服务
3、先创建Unique索引和全文索引:CREATE FULLTEXT INDEX ON table_name
4、每个表只允许创建一个全文索引
删除全文索引 DROP FULLTEXT INDEX ON table_name
全文搜索语句,contains(),freeText()
注 ......
知: 字段A='F:\photo\Winter Leaves.jpg'
要求:分段截取每段字符[字段A不能为TEXT类型,否则报错]
解决方法:
---截取字符串A的第一个\左边的字符串
select left(A,charindex('/',A)-1)
输出结果:F:
---截取\中间的字符串
select left(stuff(A,1,charindex('/',A),''),charindex('/',stuff(A,1,c ......
USE AdventureWorks
GO
CREATE PROC spEmployee
AS
SELECT * from Humanresources.Employee
EXEC spEmployee
ALTER PROC spEmployee
AS SELECT EmployeeID from Humanresources.Employee
drop proc spEmployee
ALTER PROC spEmployee
@LastName nvarchar(50) = NULL
AS
IF @LastName IS NULL
SELECT * f ......
温故而知新,果然如此呀,第二次再翻开同样的内容果然有不同的收获,有些是第一次看的时候没有仔细理解的,还有些可能是在第一次看匆匆就跳过的,当然,可能还有部分是自己当时记住了完了又给忘记了。今天第二次看到子程序这一章节,发现了些新的内容,呵呵。在这里我就写下一些基本内容和容易忘记的,免得下次又给忘了。内 ......