I am using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to Serialize a complex Project object and store it as a .dat file on the local machine. Then I can deserialize this stream and cast to Project to get an exact copy of the original project. The (simplified) code is as follows:
我正在使用System.Runtime.Serialization.Formatters.Binary.BinaryFormatter来序列化复杂的Project对象并将其作为.dat文件存储在本地计算机上。然后我可以反序列化此流并转换为Project以获取原始项目的精确副本。 (简化)代码如下:
Project project = new Project();
FileStream stream = new FileStream("file.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, project);
stream.Close();
...
stream = new FileStream("file.dat", FileMode.Open);
Project revisitedProject = (Project)formatter.Deserialize(stream);
stream.Close();
and this works for me with no loss of data, as it should. However, this only allows me to store the project on the local machine, so if the device is lost or damaged then so are all of the user's projects. The Project class is too complex to store in a table, so I'm hoping that somebody with more knowledge of Serialization can help me out a bit because I'm very beginner with the concept.
这对我有用,没有数据丢失,应该如此。但是,这只允许我将项目存储在本地计算机上,因此如果设备丢失或损坏,那么所有用户的项目也是如此。 Project类太复杂而无法存储在表中,因此我希望有更多Serialization知识的人可以帮助我,因为我对这个概念非常初学。
I would like to serialize the project just like I already do.
我想像我已经做的那样序列化项目。
Then, I want to deserialize the project and cast to a string, this string will be stored in a table.
然后,我想反序列化项目并转换为字符串,此字符串将存储在表中。
If the project ever needs to be recovered, that string will be serialized once again and stored in the local machine as a .dat file.
如果需要恢复项目,则该字符串将再次序列化并作为.dat文件存储在本地计算机中。
Then, if I deserialize that .dat file and cast it to Project, will I have an exact copy of my original project or would casting and storing it as a string have caused me to lose data?
然后,如果我反序列化该.dat文件并将其强制转换为Project,我是否会拥有原始项目的精确副本,或者将其作为字符串进行转换和存储会导致我丢失数据?
1 个解决方案
#1
2
When you have your .dat file, you already have a serialized version of the complex object, no need to turn this into string. All modern databases support storing data as a blob: that is, data without structure that would be understood by the database engine. You can store the content of your .dat file(s) in a database, in a blob field. Different databases call this data type in a different way, e.g. MSSQL calls it varbinary
.
当您拥有.dat文件时,您已经拥有了复杂对象的序列化版本,无需将其转换为字符串。所有现代数据库都支持将数据存储为blob:即没有数据库引擎可以理解的结构的数据。您可以将.dat文件的内容存储在数据库的blob字段中。不同的数据库以不同的方式调用此数据类型,例如MSSQL将其称为varbinary。
As an addition, if you insist converting to string, consider converting the binary using base64. It will make it safe both encoding and decoding without codepage and language encoding concerns.
另外,如果您坚持转换为字符串,请考虑使用base64转换二进制文件。它将使编码和解码安全,无需代码页和语言编码问题。
#1
2
When you have your .dat file, you already have a serialized version of the complex object, no need to turn this into string. All modern databases support storing data as a blob: that is, data without structure that would be understood by the database engine. You can store the content of your .dat file(s) in a database, in a blob field. Different databases call this data type in a different way, e.g. MSSQL calls it varbinary
.
当您拥有.dat文件时,您已经拥有了复杂对象的序列化版本,无需将其转换为字符串。所有现代数据库都支持将数据存储为blob:即没有数据库引擎可以理解的结构的数据。您可以将.dat文件的内容存储在数据库的blob字段中。不同的数据库以不同的方式调用此数据类型,例如MSSQL将其称为varbinary。
As an addition, if you insist converting to string, consider converting the binary using base64. It will make it safe both encoding and decoding without codepage and language encoding concerns.
另外,如果您坚持转换为字符串,请考虑使用base64转换二进制文件。它将使编码和解码安全,无需代码页和语言编码问题。