日期:2014-05-18 浏览次数:20477 次
图片二进制读取于存储到数据库
这里用的是MYSQL数据库的BLOB字段存储的图片二进制数据
由于用的是MYSQL数据库 所以在引用存储的方法与MSSQL数据库不一样,
MYSQL用的是“?”,而MSSQL数据库是“@”,请大家注意参考
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Drawing.Imaging;
using MySql.Data.MySqlClient;//引用MYSQL命名空间
public partial class _Default : System.Web.UI.Page
{
MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["conmy"]);
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.show();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//文件扩展名
string Ex=FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf('.') + 1);
if (Ex.ToLower() == "jpg" || Ex.ToLower() == "gif")
{
int ImageSize = FileUpload1.PostedFile.ContentLength;//图片的大小
string ImageType = this.FileUpload1.PostedFile.ContentType;//图片类型
Stream ImageStream = this.FileUpload1.PostedFile.InputStream;
Byte[] ImageCount = new Byte[ImageSize];//调用方法转化二进制数据
int bt = ImageStream.Read(ImageCount, 0, ImageSize);
MySqlCommand comm = new MySqlCommand("testpic",conn);
comm.CommandType = CommandType.StoredProcedure;
MySqlParameter pj = new MySqlParameter("?myimage", MySqlDbType.LongBlob,ImageCount.Length);//图片
pj.Value =ImageCount;//给这字段赋值二进制数据
comm.Parameters.Add(pj);
MySqlParameter pname = new MySqlParameter("?imagename", MySqlDbType.VarChar, 100);//图片名称
pname.Value=FileUpload1.FileName;
comm.Parameters.Add(pname);
MySqlParameter psize = new MySqlParameter("?imagesize", MySqlDbType.Int32);//图片名称
psize.Value =ImageSize;
comm.Parameters.Add(psize);
conn.Open();
comm.ExecuteNonQuery();
&n