Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
We have data in serialized form of binary.Now we have to store it in PostGre database.
For insertion , we had stored the code in data column of bytea .
And use the following code:
C#
using (FileStream pgFileStream = new FileStream("F:\\Test.abc", FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader pgReader = new BinaryReader(new BufferedStream(pgFileStream)))
                {

                    NpgsqlCommand command = new NpgsqlCommand();

                    byte[] ByteA = pgReader.ReadBytes(Convert.ToInt32(pgFileStream.Length));
                    command.CommandText = "insert into ptadata (memberid, filedata) VALUES (03, @data)";
                    command.Connection = m_IDBcloudConnection;

                    //m_IDBcloudConnection.Close();
                    if (m_IDBcloudConnection.State != ConnectionState.Open)
                    {
                        m_IDBcloudConnection.Open();
                    }                    
                    command.Parameters.Add("@data", ByteA).Value = ByteA;

                    command.ExecuteNonQuery();

                    m_IDBcloudConnection.Close();
                }
            }
But when we try to retrieve it ,comes in the form of byte.
How can I get the data in serialized form of binary?

What I have tried:

using (FileStream pgFileStream = new FileStream("F:\\Test.abc", FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader pgReader = new BinaryReader(new BufferedStream(pgFileStream)))
                {

                    NpgsqlCommand command = new NpgsqlCommand();

                    byte[] ByteA = pgReader.ReadBytes(Convert.ToInt32(pgFileStream.Length));
                    command.CommandText = "insert into ptadata (memberid, filedata) VALUES (03, @data)";
                    command.Connection = m_IDBcloudConnection;

                    //m_IDBcloudConnection.Close();
                    if (m_IDBcloudConnection.State != ConnectionState.Open)
                    {
                        m_IDBcloudConnection.Open();
                    }                    
                    command.Parameters.Add("@data", ByteA).Value = ByteA;

                    command.ExecuteNonQuery();

                    m_IDBcloudConnection.Close();
                }
            } 
Posted
Updated 14-Aug-19 3:14am
v2
Comments
Afzaal Ahmad Zeeshan 22-Jul-19 10:51am    
But why store the data in database? Why can't a blob be stored in the file system and the relation (file path) of it be stored in a record with the character data type?
ashishsri 23-Jul-19 3:24am    
Actually I want to store our project in postgre and project is binary serialized file.
in postgre database there is no blob data type so we use bytea data type in postgre.

1 solution

Check out this: sql - How to insert and retrieve image from PostgreSql using C# - Stack Overflow[^]

The process of inserting and retrieving image is the process of operating on binary data. So, it meets your criteria.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900