通过实体框架将整数数组传递给T-SQL存储的proc [英] Passing array of ints to T-SQL stored proc via entity framework

查看:51
本文介绍了通过实体框架将整数数组传递给T-SQL存储的proc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多文章,并认为我理解这些概念,但是我的一小部分int无法从C#/ EF模块传递到SQL Server存储的proc。希望其他人可以发现问题所在。

I've read many posts and think I understand the concepts, but my small array of ints fails to pass from a C#/EF module to a SQL Server stored proc. Hoping other eyes can spot the problem.

我正在使用EF6、4.5 .Net Framework,SQL Server 2014

I'm using EF6, 4.5 .Net Framework, SQL Server 2014

在数据库中,我创建了以下类型/过程:

In the database I've created these types/procs:

CREATE TYPE [dbo].[IntsTTV] AS TABLE(
  [Id] [int] NOT NULL
)

请注意一个表名为'Person'的列'Id'(int)和'LastName'(nvarchar)并具有数据。

Note that a table named 'Person' exists with columns 'Id' (int) and 'LastName' (nvarchar), and has data.

CREATE PROCEDURE [dbo].[GetUsers]
@UserIds dbo.IntsTTV READONLY
AS
BEGIN
  SELECT p.LastName
  FROM [dbo].[Person] p
  INNER JOIN @UserIds ids On p.Id = ids.Id;
END

// C#代码

SqlMetaData[] columns = new SqlMetaData[1];
columns[0] = new SqlMetaData("Id", SqlDbType.Int);

SqlDataRecord row = new SqlDataRecord(columns);
row.SetInt32(0, 1);  // Id of '1' is valid for the Person table

SqlDataRecord[] table = new SqlDataRecord[1];
table[0] = row;

SqlParameter parameter = new SqlParameter();
parameter.SqlDbType = SqlDbType.Structured;
parameter.ParameterName = "@UserIds";
parameter.TypeName = "dbo.IntsTTV";
parameter.Direction = ParameterDirection.Input;
parameter.Value = table;

SqlParameter[] parameters = new SqlParameter[1];
parameters[0] = parameter;


var res = _db.Database.SqlQuery<string>("GetUsers", parameters).ToList();






代码确实成功调用了proc,如果我对proc进行了硬编码,以简单地返回LastName的选择,然后C#代码确实收到了该信息。这告诉我在起作用

如果我从其他T-SQL代码中调用proc,并传入一个预先准备好的表值参数(IntsTTV),则它起作用。

If I call the proc from other T-SQL code, passing in a prepared table-valued parameter (IntsTTV) of ints, it works.

在proc中,如果我选择传递的参数表的行数,则从C#代码调用时得到零,但从T-SQL代码调用时得到正确的计数。

In the proc, if I select count of rows of the passed parameter table I get zero when calling from the C# code, but I get a correct count when calling from T-SQL code.

请问我缺少什么?

推荐答案

这就是我所说的具有表值参数的存储过程。主要区别在于我使用了 DataTable 参数。

This is how I call stored procedure with table valued parameter. The main difference being that I use a DataTable parameter.

我记得参数名称绑定存在问题,但我并没有确切说明它们的含义。这说明了我对过程调用的语法所做的更改。我知道这应该工作。

I remember having issues with parameter name bindings, but I don't remeber exactly what they were. This explains the change I made in the syntax of the procedure call. I know this one should be working.

var dataTable = new DataTable();
dataTable.TableName = "dbo.IntsTTV";
dataTable.Columns.Add("Id", typeof(int));
dataTable.Rows.Add(1); // Id of '1' is valid for the Person table

SqlParameter parameter = new SqlParameter("UserIds", SqlDbType.Structured);
parameter.TypeName = dataTable.TableName;
parameter.Value = dataTable;

var res = _db.Database.SqlQuery<string>("EXEC GetUsers @UserIds", parameter).ToList();

这篇关于通过实体框架将整数数组传递给T-SQL存储的proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆