我想要保存并从数据库中检索对象。我编写C#代码来保存它,如下所示,它工作正常。现在我可以将对象保存到数据库中。
ReportObject ro = new ReportObject()
{
Name = ctrl.Name,
BackColor = ctrl.BackColor,
ForeColor = ctrl.BackColor,
Fonts = ctrl.Font,
TypeofControl = ctrl.GetType()
};
MemoryStream memStream = new MemoryStream();
StreamWriter sw = new StreamWriter(memStream);
sw.Write(ro);
string sql = "INSERT INTO [TemplateDetails] ([Object]) VALUES (@Object)";
SqlCommand cmd = new SqlCommand(sql, con, tran);
cmd.Parameters.Add("@Object", SqlDbType.VarBinary, Int32.MaxValue);
cmd.Parameters["@Object"].Value = memStream.GetBuffer();
cmd.ExecuteNonQuery();
我已经在SQL Server数据库中保存了一个C#对象ReportObject
。我想将其检索回C#对象。
string sql = "SELECT [Object] FROM [TemplateDetails]"
SqlDataReader dr = db.Reader(sql);
if (dr.Read())
{
byte[] arrays = (byte[])dr["Object"];
}