简体   繁体   English

从C#中的SQL Server参数获取字节数组

[英]GET byte array from SQL Server parameter in C#

I am trying to get a byte array from my SQL Server parameter. 我正在尝试从我的SQL Server参数获取字节数组。 I get the data from SQL Server like this: 我从SQL Server这样获得数据:

cmd.Parameters.Add("@Image1", SqlDbType.VarBinary).Size = 5000000;
cmd.Parameters["@Image1"].Direction = ParameterDirection.Output;

conn.Open();
cmd.ExecuteNonQuery();

string str = cmd.Parameters["@Image1"].Value.ToString();

I get the value System.Byte[] in string however I would need the whole byte array that I could store it as an image. 我得到字符串中的System.Byte[]值,但是我需要将其存储为图像的整个字节数组。

How would I do it using .Value returns an object would I convert that object to byte array? 我将如何使用.Value返回一个对象,将其转换为字节数组?

You might be overthinking this: 您可能会对此过度考虑:

byte[] imageArray = (byte[])cmd.Parameters["@Image1"].Value;

EDIT: Casted right hand side. 编辑:铸造右侧。 Thanks JuanR. 感谢JuanR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM