希望在明天

如果,你没有耐心等待成功的到来,那么,你将用一生的耐心去面对失败。

CLR Stored Procedure

1、写一个类,方法必须是公有静态的,方法必须添加一个属性[Microsoft.SqlServer.Server.SqlProcedure],编译成一个程序集。

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;

namespace CLRProcedure
{
    
public class StoredProcedures
    
{
        [Microsoft.SqlServer.Server.SqlProcedure]
        
public static void HelloWorld()
        
{
            SqlContext.Pipe.Send(
"Hello world! It's now " + System.DateTime.Now.ToString() + "\n");
            
using (SqlConnection connection = new SqlConnection("context connection=true"))
            
{
                connection.Open();
                SqlCommand command 
= new SqlCommand("SELECT ProductNumber FROM ProductMaster", connection);
                command.CommandType 
= System.Data.CommandType.StoredProcedure;
                SqlDataReader reader 
= command.ExecuteReader();
                SqlContext.Pipe.Send(reader);
            }

        }

    }

}

2、在SQL 2005中,创建程序集

CREATE ASSEMBLY Test FROM '程序集路径'

3、创建存储过程
CREATE PROCEDURE HelloWorld
AS EXTERNAL NAME Test.StoredProcedures.HelloWorld

创建成功了!

可以在查询分析器里执行:

EXEC HelloWorld

posted on 2007-03-28 16:51  蒜头  阅读(368)  评论(0编辑  收藏  举报

导航