Is it possible to use `SqlDbType.Structured` to pass Table-Valued Parameters in NHibernate?

前端 未结 4 805
花落未央
花落未央 2020-12-04 16:12

I want to pass a collection of ids to a stored procedure that will be mapped using NHibernate. This technique was introduced in Sql Server 2008 ( more info here => Table-Val

4条回答
  •  臣服心动
    2020-12-04 16:37

    A simpler solution than the accepted answer would be to use ADO.NET. NHibernate allows users to enlist IDbCommands into NHibernate transactions.

    DataTable myIntsDataTable = new DataTable();
    myIntsDataTable.Columns.Add("ID", typeof(int));
    
    // ... Add rows to DataTable
    ISession session = sessionFactory.GetSession();
    using(ITransaction transaction = session.BeginTransaction())
    {
        IDbCommand command = new SqlCommand("StoredProcedureName");
        command.Connection = session.Connection;
        command.CommandType = CommandType.StoredProcedure;
        var parameter = new SqlParameter();
        parameter.ParameterName = "IntTable";
        parameter.SqlDbType = SqlDbType.Structured;
        parameter.Value = myIntsDataTable;
        command.Parameters.Add(parameter);            
        session.Transaction.Enlist(command);
        command.ExecuteNonQuery();
    }
    

提交回复
热议问题