存储C#数据结构到一个SQL数据库 [英] Storing C# data structure into a SQL database

查看:148
本文介绍了存储C#数据结构到一个SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来ASP.NET和SQL服务器的世界里,所以请原谅我的无知...

I am new to the world of ASP.NET and SQL server, so please pardon my ignorance ...

如果我在C#中的数据结构(例如用于让我们只想说,存储某些字符串的向量),是可以存储载体的内容是SQL表?我想这样做,以便它快于该数据尽可能快地转换回矢量形式,而不必通过元件来构造其元件。几乎类似于C的二进制数据写入一个文件,然后阅读它并将其复制到一个分配的结构。

If I have a data structure in C# (for e.g. let's just say, a vector that stores some strings), is it possible to store the contents of the vector as is in SQL table? I want to do this so that it fast to convert that data back into vector form as fast as possible without having to construct it element by element. Almost like writing binary data to a file and then reading it and copying it to an allocated structure in C.

我创建的SQL Server 2008的域中定义为VARBINARY(MAX)在桌子上。我想我会与开始。

I've created a table on SQL Server 2008 for which a field is defined as VARBINARY(MAX). I thought I'd start with that.

有人能告诉我如何,我会去存储和检索的,比方说,10串矢量,和从该领域中的例子吗?这甚至可能(我想不出为什么不)?

Could someone show me an example of how I would go about storing and retrieving a vector of, say, 10 strings, into and from that field? Is this even possible (I can't think of why not)?

谢谢!

推荐答案

首先,有简单地创建一个关系结构和​​在数据库中的对象映射到领域的明显路由。

First, there is the obvious route of simply creating a relational structure and mapping the object to fields in the database.

第二,如果你有一个对象,它是序列化,你可以将其存储在SQL服务器。我这样做的场合,并且已经使用了文本数据类型在SQL Server中存储XML。

Second, if you have an object that is serializable, you can store it in SQL server. I have done this on occasion, and have used the Text data type in SQL Server to store the XML.

的意见:我preFER存储序列化对象作为XML而不是二进制数据为什么?因为你其实可以读的是在那里(用于调试),并在SQL Server中,你可以使用XQuery从序列化对象选择数据。从我的经验,与具有数据是容易调试,并且可以在一个伪关系的方式来使用使用二进制数据的性能增益也不会是值得的。看看 SQL Server的XQuery功能。即使你不打算使用它了计划,也没有理由把自己放在一个角落。

Opinion: I prefer to store serialized objects as XML instead of binary data. Why? Because you can actually read what is in there (for debugging), and in SQL Server you can use XQuery to select data from the serialized object. From my experience, the performance gain of using binary data will not be worth it compared to having data that is easier to debug and can be used in a psuedo-relational fashion. Take a look at SQL Server's XQuery capabilities. Even if you don't plan on using it right away, there is no reason to put yourself in a corner.

您可能会看一些例子使用NetDataContractSerializer.

You might look at some examples using the NetDataContractSerializer.

我相信你所谓的载体是一个List<>在C#中。采取System.Collections.Generic看看。您可以使用NetDataContractSerializer序列化3串像一个列表:

I believe what you call a vector is a List<> in C#. Take a look in System.Collections.Generic. You can use the NetDataContractSerializer to serialize a List of 3 strings like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace SerializeThingy
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> myList = new List<string>();
            myList.Add("One");
            myList.Add("Two");
            myList.Add("Three");
            NetDataContractSerializer serializer = new NetDataContractSerializer();
            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, myList);
            stream.Position = 0;
            Console.WriteLine(ASCIIEncoding.ASCII.GetString(stream.ToArray()));
            List<string> myList2 = (List<string>)serializer.Deserialize(stream);
            Console.WriteLine(myList2[0]);
            Console.ReadKey();
        }
    }
}

这个例子只是连载列表,输出序列化到控制台,然后证明它是对的另一面正确的水分。我想你可以看到,从这里,你既可以转储内存流成字符串并写入到数据库,或使用其他流类型不是一个内存流做。

This example just serializes a list, outputs the serialization to the console, and then proves it was hydrated correctly on the flip side. I think you can see that from here you could either dump the memory stream into a string and write that to the database, or use another stream type than a memory stream to do it.

记住要引用System.Runtime.Serialization获得访问NetDataContractSerializer。

Remember to reference System.Runtime.Serialization to get access to the NetDataContractSerializer.

这篇关于存储C#数据结构到一个SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆