Restore varbinary (MAX) from SQL Server to byte [] in C #

I am trying to get varbinary(MAX) from SQL Server to the byte[] variable in C #.

How can i do this?

thanks

+7
source share
2 answers
 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[]; } } 
+19
source

You must select date (data) and data

where the data is your varbinary (max)

 int i=0; long dataLen = dr.GetInt64(i++); if (dataLen > 0) { Data = new byte[dataLen]; dr.GetBytes(i++, 0, Data, 0, (int)dataLen); } 
0
source

All Articles