Click here to Skip to main content
14,268,797 members
Rate this:
Please Sign up or sign in to 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:
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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!
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100