读取二进制大对象

Posted .NET应用开发教程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取二进制大对象相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace main
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string fileName = ofd.FileName;
                FileStream fs = new FileStream(fileName, FileMode.Open);
                byte[] imageBytes = new byte[fs.Length];
                BinaryReader br = new BinaryReader(fs);
                imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));

                string s = "server=PC-20171113RBMO;database=StudentDB;Trusted_Connection=true";
                SqlConnection con = new SqlConnection(s);

                string c = "insert into Pic(image) values(@pic)";
                SqlCommand cmd = new SqlCommand(c, con);
                SqlParameter para = new SqlParameter("@pic", SqlDbType.Image);
                para.Value = imageBytes;
                cmd.Parameters.Add(para);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string s = "server=PC-20171113RBMO;database=StudentDB;Trusted_Connection=true";
            SqlConnection con = new SqlConnection(s);
            string c = "select image from Pic where ID = " + textBox1.Text.Trim();
            SqlCommand cmd = new SqlCommand(c, con);
            con.Open();
            byte[] image = (byte[])cmd.ExecuteScalar();
            con.Close();

            MemoryStream ms = new MemoryStream(image);
            Bitmap bmp = new Bitmap(ms);
            pictureBox1.Image = bmp;
        }
    }
}

 

以上是关于读取二进制大对象的主要内容,如果未能解决你的问题,请参考以下文章

Oracle数据库中的大对象(LOB)数据类型介绍

Python3读取大文件的方法

java如何读取文件流是啥格式

Java数据库——处理大数据对象

java大对象存取的简单实现的代码

kotlin-从一个片段更改多个片段的小数位