ServiceStack.Text:将 DataSet 序列化为 json
ServiceStack.Text: serializing DataSet to json
我在使用 ServiceStack.Text(来自 Nuget.org)将数据集序列化为 json 时遇到问题。我正在使用最新的稳定版本 4.0.50 和 VS 2015。我不断收到
Process is terminated due to WhosebugException
我的代码:
using System;
using System.Data;
using System.Data.SqlClient;
using ServiceStack.Text;
namespace TestServiceStackText
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = @"Server=MyServer; Database=NORTHWND; User Id=SomeUser; Password=SomePassword;";
string SqlQuery = @"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]";
// Create new dataset instance
DataSet dataset = new DataSet();
// Fill it with a little data: 1 table, 1 row
using (var conn = new SqlConnection())
{
using (var da = new SqlDataAdapter())
{
using (da.SelectCommand = conn.CreateCommand())
{
da.SelectCommand.CommandText = SqlQuery;
da.SelectCommand.Connection.ConnectionString = ConnectionString;
da.Fill(dataset);
}
}
}
// Serialize to json: exception occurs here
string json = TypeSerializer.SerializeToString<DataSet>(dataset);
Console.WriteLine("Dataset serialized to json:\n {0}", json);
// Deserialize to DataSet
DataSet ds = TypeSerializer.DeserializeFromString<DataSet>(json);
Console.WriteLine("Name: {0}, Nr. of Tables: {1}", ds.DataSetName, ds.Tables.Count);
}
}
}
有什么建议吗?
None 的 ServiceStack 文本序列化器明确支持数据集,这是一种糟糕的序列化类型。
Micro ORM 类似于 OrmLite ends up being much cleaner and easier to use which maps to ,例如上述查询的等效代码是:
var customers = Db.Select<Customer>(q => q.Take(1));
var json = customers.ToJson();
var dto = json.FromJson<Customer>();
或者,如果您不想创建客户类型,您可以使用 OrmLite's dynamic API's 映射到丢失类型的对象列表:
var list = Db.Select<List<object>>(
"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");
或词典:
var dict = Db.Select<Dictionary<string,object>>(
"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");
检查 northwind.servicestack.net project for code examples of using OrmLite and AutoQuery 以查询 Northwind 数据集。
总而言之,如果您使用干净的 POCO 而不是每个序列化程序都支持的数据集,您将遇到的问题会少很多,它比数据集更小更快,您还可以将 POCO 或松散类型的字典序列化为CSV ServiceStack.Text:
var csv = customers.ToCsv();
var csv = dict.ToCsv();
我在使用 ServiceStack.Text(来自 Nuget.org)将数据集序列化为 json 时遇到问题。我正在使用最新的稳定版本 4.0.50 和 VS 2015。我不断收到
Process is terminated due to WhosebugException
我的代码:
using System;
using System.Data;
using System.Data.SqlClient;
using ServiceStack.Text;
namespace TestServiceStackText
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = @"Server=MyServer; Database=NORTHWND; User Id=SomeUser; Password=SomePassword;";
string SqlQuery = @"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]";
// Create new dataset instance
DataSet dataset = new DataSet();
// Fill it with a little data: 1 table, 1 row
using (var conn = new SqlConnection())
{
using (var da = new SqlDataAdapter())
{
using (da.SelectCommand = conn.CreateCommand())
{
da.SelectCommand.CommandText = SqlQuery;
da.SelectCommand.Connection.ConnectionString = ConnectionString;
da.Fill(dataset);
}
}
}
// Serialize to json: exception occurs here
string json = TypeSerializer.SerializeToString<DataSet>(dataset);
Console.WriteLine("Dataset serialized to json:\n {0}", json);
// Deserialize to DataSet
DataSet ds = TypeSerializer.DeserializeFromString<DataSet>(json);
Console.WriteLine("Name: {0}, Nr. of Tables: {1}", ds.DataSetName, ds.Tables.Count);
}
}
}
有什么建议吗?
None 的 ServiceStack 文本序列化器明确支持数据集,这是一种糟糕的序列化类型。
Micro ORM 类似于 OrmLite ends up being much cleaner and easier to use which maps to
var customers = Db.Select<Customer>(q => q.Take(1));
var json = customers.ToJson();
var dto = json.FromJson<Customer>();
或者,如果您不想创建客户类型,您可以使用 OrmLite's dynamic API's 映射到丢失类型的对象列表:
var list = Db.Select<List<object>>(
"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");
或词典:
var dict = Db.Select<Dictionary<string,object>>(
"SELECT TOP 1 * FROM [NORTHWND].[dbo].[Customers]");
检查 northwind.servicestack.net project for code examples of using OrmLite and AutoQuery 以查询 Northwind 数据集。
总而言之,如果您使用干净的 POCO 而不是每个序列化程序都支持的数据集,您将遇到的问题会少很多,它比数据集更小更快,您还可以将 POCO 或松散类型的字典序列化为CSV ServiceStack.Text:
var csv = customers.ToCsv();
var csv = dict.ToCsv();