• 大小: 333KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: C#
  • 标签: C#  照片  MySQL  

资源简介

C# 将照片存储到MySQL数据库

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Data;
using System.IO;

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

        private static String mysqlcon = “database=picture;Password=121212;User ID=root;server=127.0.0.1“;
               


        private void button1_Click(object sender EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                MemoryStream  imageStream = new MemoryStream();

                pictureBox1.Image.Save(imageStream System.Drawing.Imaging.ImageFormat.Jpeg);

                byte[] imageByte = imageStream.GetBuffer();


                //建立数据库连接
                MySqlConnection conn = new MySqlConnection(mysqlcon );
                conn.Open();
                //设置命令参数
                string insertStr = “insert into img(image) values(?imageByte)“;
                MySqlCommand comm = new MySqlCommand();
                comm.Connection = conn;
                comm.CommandText = insertStr;
                comm.CommandType = CommandType.Text;
                //设置数据库字段类型MediumBlob的值为图片字节数组imageByte
                comm.Parameters.Add(new MySqlParameter(“?imageByte“ MySqlDbType.MediumBlob)).Value = imageByte;
                //执行命令
                try
                {
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                comm.Dispose();
                conn.Close();
                conn.Dispose();

            }
        }

        private void button2_Click(object sender System.EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection(mysqlcon );
            conn.Open();
            //建立数据库连接

            MySqlCommand comm = new MySqlCommand(“select image from img where id=‘“+textBox1.Text+“‘“ conn);
 //执行命令并获得数据读取器
 MySqlDataReader dr = comm.ExecuteReader();
 if (dr.Read())
 {
 //读出图片字节数组至byte[]
 byte[] imageByte = new byte[dr.GetBytes(0 0 null 0 int.MaxValue)];
 dr.GetBytes(0 0 imageByte 0 imageByte.Length);
 //将图片字节数组加载入缓冲流
 MemoryStream imageStream = new MemoryStream(imageByte);
 //从缓冲流生成图片
 Image image = Image.FromStream(imageStream true);
 pictureBox2.Image = image;
 }
 dr.Dispose();
 comm.Dispose();
 conn.Close();
 conn.Dispose();


        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     395034  2017-04-19 15:51  照片存储数据库\Dump20170419.sql

     文件        933  2017-07-20 09:26  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\app.config

     文件      90624  2017-07-20 09:52  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe

     文件        933  2017-07-20 09:26  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe.config

     文件     126464  2017-07-20 09:52  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.pdb

     文件      11600  2017-09-07 09:04  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe

     文件        933  2017-07-20 09:26  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.config

     文件        490  2010-03-17 22:39  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.manifest

     文件       3010  2017-07-20 09:52  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs

     文件       4366  2017-07-20 09:23  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Form1.Designer.cs

     文件      67716  2017-04-19 15:50  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Form1.resx

     文件      17787  2017-07-20 09:21  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       8981  2017-07-20 09:52  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        862  2017-04-19 15:53  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\GenerateResource.read.1.tlog

     文件       1222  2017-04-19 15:53  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\GenerateResource.write.1.tlog

     文件      17078  2017-04-19 15:53  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\ResolveAssemblyReference.cache

     文件      41472  2017-07-20 09:26  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\TempPE\Web References.localhost.Reference.cs.dll

     文件       2755  2017-09-07 09:04  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csproj.FileListAbsolute.txt

     文件        975  2017-07-10 08:48  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csproj.GenerateResource.Cache

     文件      20571  2017-07-20 09:48  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csprojResolveAssemblyReference.cache

     文件      90624  2017-07-20 09:52  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.exe

     文件      41552  2017-07-20 09:28  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.Form1.resources

     文件     126464  2017-07-20 09:52  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.pdb

     文件        180  2017-07-20 09:28  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.Properties.Resources.resources

     文件        505  2017-04-19 14:14  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Program.cs

     文件       1398  2017-04-19 14:14  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Properties\AssemblyInfo.cs

     文件       2896  2017-04-19 14:14  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Resources.Designer.cs

     文件       5612  2017-04-19 14:14  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Resources.resx

     文件       1722  2017-07-20 09:26  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Settings.Designer.cs

     文件        516  2017-07-20 09:26  照片存储数据库\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Settings.settings

............此处省略35个文件信息

评论

共有 条评论