I have the following code which writes to a SQL Server database:
private void InsertResult(Result results, string messageId)
{
object chkresult = (results);
MemoryStream memStream = new MemoryStream();
StreamWriter sw = new StreamWriter(memStream);
sw.Write(chkresult);
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Reporting"].ConnectionString);
using (conn)
{
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.CommandText = "Results_Ins";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Id", SqlDbType.VarChar, 50).Value = messageId;
cmd.Parameters.Add("@result", SqlDbType.VarBinary, Int32.MaxValue);
cmd.Parameters["@result"].Value = memStream.GetBuffer();
cmd.Parameters.Add("@RowCount", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Connection = conn;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
// handle the error
//messageInsert = false;
}
finally
{
conn.Close();
}
}
}
//return rowCount;
}
When I execute this code, I get 0x being stored in the Result column. I'm not sure this is correct, and therefore I need some help.
Basically, I need to store a complete object into a single column in a SQL Server database table.