简体   繁体   English

将数据集作为参数从C#传递到T-SQL存储过程

[英]Passing a DataSet in to a T-SQL stored procedure as a parameter from C#

I am attempting to pass a populated dataset from C# to a SQL Server stored procedure parameter, but I'm having trouble. 我试图将填充的数据集从C#传递到SQL Server存储过程参数,但是遇到了麻烦。

Here is my C# code: 这是我的C#代码:

if (tblNoMatch.Rows.Count > 0)
{
    pge.dbconn.ExecuteNonQuery("dbo.MySproc",
                               new SqlParameter("@MyTableParam", tblNoMatch),
                               new SqlParameter("@IsProcessed", "2")
                              );

    pge.dbconn.ShutDown();
}

where tblNoMatch is type DataSet , and populated with a column ID . 其中tblNoMatchDataSet类型,并填充有列ID The dataset variable is filled with 40 rows of ID values. 数据集变量填充了40行ID值。

Here is my SQL end: 这是我的SQL结尾:

IF TYPE_ID(N'MyTable') IS NULL
    CREATE TYPE dbo.MyTable AS TABLE(BatchID INT);
GO

ALTER PROCEDURE [dbo].[MySproc] 
    @MyTableParam AS dbo.MyTable READONLY,
    @IsProcessed [smallint] = 0
AS
    IF (SELECT COUNT(*) FROM @MyTableParam) > 0
        --Process Records from Batch 
    BEGIN
        UPDATE [dbo].[MyTable]
        SET [IsProcessed] = ISNULL(@IsProcessed, IsProcessed)
        WHERE [ID] IN (SELECT ID FROM @MyTableParam)
    END

When I sniff the throughput with SQL Server Profiler tool, I don't even see @MyTableParam getting passed, and on the SQL Server end, it's getting ignored entirely. 当我使用SQL Server Profiler工具嗅探吞吐量时,我什至看不到@MyTableParam通过,并且在SQL Server端,它被完全忽略了。

What am I doing wrong here? 我在这里做错了什么?

Many thanks in advance! 提前谢谢了!

EDIT: 编辑:

I made this alteration as I know the parameter needs to be of type Structured , but this unfortunately did not do the trick for me either: 我进行了更改,因为我知道参数的类型必须为Structured ,但是不幸的是,这对我也没有帮助:

if (tblNoMatch.Rows.Count > 0)
{
    var tblParameter = new SqlParameter
                           {
                                ParameterName = "@MyTableParam",
                                SqlDbType = SqlDbType.Structured,
                                Value = tblNoMatch
                           };

    pge.dbconn.ExecuteNonQuery("dbo.MySproc", tblParameter,
                               new SqlParameter("@IsProcessed", "2"));

    pge.dbconn.ShutDown();
}

What else am I missing here? 我在这里还想念什么?

尝试按以下方式指定TypeName ...

tblParameter.TypeName = "dbo.MyTable";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM