I'm having a problem either inserting or selecting data from a varbinary(max) column in my database (it is a localDB) and I can't seem to figure out why.
I'm adding the binary data from a C# application the following way.
SqlConnection con = new SqlConnection("<connection string...>");
SqlCommand cmd = new SqlCommand("sp_test_varbinary", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@content", SqlDbType.VarBinary, -1).Value = Convert.FromBase64String("<base64 string>");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
The sp_test_varbinary stored procedure takes just the one parameter @content and inserts it into a varbinary(max) column - so far all seems to work as expected. I can't tell what is in the varbinary(max) column, since it just says binary data, but something is there at least.
Then when I try to select the row I just inserted and attempt to get back the base64 string i originally had, something goes wrong or the data in the field is wrong.
I'm doing the following (file_id is the ID of the row I added).
SqlDataAdapter da = new SqlDataAdapter("SELECT [content] FROM [BaseFiles] WHERE ([file_id] = '" + file_id + "');", con);
DataSet ds = new DataSet();
da.Fill(ds);
string base64 = Convert.ToBase64String((byte[])ds.Tables[0].Rows["content"]);
At this point however, the base64 variable only contains the value:
7w==
Any idea what I'm missing to make this work?