我有以下代码写入 SQL Server 数据库:
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;
}
当我执行此代码时,我得到 0x 存储在Result
列中。我不确定这是否正确,因此我需要一些帮助。
基本上,我需要将完整的对象存储到 SQL Server 数据库表中的单个列中。