我正在审查一些顾问签到的代码并注意到他们正在使用SQLCLR.我没有任何经验,所以我想我会研究它的内容.我注意到他们用过
Dim cn As New SqlConnection("server=LOCALHOST;integrated security=yes;database=" & sDb)
Run Code Online (Sandbox Code Playgroud)
代替
DIM conn As New SqlConnection("context connection=true")
Run Code Online (Sandbox Code Playgroud)
我想知道它是什么区别,因为它是第一个localhost?
我有一个流程来分析来自一个系统的审计数据,以构建另一个系统的报告数据.有一个管理程序循环每天进行分析,并在当前迭代日调用实体特定程序.有些实体需要不到一秒的时间来处理,而其他实体可能需要几分钟.像在t-sql中一样串行运行,16核服务器上的cpu利用率从未达到8%以上.每个实体特定程序不依赖于其他程序,只是当天的所有实体在第二天开始之前完成.
我的想法是拥有一个CLR管理程序,并在他们自己的线程上运行当天运行时间较长的程序,然后一旦快速完成,Thread.Join()长时间运行的线程等待所有实体完成前一天继续下一个.
下面是我的尝试作为最简单的事情,只适用于一个工作线程,并在该线程上调用Start不会导致调用静态方法.我在HelloWorld方法中设置了一个断点,它永远不会被击中.
我在控制台应用程序中尝试过非常类似的东西,并且它在AsyncHelloWorld开头的注释行中的同一线程上调用它.SQL CLR过程中的线程有什么不同吗?
using System.Threading;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[SqlProcedure]
public static void AsyncHelloWorld()
{
// HelloWorld(SqlContext.Pipe);
var worker = new Thread(HelloWorld);
worker.Start(SqlContext.Pipe);
worker.Join();
}
public static void HelloWorld(object o)
{
var pipe = o as SqlPipe;
if (pipe != null)
pipe.Send("Hello World!");
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个自动SQLCLR部署工具,我正在使用反射来发现必须声明的过程和函数.所以我使用这样的代码来构建部署汇编方法所需的T-SQL:
...
if (p.ParameterType == typeof(string))
{
sql = "nvarchar(4000)";
}
...
Run Code Online (Sandbox Code Playgroud)
但是这个方法包含一个在C#中声明的参数,out SqlBytes bytes而ParameterType是SqlBytes&.我不能typeof(SqlBytes&)用于比较,因为语法无效.所以我有点疑惑究竟是什么SqlBytes&类型,如果有办法为它生成typeof.我知道我可以求助于类型名称(即.字符串)比较,这不是我的问题,我更好奇这是什么类型的&,似乎是一个C++引用类型,但我估计在+ 10年的和.Net一起工作我从来没有注意到它们.

嗨,我正在使用C#制作CLR存储过程,我正在通过示例学习.
以下是我现在正在尝试的内容
public static void GetProductsByPrice(int price)
{
SqlConnection connection = new SqlConnection("context connection=true");
connection.Open();
string commandText = "SELECT * FROM Products WHERE PRICE < " + price.ToString();
SqlCommand command = new SqlCommand(commandText, connection);
SqlDataReader reader = command.ExecuteReader();
// Create the record and specify the metadata for the columns.
SqlDataRecord record = new SqlDataRecord(
new SqlMetaData("col1", SqlDbType.NVarChar, 100),
new SqlMetaData("col2", SqlDbType.NVarChar, 100));
// Mark the begining of the result-set.
SqlContext.Pipe.SendResultsStart(record);
// Send 10 rows back to the client.
while (reader.Read()) …Run Code Online (Sandbox Code Playgroud) 我实现了 CLR 存储过程的“HelloWorld”示例,它工作正常。
然后我更改了代码,重新编译并重新运行它,得到了旧的结果,而不是新的结果。
示例代码更改(所有其他代码都在上面的链接中):
前:
SqlContext.Pipe.Send("Hello world!\n");
Run Code Online (Sandbox Code Playgroud)
后:
SqlContext.Pipe.Send("Hello world new version!\n");
Run Code Online (Sandbox Code Playgroud)
除了回收 SQL 来加载新程序集之外,还有什么不足之处吗?
我正在将SQL Server 2008迁移到2012,并在为CLR例程创建一些必要的程序集时遇到了挑战.例程依赖于stdole.dll,但我无法创建此程序集.我的代码如下:
ALTER DATABASE main SET TRUSTWORTHY ON;
create assembly [stdole]
from
'C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\stdole.dll'
WITH PERMISSION_SET = unsafe
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Warning: The Microsoft .NET Framework assembly 'stdole, version=7.0.3300.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.' you are registering is not fully tested in the SQL Server hosted environment and is not supported. In the future, if you upgrade or service this assembly or the .NET Framework, your CLR integration routine may stop working. Please refer SQL Server Books Online for …Run Code Online (Sandbox Code Playgroud) 我们在ETL过程中使用CLR函数来集中特定的数据转换和数据检查逻辑.这些功能是相当基本的,不需要数据访问,并且具有允许并行性的确定性.
例如:
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, SystemDataAccess = SystemDataAccessKind.None, IsPrecise = true)]
public static bool check_smallint(string input)
{
string teststring;
try
{
teststring = input.Trim(' ').Replace('-', '0');
if (teststring.Length == 0)
{
teststring = "0";
}
Convert.ToInt16(teststring);
}
catch (NullReferenceException)
{
return true;
}
catch (FormatException)
{
return false;
}
catch (OverflowException)
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
除性能外,这种方法很好.查询的速度已大大减慢,这会导致处理大型数据集(数百万行甚至更多)时出现问题.
到目前为止,我们没有找到真正了解SQL CLR架构的人,但我们收到的一个建议是,它可能是由为每个函数调用创建新连接或分配内存的开销引起的.所以解决方案可能是连接/内存池.
请不要提出不同的解决方案,我们已经在考虑它们,比如内联sql,或者完全不同的方法.在许多情况下,标准的sql函数没有选项,因为没有错误提升.
PS.我们正在使用SQL 2008R2.
我试图了解启用CLR是在数据库级别还是服务器级别/实例级别启用它.在线书籍根本不清楚,如果我使用这个命令启用CLR会发生什么,它是否会在所有服务器上启用?
sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO
Run Code Online (Sandbox Code Playgroud) 一般问题
迁移到SQL Server 2014之后,执行之前为SQL Server 2008 R2中的每次执行产生不同结果的CLR标量函数,现在为每个后续调用生成相同的结果.
测试/插图
DECLARE @i int = 0;
WHILE @i < 10
BEGIN
print dbo.getEligLoadTXID();
--> Simulate delay as this code never gets called more than once every few seconds in our environment
WAITFOR DELAY '00:00:00.1';
SET @i += 1;
END;
Run Code Online (Sandbox Code Playgroud)
我们目前正在将环境从SQL Server 2008 R2升级到SQL Server 2014.我们遇到的一个问题是特定于CLR标量函数的执行.功能相对简单; 它正在根据刻度数生成一个内部ID(让我们不要争论这个函数的需要/目的).
在SQL Server 2008 R2环境中,在循环中执行函数会返回预期结果(每次执行都会产生新值).但是,在我们新的SQL Server 2014环境中进行测试时,相同的代码会为循环中的每次执行返回SAME值.
SQL Server 2008 R2中的结果:
20161213502167209995
20161213502168210095
20161213502169210195
20161213502170210295
20161213502171210395
20161213502172210495
20161213502173210595
20161213502174210695
20161213502175210795
20161213502176210895
Run Code Online (Sandbox Code Playgroud)
SQL Server 2014中的结果:
20161213502199924787
20161213502199924787
20161213502199924787
20161213502199924787 …Run Code Online (Sandbox Code Playgroud) 我有一个桌面应用程序,应该在任何表更改时收到通知。因此,我只找到了两个非常适合我的情况的解决方案:SqlDependency和SQLCLR。(我想知道 .NET 堆栈中是否有更好的)我已经构建了两个结构并使它们工作。我只能比较从 SQL Server 到客户端的 s̲i̲n̲gl̲e̲ 响应的持续时间。
持续时间:从 100 毫秒到 4 秒
持续时间:从 10ms 到 150ms
我希望这个结构能够处理高速率通知*,我已经阅读了一些 SO 和博客文章(例如:here),并且我的同事也警告说,在大量请求时 SqlDependency 可能会出错。在这里,MS 提供了一些我没有得到的东西,这可能是我问题的另一种解决方案。
*:不是所有时间,而是一个季节;在 1-2 个服务器上每秒 50-200 个请求。
基于高通知率和性能,我应该继续使用这两个中的哪一个,还是有其他选择?