I'm trying to get a varbinary(MAX)
from SQL Server to a byte[]
variable in C#.
我正在尝试从SQL Server获取varbinary(MAX)到C#中的byte []变量。
How can I do this?
我怎样才能做到这一点?
Thanks
谢谢
2 个解决方案
#1
20
private static byte[] getDocument(int documentId)
{
using (SqlConnection cn = new SqlConnection("..."))
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = @"
SELECT DocumentData
FROM Document
WHERE DocumentId = @Id";
cm.Parameters.AddWithValue("@Id", documentId);
cn.Open();
return cm.ExecuteScalar() as byte[];
}
}
#2
0
You have to SELECT DATALENGTH(data) and data
您必须SELECT DATALENGTH(数据)和数据
where data is your varbinary(max)
数据是你的varbinary(最大)
int i=0;
long dataLen = dr.GetInt64(i++);
if (dataLen > 0)
{
Data = new byte[dataLen];
dr.GetBytes(i++, 0, Data, 0, (int)dataLen);
}
#1
20
private static byte[] getDocument(int documentId)
{
using (SqlConnection cn = new SqlConnection("..."))
using (SqlCommand cm = cn.CreateCommand())
{
cm.CommandText = @"
SELECT DocumentData
FROM Document
WHERE DocumentId = @Id";
cm.Parameters.AddWithValue("@Id", documentId);
cn.Open();
return cm.ExecuteScalar() as byte[];
}
}
#2
0
You have to SELECT DATALENGTH(data) and data
您必须SELECT DATALENGTH(数据)和数据
where data is your varbinary(max)
数据是你的varbinary(最大)
int i=0;
long dataLen = dr.GetInt64(i++);
if (dataLen > 0)
{
Data = new byte[dataLen];
dr.GetBytes(i++, 0, Data, 0, (int)dataLen);
}