博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
postgre 二进制存储
阅读量:4511 次
发布时间:2019-06-08

本文共 4741 字,大约阅读时间需要 15 分钟。

show client_encoding;

set client_encoding='UTF8';

show server_encoding;
SELECT E'\\xDEADBEEF';

 

CREATE TABLE tbl_image_t

(
  id bigserial NOT NULL,
  name character varying(32),
  thumb bytea
)

insert into "tbl_image_t" values(4,'中文', E'\\x15191a1b')

SELECT id, name,  encode(thumb, 'hex')  FROM "tbl_image_t";

.net写入读取

public void Save(string imageName, string imageFilePath)        {            using (NpgsqlConnection pgConnection = new NpgsqlConnection(ConnString))            {                try                {                    using (FileStream pgFileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))                    {                        using (BinaryReader pgReader = new BinaryReader(new BufferedStream(pgFileStream)))                        {                            byte[] pgByteA = pgReader.ReadBytes(Convert.ToInt32(pgFileStream.Length));                            using (NpgsqlCommand pgCommand = new NpgsqlCommand("INSERT INTO \"tbl_image_t\" (name, thumb) SELECT @Name, @Image", pgConnection))                            {                                pgCommand.Parameters.AddWithValue("@Name", imageName);                                pgCommand.Parameters.AddWithValue("@Image", pgByteA);                                try                                {                                    pgConnection.Open();                                    pgCommand.ExecuteNonQuery();                                }                                catch                                {                                    throw;                                }                            }                        }                    }                }                catch                {                    throw;                }            }        }        public Image Get(string imageName)        {            using (NpgsqlConnection pgConnection = new NpgsqlConnection(ConnString))            {                try                {                    using (NpgsqlCommand pgCommand = new NpgsqlCommand("SELECT thumb FROM \"tbl_image_t\" WHERE name=@imageName;", pgConnection))                    {                        pgCommand.Parameters.AddWithValue("@imageName", imageName);                        try                        {                            pgConnection.Open();                            Byte[] productImageByte = (Byte[])pgCommand.ExecuteScalar();                            if (productImageByte != null)                            {                                using (Stream productImageStream = new System.IO.MemoryStream(productImageByte))                                {                                    return Image.FromStream(productImageStream);                                }                            }                        }                        catch                        {                            throw;                        }                    }                }                catch                {                    throw;                }            }            return null;        }

 

blob

private static void WriteLargeObject()        {            newcon.Open();            NpgsqlTransaction transaction = newcon.BeginTransaction();            LargeObjectManager largeObjectManager = new LargeObjectManager(newcon);            int noid = largeObjectManager.Create(LargeObjectManager.READWRITE);            LargeObject lo = largeObjectManager.Open(noid, LargeObjectManager.READWRITE);            FileStream fs = File.OpenRead(FileName);            byte[] buf = new byte[fs.Length];            fs.Read(buf, 0, (int)fs.Length);            lo.Write(buf);            lo.Close();            transaction.Commit();            transaction = newcon.BeginTransaction();            lo = largeObjectManager.Open(noid, LargeObjectManager.READWRITE);            FileStream fsout = File.OpenWrite(FileName + "database");            buf = lo.Read(lo.Size());            fsout.Write(buf, 0, (int)lo.Size());            fsout.Flush();            fsout.Close();            lo.Close();            transaction.Commit();            newcon.Close();            DeleteLargeObject(noid);            Console.WriteLine("noid: {0}", noid);        }        public static void DeleteLargeObject(Int32 noid)        {            //NpgsqlConnection conn = new NpgsqlConnection("server=localhost;user id=npgsql_tests;password=npgsql_tests");            newcon.Open();            NpgsqlTransaction t = newcon.BeginTransaction();            LargeObjectManager largeObjectManager = new LargeObjectManager(newcon);            largeObjectManager.Delete(noid);            t.Commit();            newcon.Close();        }

 

转载于:https://www.cnblogs.com/devourer/p/3364804.html

你可能感兴趣的文章
python学习笔记(xlwt/xlrd下载安装)
查看>>
java代码中添加log4j日志
查看>>
Java学习不走弯路教程(19 对于Service的自动注入)
查看>>
[CSS3] :empty Selector
查看>>
webpack4 入门(二)
查看>>
LoadRunner基本简介
查看>>
编写一个模拟注册用户和验证用户登陆的程序
查看>>
jquery 左侧多级菜单 根据xml文件自动生成
查看>>
JS - CommonJS、AMD、CMD
查看>>
Ajax笔记(一)
查看>>
生活网站
查看>>
java-5
查看>>
2019-05-16 Java学习日记 day6
查看>>
中间件
查看>>
Bytom移动端钱包SDK开发基础
查看>>
大龄恐惧症 (zz)
查看>>
MySQL数据分组GROUP BY 和HAVING
查看>>
vim配置成c++IDE
查看>>
iOS开发中APP之间传递信息1--URL Schema(应用程序间互相启动)
查看>>
MyEclipse持续性开发教程:用JPA和Spring管理数据(一)
查看>>