如题,通过按钮上传和下载10张图片,存储图片。
话不多说,直接上代码
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.Data.SqlClient;
using System.IO;
using System.Collections;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Threading;
namespace ConsoleApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Byte[] img2stream(string imgpath)
{
FileStream imgstr = new FileStream(imgpath, FileMode.Open);
Byte[] buffer = new byte[imgstr.Length];
imgstr.Read(buffer, 0, buffer.Length);
Bitmap bmp = new Bitmap(imgstr);
Image image = bmp;
pictureBoxGetImage.Image = image;
imgstr.Close();
return buffer;
}
private void UpLoad(string name, string picpath)
{
string con =
"server =KING-SUN\\SUNSQLSERVER;database = sun;uid = sa;pwd = 123456";
SqlConnection conn = new SqlConnection(con);
//打开数据库连接
conn.Open();
//事先在SQL创建 存储过程 uploadpic
SqlCommand cmd = new SqlCommand("uploadpic", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
Byte[][] buffer = new Byte[11][];
for(int i = 1;i<11;i++)
{
string p = picpath + i + ".jpg";
buffer[i]= img2stream(p);
cmd.Parameters.Add("@Picture"+i, SqlDbType.Image).Value = buffer[i];
}
int n = cmd.ExecuteNonQuery();
if (n != 0)
{
MessageBox.Show("插入成功", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBoxGetID.Text= name+":图片导入数据库成功!";
}
else
{
MessageBox.Show("插入失败", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBoxGetID.Text = "图片导入数据库失败!";
}
conn.Close();
pictureBoxGetImage.Image = null;
}
public void button1_Click(object sender, EventArgs e)
{
textBoxGetDescription.Text = null;
try
{
string picname = "乔布斯";
string picpath = @"G:\picsource\";
UpLoad(picname, picpath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Download(string name)
{
string con =
"server =KING-SUN\\SUNSQLSERVER;database = sun;uid = sa;pwd = 123456";
SqlConnection conn = new SqlConnection(con);
//打开数据库连接
conn.Open();
SqlCommand cmd = new SqlCommand("downloadpic", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
for (int i = 1; i < 11; i++)
{
string rea = "RegPicture" + i;
byte[] imgbyte = (byte[])reader[rea];
MemoryStream ms = new MemoryStream(imgbyte);
Bitmap img = new Bitmap(ms);
string saven = "db" +name + i + ".jpg";
img.Save(saven);
ms.Close();
pictureBoxGetImage.Image = img;
if (img.Size.Height > 0)
textBoxGetDescription.Text = "图片导出成功!";
}
}
reader.Close();
conn.Close();
}
public void button2_Click(object sender, EventArgs e)
{
textBoxGetID.Text = null;
try
{
string namedown = "乔布斯";
Download(namedown);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
textBoxGetDescription.Text = "图片导出失败!";
}
}
}
}
结果截图:
*****************************************************************************************************************************************************************************************
**************************************************************************************************************************************************************************************
**************************************************************************************************************************************************************************************