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.
I need to convert the data returned from the database to SqlBytes 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 image.
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'
I've tried a few different options/syntaxes for the last line and also just passing MyData into the method directly.
I've been following this Microsoft article but it's not quite the same as my scenario. For reference I want to pass the data returned from the SQL query to: public static SqlString WriteToFile(SqlBytes binary, SqlString path, SqlBoolean append)
How do I perform this conversion, or is it only possible to pass in SqlBytes when using a CLR function from the SQL side?