資源簡介
C# 將照片存儲到MySQL數(shù)據(jù)庫

代碼片段和文件信息
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();
????????????????//建立數(shù)據(jù)庫連接
????????????????MySqlConnection?conn?=?new?MySqlConnection(mysqlcon?);
????????????????conn.Open();
????????????????//設(shè)置命令參數(shù)
????????????????string?insertStr?=?“insert?into?img(image)?values(?imageByte)“;
????????????????MySqlCommand?comm?=?new?MySqlCommand();
????????????????comm.Connection?=?conn;
????????????????comm.CommandText?=?insertStr;
????????????????comm.CommandType?=?CommandType.Text;
????????????????//設(shè)置數(shù)據(jù)庫字段類型MediumBlob的值為圖片字節(jié)數(shù)組imageByte
????????????????comm.Parameters.Add(new?MySqlParameter(“?imageByte“?MySqlDbType.MediumBlob)).Value?=?imageByte;
????????????????//執(zhí)行命令
????????????????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();
????????????//建立數(shù)據(jù)庫連接
????????????MySqlCommand?comm?=?new?MySqlCommand(“select?image?from?img?where?id=‘“+textBox1.Text+“‘“?conn);
?//執(zhí)行命令并獲得數(shù)據(jù)讀取器
?MySqlDataReader?dr?=?comm.ExecuteReader();
?if?(dr.Read())
?{
?//讀出圖片字節(jié)數(shù)組至byte[]
?byte[]?imageByte?=?new?byte[dr.GetBytes(0?0?null?0?int.MaxValue)];
?dr.GetBytes(0?0?imageByte?0?imageByte.Length);
?//將圖片字節(jié)數(shù)組加載入緩沖流
?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??照片存儲數(shù)據(jù)庫\Dump20170419.sql
?????文件????????933??2017-07-20?09:26??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\app.config
?????文件??????90624??2017-07-20?09:52??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe
?????文件????????933??2017-07-20?09:26??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe.config
?????文件?????126464??2017-07-20?09:52??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.pdb
?????文件??????11600??2017-09-07?09:04??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe
?????文件????????933??2017-07-20?09:26??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.config
?????文件????????490??2010-03-17?22:39??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.manifest
?????文件???????3010??2017-07-20?09:52??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs
?????文件???????4366??2017-07-20?09:23??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Form1.Designer.cs
?????文件??????67716??2017-04-19?15:50??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Form1.resx
?????文件??????17787??2017-07-20?09:21??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????8981??2017-07-20?09:52??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????862??2017-04-19?15:53??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\GenerateResource.read.1.tlog
?????文件???????1222??2017-04-19?15:53??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\GenerateResource.write.1.tlog
?????文件??????17078??2017-04-19?15:53??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\ResolveAssemblyReference.cache
?????文件??????41472??2017-07-20?09:26??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\TempPE\Web?References.localhost.Reference.cs.dll
?????文件???????2755??2017-09-07?09:04??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csproj.FileListAbsolute.txt
?????文件????????975??2017-07-10?08:48??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csproj.GenerateResource.Cache
?????文件??????20571??2017-07-20?09:48??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.csprojResolveAssemblyReference.cache
?????文件??????90624??2017-07-20?09:52??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.exe
?????文件??????41552??2017-07-20?09:28??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.Form1.resources
?????文件?????126464??2017-07-20?09:52??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.pdb
?????文件????????180??2017-07-20?09:28??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\obj\x86\Debug\WindowsFormsApplication2.Properties.Resources.resources
?????文件????????505??2017-04-19?14:14??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Program.cs
?????文件???????1398??2017-04-19?14:14??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Properties\AssemblyInfo.cs
?????文件???????2896??2017-04-19?14:14??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Resources.Designer.cs
?????文件???????5612??2017-04-19?14:14??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Resources.resx
?????文件???????1722??2017-07-20?09:26??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Settings.Designer.cs
?????文件????????516??2017-07-20?09:26??照片存儲數(shù)據(jù)庫\WindowsFormsApplication2\WindowsFormsApplication2\Properties\Settings.settings
............此處省略35個文件信息
評論
共有 條評論