简体   繁体   中英

Call stored procedure with user defined parameter

Is it possible to call Procedure A with parameter B which is User defined table type?

CREATE PROCEDURE A
(
    @MyTable B READONLY
)
AS
BEGIN
   SELECT * FROM C;
END

CREATE TYPE B AS TABLE 
(
   ID int, 
   PRIMARY KEY (ID)
)

EF-6 is version I use.

To execute stored procedure you can use SqlQuery<T> and to pass table-valued parameter you can use DataTable .

var dt = new DataTable();
dt.Columns.Add("ID");
dt.Rows.Add(new object[]{ 1 });
dt.Rows.Add(new object[]{ 2 });
dt.Rows.Add(new object[]{ 3 });

var sqlParam = new SqlParameter("MyTable", dt){ TypeName = "B" };
var query = db.Database.SqlQuery<CModel>("A @MyTable", sqlParam);
var cmodels = query.ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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