请高手指点!!!
我要将图片插入到数据库中,然后又要将他读取出来。(不要上传到文件夹中,而是直接存入数据库中)
我用的开发工具是Microsoft Visual Studio 2005,语言是C#。
有谁知道的话请提供完整的页面代码咯,包括 .aspx 和 .aspx.cs 页面。
我要将图片插入到数据库中,然后又要将他读取出来。(不要上传到文件夹中,而是直接存入数据库中)
我用的开发工具是Microsoft Visual Studio 2005,语言是C#。
有谁知道的话请提供完整的页面代码咯,包括 .aspx 和 .aspx.cs 页面。
解决方案 »
- DataSet 也 DataTable 是什么关系
- 怎样在服务器上生成一个新文件(xls),然后在客户端打开(或下载)?
- asp.net中怎么实现满足一定条件后,自动弹出已个窗口
- Asp.net Url 重写成功(使用Intelligencia.UrlRewriter),但Google不收录重定向后的页面,请大家帮忙
- 在vs2005中 使用qq生成现在状态。
- 怎么把dataset里面的数据倒回数据库?
- 我用dropdownlist控件做了数据绑定,但却显示不出结果来
- 如何理解“把一个对象存储于session中”,请对.NET基础了解深刻的同仁来探讨!
- 问个问题
- “求购!”^_^
- 如何让服务器持续响应
- asp.net如何封装对数据库操作,谢谢!希望有详细的例子
这里用的是MYSQL数据库的BLOB字段存储的图片二进制数据
由于用的是MYSQL数据库 所以在引用存储的方法与MSSQL数据库不一样,
MYSQL用的是“?”,而MSSQL数据库是“@”,请大家注意参考
InsertImage.aspxusing 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();
conn.Close();
this.Label1.Text="插入成功!";
this.show();
}
else
{
this.Label1.Text = "格式不正确!!!";
}
}
else
{
this.Label1.Text = "没有文件!!!";
}
}
private byte[] ImageToByte(string path)//声名一个把图片转化到二进制数据的方法
{
FileStream buffer = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(buffer);
byte[] image = br.ReadBytes((int)buffer.Length);
return image;
}
private void show()
{
MySqlDataAdapter da = new MySqlDataAdapter("select * from imgpic", conn);
DataSet ds = new DataSet();
da.Fill(ds, "a");
this.GridView1.DataSource = ds.Tables["a"].DefaultView;
this.GridView1.DataKeyNames = new string[] { "id" };
this.GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string sid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Session["id"] = sid;
this.Label1.Text = sid;
this.Image1.ImageUrl = "jpg2.aspx";
}
}
读取图片
ShowImage.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
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.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
public partial class Default2 : 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();
}
}
public void show()
{
string ss = Request.QueryString["id"].ToString();
string s3 = "select * from imgpic where id=" + ss;
MySqlCommand comm = new MySqlCommand(s3, conn);
conn.Open();
MySqlDataReader dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
while(dr.Read())
{
Response.Clear();
Response.C;
Response.BinaryWrite((byte[])dr["myimg"]);//读取
}
Response.End();
conn.Close();
}
}在你要显示的页面添加img 控件src="ShowImage.aspx";
这段代码你不用关心,也你不用复制这段,这是我用来测试用的(红色部分你不用写) private void show()
{
MySqlDataAdapter da = new MySqlDataAdapter("select * from imgpic", conn);
DataSet ds = new DataSet();
da.Fill(ds, "a");
this.GridView1.DataSource = ds.Tables["a"].DefaultView;
this.GridView1.DataKeyNames = new string[] { "id" };
this.GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string sid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Session["id"] = sid;
this.Label1.Text = sid;
this.Image1.ImageUrl = "jpg2.aspx";
} protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
string sid = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
Session["id"] = sid;
this.Label1.Text = sid;
this.Image1.ImageUrl = "jpg2.aspx";
}
你把所有带my去调就行
比如 MySqlConnection 去掉my 就是SqlConnection
以下都是这么做