当前位置:网站首页>C # mixed graphics and text, written to the database in binary mode

C # mixed graphics and text, written to the database in binary mode

2022-07-05 17:29:00 Game programming

`

c# Graphic blending , Write to the database in binary - The first 1 Zhang
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_ Binary save database {    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        // Database connection string         private static readonly string connStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB.mdb");        MemoryStream StreamObject = null;        // Write to the database    Write button **************************************************************        private void button1_Click(object sender, EventArgs e)        {            Byte[] bt;            //string str;            // Use memory streams             using (MemoryStream savefilestream = new MemoryStream())            {                richTextBox1.SaveFile(savefilestream, RichTextBoxStreamType.RichText);                bt = savefilestream.ToArray();            }            //string sql = string.Format("insert into  surface 1( Field 2)values(@ Field 2)");            string sql = string.Format("insert into  surface 1( Field 2)values(@ Field 2)");            //string sql = "update Tab1 set [email protected] where id=1";            int a = CRdata(sql, "@ Field 2",bt);            if (a > 0)            {                MessageBox.Show(" Write successfully ");            }        }        // Read from database   Read button ###############################################################        private void button2_Click(object sender, EventArgs e)        {            //select  Column 1,  Column 2,  Column 3 from  Table name  where  Field 1 = '4';            string sql1 = string.Format("select  Field 2 from  surface 1 where ID=21");            // Read data from the database              DataTable dt = DCdata(sql1);            byte[] bWrite = (byte[])dt.Rows[0][0];            // Convert an array to stream             System.IO.MemoryStream mstream = new System.IO.MemoryStream(bWrite, false);            // take stream Fill in RichTextBox             this.richTextBox1.LoadFile(mstream, RichTextBoxStreamType.RichText);        }        /// <summary>        ///  The method of writing text and pictures to the database . Binary array .        /// </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();                    // This line determines whether the writing is successful .                    comm.Parameters.Add(paramName, OleDbType.Binary, btArray.GetLength(0)).Value = btArray;                    returnResult = comm.ExecuteNonQuery();                }                conn.Close();            }            return returnResult;        }        /// <summary>        ///  Read database method         /// </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;        }    }}

author : Reading in rainy days

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .

原网站

版权声明
本文为[Game programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051659471518.html