Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MS SQL Server 2008中选择CLR存储过程中的行?_Sql_Sql Server_Sqlclr_Clrstoredprocedure - Fatal编程技术网

如何在MS SQL Server 2008中选择CLR存储过程中的行?

如何在MS SQL Server 2008中选择CLR存储过程中的行?,sql,sql-server,sqlclr,clrstoredprocedure,Sql,Sql Server,Sqlclr,Clrstoredprocedure,下面是我在internet上找到的一个示例,我编写了以下C代码,用于在MS SQL Server 2008中实现CLR存储过程: public class Class1 { [Microsoft.SqlServer.Server.SqlProcedure] public static void CountStringLength(string inputString) { SqlContext.Pipe.Send(inputString.Length.To

下面是我在internet上找到的一个示例,我编写了以下C代码,用于在MS SQL Server 2008中实现CLR存储过程:

public class Class1
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void CountStringLength(string inputString)
    {
        SqlContext.Pipe.Send(inputString.Length.ToString());            
    }
}
此过程将字符串作为参数,并输出字符串中的字符数

我一直在编写一些从SQL检索数据的代码;检索数据的方式没有什么特别之处:它与SQL server建立连接,选择数据,然后关闭连接。我想让这段代码在SQL server上的存储过程或触发器中运行

我想我可以让代码与现有的SQL代码完全一样运行:建立到SQL server的连接,选择数据,然后关闭连接。但是,一旦代码在SQL server本身上运行,这就没有意义了!为什么我要在SQL server上运行的代码使服务器连接到自身


对于我正在尝试做的事情,是否有最佳实践?我可以使用与执行存储过程相同的连接来选择代码中的行吗?

我找到了下面解释的答案:

CLR过程运行的连接称为上下文连接,其使用方式如下:

using(SqlConnection connection = new SqlConnection("context connection=true")) 
{
    connection.Open();
    // Use the connection
}
我想知道你为什么要打开连接?我认为连接已经打开,因为它正在执行正在运行的过程

也密切相关: