Click here to Skip to main content
14,298,585 members

How to retrieval of binary data from bytea data type in postgres

ashishsri asked:

Open original thread
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();
                }
            } 
Tags: C#, PostgreSQL, WinForms.Net

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




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