什么是DynamicParameters(小巧玲珑)为varbinary数据类型的正确用法?

问题描述 投票:2回答:1

存储过程返回,除其他事项外,一个VARBINARY(最大值)作为输出。我不知道如何访问使用小巧精致的这些信息。

下面是一个能说明问题的一些删节的示例代码。我提供一些参数的StoredProcedure的,我希望找回在SQL Server上存储为varbinary(最大值)中的照片。

有没有为DbType之VARBINARY。我试着用DbType.Binary但这会导致在小巧玲珑的异常。如果我拍摄照片的参数了,所有其他参数我希望要回(被切出的样品为简洁起见的)都在工作。所以,唯一的问题是与检索varbinary数据。

什么是实现这一目标的正确方法是什么?

using (var connection = new System.Data.SqlClient.SqlConnection(HelperClassesBJH.HelperMethods.ConString("ProductionLocal")))
        {
            connection.Open();

            DynamicParameters p = new DynamicParameters();

            p.Add("@OpID", id, DbType.Int32, ParameterDirection.Input);
            p.Add("@StageID", Properties.Settings.Default.StageID, DbType.Int32, ParameterDirection.Input);               
            p.Add("@Photo", dbType: DbType.Binary, direction: ParameterDirection.Output);                

            try
            {
                connection.Execute(sql, p, commandType: CommandType.StoredProcedure);

                op.Photo = p.Get<byte[]>("@Photo");                    
            }
            catch {}

        }

更新:

我发现,我在DynamicParameters构造函数来提供“价值”参数。这避免了我得到的例外。我无法理解为什么我需要提供一个值,因为参数是一个输出,我提供的价值没有得到使用。下面是修改后的代码:

DynamicParameters p = new DynamicParameters();
MemoryStream b = new MemoryStream();

p.Add("@OpID", id, DbType.Int32, ParameterDirection.Input);
p.Add("@StageID", Properties.Settings.Default.StageID, DbType.Int32, ParameterDirection.Input);                
p.Add("@Photo", b, DbType.Binary, direction: ParameterDirection.Output);                

try
  {
     connection.Execute(sql, p, commandType: CommandType.StoredProcedure);

     op.Photo = p.Get<byte[]>("@Photo");
  }
 catch {}

这导致在其中包含期望图像数据的字节数组的检索。

dapper dynamicparameters
1个回答
0
投票

你可以试试:

p.Add("@Photo", dbType: DbType.Binary, direction: ParameterDirection.Output, size: -1);

这似乎为我工作的地方,那就是varchar(max)如何nvarchar(max)并映射(除非DbType.AnsiStringDbType.String,分别),因此这将是一致的。

© www.soinside.com 2019 - 2024. All rights reserved.