我正在尝试使用 petapoco 将对象列表传递给存储过程,该存储过程返回一条消息,指出没有托管 native 类型可以将我的对象类型映射到,并且我不完全理解原因以及如何纠正它。
我正在使用 Azure 作为我的数据库,其中创建了表值参数
CREATE TYPE TransactionModel AS TABLE (
Id int,
UserName nvarchar(128),
RecordId int,
OrderId int,
Usd decimal(18,2),
Btc decimal(18,8),
BtcUsd decimal(18,2),
Fee decimal(18,2),
Type int,
DateTime datetime,
Updated datetime
)
我有一个与名为“交易”的表相匹配的表
我的存储过程
ALTER PROCEDURE sp_SaveTransactions
-- Add the parameters for the stored procedure here
@Transactions TransactionModel READONLY,
@UserName nvarchar(128)
AS
BEGIN
SET NOCOUNT ON;
BEGIN
INSERT INTO Transactions(UserName, RecordId, OrderId, Usd, Btc, BtcUsd, Fee, Type, DateTime, Updated)
SELECT UserName, RecordId, OrderId, Usd, Btc, BtcUsd, Fee, Type, DateTime, Updated
FROM @Transactions
END
END
GO
最后是我的 C# 方法
public static void SpSave<T>(T obj, string userName)
{
try
{
var ctx = new PetaPoco.Database("MyDatabase");
if (obj.GetType().IsGenericType && obj is IEnumerable)
{
var list = (IList)obj;
var l = ctx.Query<List<TransactionModel>>(";EXEC sp_SaveTransactions @0", list, userName);
}
}
catch (Exception ex)
{
}
}
最佳答案
MSDN 中 SQL Server 支持的表变量类型为数据表、DbDataReader 或 SqlDataRecord。 https://msdn.microsoft.com/en-us/library/bb675163%28v=vs.110%29.aspx
您应该能够在使用具有这些数据类型之一的 PetaPoco 时简单地传递 SqlParameter。
var param = new SqlParameter();
param.DbType = SqlDbType.Structured;
param.ParameterName = "@Transaction";
param.SqlValue = table; // Datatable or one of the acceptable SqlClient types above
ctx.Query<List<TransactionModel>>(";exec sp_SaveTransactions @0", param);
关于c# - PetaPoco,将列表传递给存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30281501/