Dapper是否支持SQL 2008表值参数2?

mcp*_*abi 9 c# model-view-controller orm dapper

我知道dapper可以支持TVF,但是如何与TVF一起发送额外的参数(不将其添加到IntDynamicParam类)?请参阅Tests.cs中的以下示例,我已修改为添加额外参数:

connection.Execute("CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)");
connection.Execute("CREATE PROC get_ints @x int, @ints int_list_type READONLY AS select * from @ints");
Run Code Online (Sandbox Code Playgroud)

我尝试了以下但是遇到了错误(从对象类型SqlMapper.Tests + IntDynamicParam到已知的托管提供者本机类型没有映射.):

var p = new DynamicParameters();
p.Add("x", 4);
p.Add("ints",new IntDynamicParam(new int[] { 1, 2, 3 }));

var nums = connection.Query<int>("get_ints", p).ToList();
Run Code Online (Sandbox Code Playgroud)

谢谢Sam的回复,但问题有点不同.我想知道如何传入另一个变量和元组.请参阅下面的修改后的SP:

CREATE TYPE int_tuple_list_type AS TABLE (n int NOT NULL PRIMARY KEY, n2 int)

CREATE PROC get_int_tuples 
  @someVar varchar(10),
  @ints int_tuple_list_type READONLY
AS select * from @ints
Run Code Online (Sandbox Code Playgroud)

Sam*_*ron 7

关于准备好运行开放的IDbCommand所IDynamicParameters需要担心的所有内容都很少AddParameters.

假设您想要一个整数元组,您可以实现以下内容:

CREATE TYPE int_tuple_list_type 
     AS TABLE (n int NOT NULL PRIMARY KEY, n2 int)
CREATE PROC get_int_tuples @ints 
     int_tuple_list_type READONLY AS select * from @ints
Run Code Online (Sandbox Code Playgroud)

其次是:

class TupleIntDynamicParam : Dapper.SqlMapper.IDynamicParameters
{
    IEnumerable<int> tuples;
    public IntDynamicParam(IEnumerable<Tuple<int,int>> tuples)
    {
        this.tuples= tuples;
    }

    public void AddParameters(IDbCommand command)
    {
        var sqlCommand = (SqlCommand)command;
        sqlCommand.CommandType = CommandType.StoredProcedure;

        List<Microsoft.SqlServer.Server.SqlDataRecord> number_list = 
           new List<Microsoft.SqlServer.Server.SqlDataRecord>();

        // Create an SqlMetaData object that describes our table type.
        Microsoft.SqlServer.Server.SqlMetaData[] tvp_definition = { 
          new Microsoft.SqlServer.Server.SqlMetaData("n", SqlDbType.Int), 
          new Microsoft.SqlServer.Server.SqlMetaData("n2", SqlDbType.Int) };

        foreach (int n in tuples)
        {
            // Create a new record, using the metadata array above.
            Microsoft.SqlServer.Server.SqlDataRecord rec = 
                new Microsoft.SqlServer.Server.SqlDataRecord(tvp_definition);
            rec.SetInt32(0, n.Item1);
            rec.SetInt32(1, n.Item2);
            number_list.Add(rec);      // Add it to the list.
        }

        // Add the table parameter.
        var p = sqlCommand.Parameters.Add("ints", SqlDbType.Structured);
        p.Direction = ParameterDirection.Input;
        p.TypeName = "int_tuple_list_type";
        p.Value = number_list;

    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以传入元组:

var nums = connection.Query("get_int_tuples", 
      new TupleIntDynamicParam (new Tuple<int,int>[] 
      { 
           Tuple.Create(1,2), Tuple.Create(2,3) 
      })).ToList();
Run Code Online (Sandbox Code Playgroud)

  • 你如何将其他参数与TVP一起传递? (6认同)

Uri*_*son 6

这是一个完整的解决方案,使用ICustomQueryParameter接口和我编写的一些代码(在附页的底部),让你使用dapper将任何IEnumerable发送到storedprocedure,即使T有多个属性:

http://code.google.com/p/dapper-dot-net/issues/detail?can=2&start=0&num=100&q=ICustomQueryParameter&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id= 69

这是链接中的代码:

  /// <summary>
    /// Send DataTable as dapper parameter
    /// </summary>
    public class DapperTableParameter : ICustomQueryParameter
    {
        protected DataTable _table = null;

        public DapperTableParameter(DataTable table)
        {
            _table = table;
        }

        public void AddParameter(System.Data.IDbCommand command, string name)
        {
            // This is SqlConnection specific
            ((SqlCommand)command).Parameters.Add("@" + name, SqlDbType.Structured).Value = _table;
        }
    }



public class DapperTVP<T> : DapperTableParameter
    {
        public DapperTVP(IEnumerable<T> list) : base(new System.Data.DataTable())
        {
            var t = typeof(T);
            var propertyByName = new Dictionary<string, PropertyInfo>();

            foreach (var p in t.GetProperties())
            {
                propertyByName.Add(p.Name, p);
                _table.Columns.Add(p.Name, p.PropertyType);
            }

            foreach (var i in list)
            {
                var row = _table.NewRow();
                foreach (var p in propertyByName)
                {
                    row[p.Key] = p.Value.GetValue(i, null);
                }

                _table.Rows.Add(row);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

您仍然必须ICustomQueryParameter像这样合并:http: //code.google.com/p/dapper-dot-net/issues/attachmentText?id = 69&aid = 690000000&name = SqlMapper.patch &token = wFLdWLM4LPamcAwcDaGqcITaAmg%3A1392913796708