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

前端 未结 2 994
不知归路
不知归路 2020-12-18 23:15

I\'m trying to get a varbinary(MAX) from SQL Server to a byte[] variable in C#.

How can I do this?

Thanks

2条回答
  •  半阙折子戏
    2020-12-18 23:33

    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[];
        }
    }
    

提交回复
热议问题