英文出处:http://daron.yondem.com/PermaLink.aspx?guid=b7ee0be4-a5f3-49c3-b5f0-5ee02512290b
在外包公司做了三个月了,英文稍微有一点进步,能看懂相对简单的文章了,今天正好遇到一个关于VS CLR存储过程的问题,没有找到中文的解决方案,特翻译此文章,第一次翻译,很多不对之处请批评指正。(一些英文没有按照原文翻译,不重要的省略,尽量不影响大家理解)
打开Visual Studio2005并且建立一个新的Sql Server项目,燃火输入一下代码到你的工程文件
(Launch Visual Studio 2005 and open a new SQL Server Project. Add a new Stored Procedure to your project.)
Using cnn As New SqlClient.SqlConnection("context connection=true")
cnn.Open()
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT * FROM Table_1", cnn)
SqlContext.Pipe.ExecuteAndSend(cmd)
End Using
注意:关于数据库连接字符串,我们不需要确定属于哪一种数据库类型,CLR存储过程只存在于SQL SERVER中,应为我们不需要重新连接,使用Content Connnection 就可以了。
(Be carefull about our Conntection String. We do not define any specific connection string. Our stored procedure is already in SQL server, so we dont need to connect it again, we just use the default context connection.)
现在编译部署项目,选择菜单编译部署.
(Now Build your project and deploy it with "Build / Deploy" menu in VS2005.)
Msg 6263, Level 16, State 1, Line 4
Execution of user code in the .NET Framework is disabled. Enable "clr enabled" configuration option.
出现以上错误到开始菜单-Sql Server2005-SQL Server 2005/Configuration Tools/Surface Area Configuration,在打开的界面中选择
Surface Area Configuration for Features,然后选择CLR Integration选项激活并保存.
(This is the next error you will get. By default .NET Framwork is disabled in SQL2005. Explore "SQL Server 2005/Configuration Tools/Surface Area Configuration" in your Start menu. Select "Surface Area Configuration for Features", You will find "CLR Integration" option, activate it and save.)
现在可以运行新的存储过程了.
Done :) You can now just run your new stored procedure.
EXECUTE [dbo].[StoredProcedure1]
第一次运行可能比较慢,因为CLR编译我们的存储过程,但是下一次运行就快了.
The first run will be a little slow; because CLR will compile your stored procedure but next runs will be more effective.
2008年9月16日 16:15 于青岛软件园G3楼