Storing C# data structure into a SQL database

前端 未结 7 2051
半阙折子戏
半阙折子戏 2021-02-01 10:02

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

If I have a data structure in C# (for e.g. let\'s just say, a vector that stores some

相关标签:
7条回答
  • 2021-02-01 10:20
    [Serializable]
    public struct Vector3
    {
        public double x, y, z;
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Vector3 vector = new Vector3();
            vector.x = 1;
            vector.y = 2;
            vector.z = 3;
    
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, vector);
            string str = System.Convert.ToBase64String(memoryStream.ToArray());
    
            //Store str into the database
        }
    }
    
    0 讨论(0)
  • 2021-02-01 10:22

    There are reasons to be flexible. Rules, or guidelines, for database structure should not be allowed to impede creativity. Given the first thread here, I can see a hybrid approach for storing both serialized and constraining columns as well. Many an application could be vastly improved by keeping ones mind open to possibilities.

    Anyway, I appreciated a newbies perspective on the matter. Keeps us all fresh..

    0 讨论(0)
  • 2021-02-01 10:23

    I have more experience with relational databases than c#, but binary serialization is an acceptable way to go, as it allows the entire object's state to be saved into the database. XML serialization is pretty much the same, although generic types are not allowed.

    0 讨论(0)
  • 2021-02-01 10:30

    Assuming the objects are marked with [Serializable] or implement ISerializable the the BinaryFormatter class gives a simple way to do this.

    If not, you're looking at (non trivial) custom code.

    0 讨论(0)
  • 2021-02-01 10:30

    Here's another more general approach for generic lists. Note, the actual type stored in the list must also be serializable

    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    using System.Data.SqlClient;
    using System.Runtime.Serialization;
    
    public byte[] SerializeList<T>(List<T> list)
    {
    
        MemoryStream ms = new MemoryStream();
    
        BinaryFormatter bf = new BinaryFormatter();
    
        bf.Serialize(ms, list);
    
        ms.Position = 0;
    
        byte[] serializedList = new byte[ms.Length];
    
        ms.Read(serializedList, 0, (int)ms.Length);
    
        ms.Close();
    
        return serializedList; 
    
    } 
    
    public List<T> DeserializeList<T>(byte[] data)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
    
            ms.Write(data, 0, data.Length);
    
            ms.Position = 0;
    
            BinaryFormatter bf = new BinaryFormatter();
    
            List<T> list = bf.Deserialize(ms) as List<T>;
    
            return list;
        }
        catch (SerializationException ex)
        {
            // Handle deserialization problems here.
            Debug.WriteLine(ex.ToString());
    
            return null;
        }
    
    }
    

    Then in client code:

    List<string> stringList = new List<string>() { "January", "February", "March" };
    
    byte[] data = SerializeList<string>(stringList);
    

    One basic way of storing/retrieving this array of bytes could be to use simple SQLClient objects:

    SqlParameter param = new SqlParameter("columnName", SqlDbType.Binary, data.Length);
    param.Value = data; 
    
    etc...
    
    0 讨论(0)
  • 2021-02-01 10:34

    If you're gonna do that (and I guess it's technically possible), you might just as well use a flat file: there's no point in using a relational database any more.

    0 讨论(0)
提交回复
热议问题