资源简介
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个文件信息
- 上一篇:ASP.NET 动态网站开发基础教程(C#篇)
- 下一篇:WPF简单的视频直播案例
相关资源
- C#解析HL7消息的库135797
- C# OCR数字识别实例,采用TessnetOcr,对
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
评论
共有 条评论