我正在使用SqlFileStream将我的(大型)对象存储在Server上(在varbinary(max)列中),但我真的不能按我的意愿得到一个it。我一直试图找到一些专门用于序列化和存储(不仅仅是从磁盘读取文件到存储)的文章,以及如何最有效地做到这一点。有没有一些我还没有找到的“隐藏”最佳实践文章?或者有人有一个很好的例子来说明如何做到这一点?
我的主要来源是使用SqlFileStream和C#访问Server数据
编辑:
添加了示例代码,其中对象DataObject必须使用filestream存储在Server表中。我希望将我的对象流到数据库中是可能的,但我目前不知道如何以最好的方式处理这个对象,我需要对对象做些什么来流它,并记住对象可以大于1GB,其中包含非常复杂的结构。
新编辑:--我已经用数据结构测试了它,数据结构中包含的数据非常少,小于300 kb,没有问题。那么我在想是缓冲区问题还是类似的问题呢?
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
namespace QEStore
{
public class DataObject
{
private Dictionary<int, string> _values = new Dictionary<int, string>();
private string _objName;
}
public class Store
{
private string _connStr = "Data Source=127.0.0.1;Integrated Security=True;Initial Catalog=my_data;";
public void InsertObject(int id, DataObject data)
{
var insStmt =
@"insert into data (data_id) values (@dataID);
select data_value.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() from data where data_id = @dataID;";
string serverPath;
byte[] serverTxn;
using (var ts = new TransactionScope())
{
using (var conn = new SqlConnection(this._connStr))
{
conn.Open();
using (var cmd = new SqlCommand(insStmt, conn))
{
cmd.Parameters.Add("@dataID", SqlDbType.Int).Value = id;
using (var reader = cmd.ExecuteReader())
{
reader.Read();
serverPath = reader.GetSqlString(0).Value;
serverTxn = reader.GetSqlBinary(1).Value;
reader.Close();
}
}
using (var dest = new SqlFileStream(serverPath, serverTxn, FileAccess.Write))
{
// How to write the DataObject to the database using SqlFileStream
// dest.Write(...);
}
}
ts.Complete();
}
}
}
}我尝试按以下方式序列化该对象,但得到了一个IOException错误,上面写着“句柄无效”:
using (var dest = new SqlFileStream(serverPath, serverTxn, FileAccess.Write))
{
var formatter = new BinaryFormatter();
formatter.Serialize(dest, data);
dest.Close();
}发布于 2012-02-06 13:22:36
我发现了问题。问题是内置序列化程序无法处理大型数据对象,而是使用原状网工作。
https://stackoverflow.com/questions/9112049
复制相似问题