c# - PetaPoco,将列表传递给存储过程

标签 c# azure stored-procedures petapoco

我正在尝试使用 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/

相关文章:

c# - 从 .NET 调用基于 AXIS 的 Web 服务时,永远不会填充嵌套的复杂类型元素

c# - 处理类和 NHibernate 实体中的集合属性

mysql - 在 SQL 中插入一行并在存储过程中设置变量?

MySql 存储过程错误 [ERROR 1338 (42000) : Cursor declaration after handler declaration]

c# - "TypeError: Failed to fetch"特定字符

c# - .NET 自动化 ControlType.Document : how to manipulate text?

azure - 如何提取子字符串的特定部分并从中创建列

sql - 读取位于 LINUX 服务器上的文本文件并更新 SQL Server 数据库中的表

azure - 使用 bicep 将混合连接部署到 webapp

azure - HIVE查询删除源数据 block