一、VS2005创建项目SqlServerCLRCallNet,新建CLRFunction.cs
View Code
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace SqlServerCLRCallNet
{
public class CLRFunction
{
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void GetResultSetString(SqlString SNO)//不同点
{
SqlCommand cmd;
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
cmd = new SqlCommand("SELECT id,name,modifieddate FROM ChangeHistory WHERE name = @SNO ", conn);
SqlParameter para = new SqlParameter("@SNO", System.Data.SqlDbType.VarChar);//不同点
para.Value = SNO;
cmd.Parameters.Add(para);
SqlContext.Pipe.ExecuteAndSend(cmd);
}
}
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void GetResultSetInt(int SNO)//不同点
{
SqlCommand cmd;
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
cmd = new SqlCommand("SELECT id,name,modifieddate FROM ChangeHistory WHERE id = @SNO ", conn);
SqlParameter para = new SqlParameter("@SNO", System.Data.SqlDbType.Int);//不同点
para.Value = SNO;
cmd.Parameters.Add(para);
SqlContext.Pipe.ExecuteAndSend(cmd);
}
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace SqlServerCLRCallNet
{
public class CLRFunction
{
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void GetResultSetString(SqlString SNO)//不同点
{
SqlCommand cmd;
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
cmd = new SqlCommand("SELECT id,name,modifieddate FROM ChangeHistory WHERE name = @SNO ", conn);
SqlParameter para = new SqlParameter("@SNO", System.Data.SqlDbType.VarChar);//不同点
para.Value = SNO;
cmd.Parameters.Add(para);
SqlContext.Pipe.ExecuteAndSend(cmd);
}
}
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void GetResultSetInt(int SNO)//不同点
{
SqlCommand cmd;
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
cmd = new SqlCommand("SELECT id,name,modifieddate FROM ChangeHistory WHERE id = @SNO ", conn);
SqlParameter para = new SqlParameter("@SNO", System.Data.SqlDbType.Int);//不同点
para.Value = SNO;
cmd.Parameters.Add(para);
SqlContext.Pipe.ExecuteAndSend(cmd);
}
}
}
}
二、将DLL放到服务器C盘根目录
三、执行以下SQL语句
CREATE ASSEMBLY InsertCurrency FROM 'c:\SqlServerCLRCallNet.dll'
WITH permission_set = Safe;
GO
--drop PROCEDURE InsertCurrency_Int
CREATE PROCEDURE dbo.InsertCurrency_Int(@SNO int)
AS EXTERNAL NAME InsertCurrency.[SqlServerCLRCallNet.CLRFunction].GetResultSetInt;
GO
EXEC InsertCurrency_Int '1'
go
--drop PROCEDURE InsertCurrency_String
CREATE PROCEDURE dbo.InsertCurrency_String(@SNO nvarchar(200))
AS EXTERNAL NAME InsertCurrency.[SqlServerCLRCallNet.CLRFunction].GetResultSetString;
go
EXEC InsertCurrency_String 'landor'
WITH permission_set = Safe;
GO
--drop PROCEDURE InsertCurrency_Int
CREATE PROCEDURE dbo.InsertCurrency_Int(@SNO int)
AS EXTERNAL NAME InsertCurrency.[SqlServerCLRCallNet.CLRFunction].GetResultSetInt;
GO
EXEC InsertCurrency_Int '1'
go
--drop PROCEDURE InsertCurrency_String
CREATE PROCEDURE dbo.InsertCurrency_String(@SNO nvarchar(200))
AS EXTERNAL NAME InsertCurrency.[SqlServerCLRCallNet.CLRFunction].GetResultSetString;
go
EXEC InsertCurrency_String 'landor'
id | name | modifieddate |
1 | Landor | 2012-9-13 9:26 |
為什麼要用.net開發store procedure?
T-SQL stored procedure 和 SQL CLR stored procedure 其實不一定是在效能上的選擇,而是開發難易度的選擇,例如若要格式化字串,T-SQL 根本做不到,一定要用 SQL CLR 的 UDF,若是要存取檔案/AD/其他外部資源時,T-SQL 要實作也很困難,這時除了 SQL CLR 外,別無它法 .
另一種因素是簡化設計的複雜度,例如累進式迴圈(for)/游標(Cursor)或是其他 T-SQL 無法做到或必須以相當複雜的 T-SQL 才能實作出來的程式,就可以改用 SQL CLR 來處理 .http://social.msdn.microsoft.com/Forums/zh-TW/240/thread/c80255a7-5cf2-4a54-b9c1-995098c18a0b