title: Convert byte[] to SqlBytes
body: <p>I want to do some testing of a SQL CLR procedure and I'm trying to replicate it in a local C# project for better debugging.</p>
  
  <p>I need to convert the data returned from the database to <code>SqlBytes</code> in order for my C# function to accept it but it tells me that I can't implicitly convert it. Note that the SQL column is of type <code>image</code>. </p>
  
  <p>Code:</p>
  
  <pre><code>        SqlConnection con = new SqlConnection(connString);
          SqlDataAdapter da = new SqlDataAdapter(query, con); //query gets a field from the database of type 'image'
          SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
          DataSet ds = new DataSet("Image");
          byte[] MyData = new byte[0];
          da.Fill(ds, "Image");
          DataRow myRow;
          myRow = ds.Tables["Image"].Rows[0];
  
          MyData = (byte[])myRow["econtents"];
          SqlBytes SQL = (SqlBytes)MyData; //Cannot implicitly convert type 'byte[]' to 'System.Data.SqlTypes.SqlBytes'
  </code></pre>
  
  <p>I've tried a few different options/syntaxes for the last line and also just passing <code>MyData</code> into the method directly. </p>
  
  <p>I've been following <a href="https://support.microsoft.com/en-gb/help/309158/how-to-read-and-write-blob-data-by-using-ado-net-with-visual-c-net" rel="nofollow noreferrer">this Microsoft article</a> but it's not quite the same as my scenario. For reference I want to pass the data returned from the SQL query to: <code>public static SqlString WriteToFile(SqlBytes binary, SqlString path, SqlBoolean append)</code></p>
  
  <p>How do I perform this conversion, or is it only possible to pass in <code>SqlBytes</code> when using a CLR function from the SQL side?</p>