2

对于那些感兴趣的人,我现在修改了 SubSonic 2.x 代码以识别和支持 DataTable 参数类型。

您可以在此处阅读有关 SQL Server 2008 功能的更多信息:http: //download.microsoft.com/download/4/9/0/4906f81b-eb1a-49c3-bb05-ff3bcbb5d5ae/SQL%20SERVER%202008-RDBMS/T-SQL %20增强功能%20with%20SQL%20Server%202008%20-%20Praveen%20Srivatsav.pdf

现在,此增强功能将允许您创建一个部分 StoredProcedures.cs 类,该类具有一个覆盖存储过程包装器方法的方法。

关于良好形式的一点:我的 DAL 没有直接的表访问权限,我的数据库只有该用户对我的存储过程的执行权限。因此,SubSonic 仅生成 AllStructs 和 StoredProcedures 类。

SPROC:

    ALTER PROCEDURE [dbo].[testInsertToTestTVP]
    @UserDetails TestTVP READONLY,
    @Result INT OUT
AS
BEGIN
    SET NOCOUNT ON;

    SET @Result = -1

    --SET IDENTITY_INSERT [dbo].[tbl_TestTVP] ON

    INSERT INTO [dbo].[tbl_TestTVP]
            ( [GroupInsertID], [FirstName], [LastName] )
    SELECT [GroupInsertID], [FirstName], [LastName]
    FROM @UserDetails

    IF @@ROWCOUNT > 0
        BEGIN
            SET @Result = 1
            SELECT @Result
            RETURN @Result
        END
    --SET IDENTITY_INSERT [dbo].[tbl_TestTVP] OFF

END

TVP:

    CREATE TYPE [dbo].[TestTVP] AS TABLE(
    [GroupInsertID] [varchar](50) NOT NULL,
    [FirstName] [varchar](50) NOT NULL,
    [LastName] [varchar](50) NOT NULL
)
GO

自动生成工具运行时,它会创建以下错误方法:

    /// <summary>
    /// Creates an object wrapper for the testInsertToTestTVP Procedure
    /// </summary>
    public static StoredProcedure TestInsertToTestTVP(string UserDetails, int? Result)
    {
        SubSonic.StoredProcedure sp = new SubSonic.StoredProcedure("testInsertToTestTVP", DataService.GetInstance("MyDAL"), "dbo");     
        sp.Command.AddParameter("@UserDetails", UserDetails, DbType.AnsiString, null, null);
        sp.Command.AddOutputParameter("@Result", DbType.Int32, 0, 10);            
        return sp;
    }

它将 UserDetails 设置为字符串类型。

由于为 SubSonic DAL 提供两个文件夹(自定义和生成)是一种很好的形式,因此我在自定义中创建了一个 StoredProcedures.cs 部分类,如下所示:

    /// <summary>
    /// Creates an object wrapper for the testInsertToTestTVP Procedure
    /// </summary>
    public static StoredProcedure TestInsertToTestTVP(DataTable dt, int? Result)
    {
        SubSonic.StoredProcedure sp = new SubSonic.StoredProcedure("testInsertToTestTVP", 
                                                                    DataService.GetInstance("MyDAL"), 
                                                                    "dbo");

        // TODO: Modify the SubSonic code base in sp.Command.AddParameter to accept
        //       a parameter type of System.Data.SqlDbType.Structured, as it currently only accepts
        //       System.Data.DbType.
        //sp.Command.AddParameter("@UserDetails", dt, System.Data.SqlDbType.Structured null, null);

        sp.Command.AddParameter("@UserDetails", dt, SqlDbType.Structured);
        sp.Command.AddOutputParameter("@Result", DbType.Int32, 0, 10);

        return sp;
    }

如您所见,方法签名现在包含一个 DataTable,并且通过我对 SubSonic 框架的修改,现在可以完美运行。

我想知道 SubSonic 的家伙是否可以修改自动生成以识别存储过程签名中的 TVP,以避免重新编写包装器?

SubSonic 3.x 是否支持结构化数据类型?

Also, I'm sure many will be interested in using this code, so where can I upload the new code?

Thanks.

4

1 回答 1

0

I've posted a CodePlex project with the entire solution, source code and instructions.

The project can be found here.

于 2011-09-05T16:22:03.710 回答