Dapper完全支持存储过程

var user=conn.Query("spGetUser",new{Id=1},commandType:CommandType.StoredProcedure).SingleOrDefault();
var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", 
      dbType: DbType.Int32, 
      direction: ParameterDirection.Output);
p.Add("@c", 
      dbType: DbType.Int32, 
      direction: ParameterDirection.ReturnValue);

conn.Execute("spMagicProc", p, 
             commandType: CommandType.StoredProcedure); 

var b = p.Get("@b");
var c = p.Get("@c");  

表值参数

如果你有一个接收表值参数的存储过程,则需要传递一个DataTable,它具有与Sql Server中的表类型相同的结构。这是一个使用他的表类型和过程的定义:

CREATE TYPE [dbo].[myUDTT] AS TABLE([i1] [int] NOT NULL);
GO
CREATE PROCEDURE myProc(@data dbo.myUDTT readonly) AS
SELECT i1 FROM @data;
GO
/*
-- optionally grant permissions as needed, depending on the user you execute this with.
-- Especially the GRANT EXECUTE ON TYPE is often overlooked and can cause problems if omitted.
GRANT EXECUTE ON TYPE::[dbo].[myUDTT] TO [user];
GRANT EXECUTE ON dbo.myProc TO [user];
GO
*/

要从C#中调用该存储过程

DataTable data=new DataTable data = new DataTable();
data.Columns.Add("i1", typeof(int));
// Add two rows
data.Rows.Add(1);
data.Rows.Add(2);

var q = conn.Query("myProc", new {data}, commandType: CommandType.StoredProcedure);

 

你可能感兴趣的:(Dapper)