将表值参数传递给存储过程

时间:2015-09-08 13:26:25

标签: c# sql entity-framework sql-server-2008

我一直在搜索和更改代码几个小时,但找不到我面临的问题,我的更改似乎没有帮助。

TVP的Sql类型:

CREATE TYPE [dbo].[MessagesIds] AS TABLE(
[msgId] [nvarchar](500) NULL)

步骤:

ALTER Procedure [dbo].[DeleteMessagesByIds] 
@SocialMediaType nvarchar(50),
@MsgIds dbo.MessagesIds READONLY
As 
Begin
    DECLARE @TypeId int
    SELECT @TypeId = ID FROM SocialMediaType WHERE Title = @SocialMediaType

    Set NOCOUNT ON;
    DELETE FROM [Message]
    Where ID In 
    (Select B.ID From [Message] B 
    Inner Join @MsgIds msgsToDeleteIds On B.id_str = msgsToDeleteIds.msgId
    Where B.SocialMediaTypeID = @TypeId);
END

C#程序调用代码:

var idsTable = new DataTable();
idsTable.Columns.Add("msgId", typeof(string));
foreach (var id in ids) { //ids = List<string>
  idsTable.Rows.Add(id);
}
var socialMediaTypeParameter = new SqlParameter("@SocialMediaType", SqlDbType.NVarChar);
socialMediaTypeParameter.Value = socialMediaType;
var idsParameter = new SqlParameter("@MsgIds", SqlDbType.Structured);
idsParameter.Value = idsTable;
idsParameter.TypeName = "dbo.MessagesIds";
context.Database.Log = s => { System.Diagnostics.Debug.WriteLine(s); }; 
context.Database.ExecuteSqlCommand("exec dbo.DeleteMessagesByIds @SocialMediaType @MsgIds", socialMediaTypeParameter, idsParameter);

执行sql命令时收到错误:Incorrect syntax near '@MsgIds'。在调试控制台中,我看到生成的sql @MsgIds为空,但为什么?

exec dbo.DeleteMessagesByIds @SocialMediaType @MsgIds
-- @SocialMediaType: 'Facebook' (Type = String, IsNullable = false, Size = 8)
-- @MsgIds: '' (Type = Object, IsNullable = false)
-- Executing at 2015.09.08. 16:05:52 +03:00

0 个答案:

没有答案
相关问题