c#图文混合,以二进制方式写入数据库

`
在这里插入图片描述

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.Data.OleDb;
using System.IO;

namespace _07_二进制保存数据库
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //数据库连接字符串
        private static readonly string connStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb");
        MemoryStream StreamObject = null;


        //写入到数据库  写入按钮**************************************************************
        private void button1_Click(object sender, EventArgs e)
        {
            Byte[] bt;
            //string str;
            //使用内存流
            using (MemoryStream savefilestream = new MemoryStream())
            {
                richTextBox1.SaveFile(savefilestream, RichTextBoxStreamType.RichText);
                bt = savefilestream.ToArray();
            }

            //string sql = string.Format("insert into 表1(字段2)values(@字段2)");
            string sql = string.Format("insert into 表1(字段2)values(@字段2)");

            //string sql = "update Tab1 set richText=@richText where id=1";
            int a = CRdata(sql, "@字段2",bt);
            if (a > 0)
            {
                MessageBox.Show("写入成功");
            }

        }
        //从数据库中读取 读取按钮###############################################################
        private void button2_Click(object sender, EventArgs e)
        {
            //select 列1, 列2, 列3 from 表名 where 字段1 = '4';
            string sql1 = string.Format("select 字段2 from 表1 where ID=21");

            //从数据库中读出数据 
            DataTable dt = DCdata(sql1);
            byte[] bWrite = (byte[])dt.Rows[0][0];
            //将数组转换成stream 
            System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);
            //将stream填充到RichTextBox 
            this.richTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);

        }
        /// <summary>
        /// 文字加图片写入到数据库方法。二进制数组。
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="paramName"></param>
        /// <param name="btArray"></param>
        /// <returns></returns>
        public static int CRdata(string sql,string paramName, byte[] btArray)
        {
            int returnResult = 0;
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                using (OleDbCommand comm = new OleDbCommand(sql, conn))
                {
                    conn.Open();
                    //这一行决定是否写入成功。
                    comm.Parameters.Add(paramName, OleDbType.Binary, btArray.GetLength(0)).Value = btArray;
                    
                    returnResult = comm.ExecuteNonQuery();
                }
                conn.Close();
            }

            return returnResult;
        }

        /// <summary>
        /// 读取数据库方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="ReturnRowCounter"></param>
        /// <returns></returns>
        public static DataTable DCdata(string sql)
        {
            DataTable dt = new DataTable();
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                using (OleDbDataAdapter ADP = new OleDbDataAdapter(sql, conn))
                {
                    //ReturnRowCounter = ADP.Fill(dt);
                    ADP.Fill(dt);
                }
            }
            return dt;
        }
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值