当前位置:网站首页>c#图文混合,以二进制方式写入数据库
c#图文混合,以二进制方式写入数据库
2022-07-05 16:59:00 【游戏编程】
`

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 [email protected] 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; } }}作者:雨天读书
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- Check the WiFi password connected to your computer
- 【性能测试】全链路压测
- 力扣解法汇总1200-最小绝对差
- mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
- C language to get program running time
- thinkphp3.2.3
- Zhang Ping'an: accélérer l'innovation numérique dans le cloud et construire conjointement un écosystème industriel intelligent
- Matery主题自定义(一)黑夜模式
- MySQL queries the latest qualified data rows
- 一文了解MySQL事务隔离级别
猜你喜欢

CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计

VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
![[first lecture on robot coordinate system]](/img/3c/af056f0fe68b3244a3dc491ceb291d.png)
[first lecture on robot coordinate system]

机器学习02:模型评估

Winedt common shortcut key modify shortcut key latex compile button

Embedded -arm (bare board development) -2

Deeply cultivate 5g, and smart core continues to promote 5g applications

ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée

33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
Database design in multi tenant mode
随机推荐
[binary tree] insufficient nodes on the root to leaf path
Understand the usage of functions and methods in go language
云安全日报220705:红帽PHP解释器发现执行任意代码漏洞,需要尽快升级
独立开发,不失为程序员的一条出路
Oracle缩表空间的完整解决实例
[Web attack and Defense] WAF detection technology map
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
兰空图床苹果快捷指令
Tips for extracting JSON fields from MySQL
ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
mysql中取出json字段的小技巧
Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
Deeply cultivate 5g, and smart core continues to promote 5g applications
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
Embedded-c Language-4
漫画:有趣的海盗问题 (完整版)
C # realizes crystal report binding data and printing 3-qr code barcode
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
winedt常用快捷键 修改快捷键latex编译按钮
ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"