下面是代码判断:
存入:
string StrConn=System.Configuration.ConfigurationManager.ConnectionStrings["DSN"].ToString();
SqlConnection sqlconn = new SqlConnection(StrConn);
SqlCommand sqlcomm = new SqlCommand("filesAdd",sqlconn);
sqlcomm.Parameters.Add("@filName",SqlDbType.NVarChar,32).Value=fileName;
sqlcomm.Parameters.Add("@fileData",SqlDbType.Image,fileLength).Value=fileData;
sqlcomm.Parameters.Add("@fileE", SqlDbType.NVarChar, 32).Value = fileE; try
{
sqlconn.Open();
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.ExecuteNonQuery();
}
catch (Exception)
{ throw;
}
finally {
sqlconn.Close();
}
读取:
//sqlReader[0] 就是数据,sqlReader[1]就是扩展名
try
{
sqlconn.Open();
sqlcomm.CommandType = CommandType.StoredProcedure;
SqlDataReader sqlReader = sqlcomm.ExecuteReader();
sqlReader.Read();
Stream Astream = File.Open("D:\\TTTT" + sqlReader[1].ToString(), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
Astream.Write((byte[])sqlReader[0], 0, ((byte[])sqlReader[0]).Length);
Astream.Flush();
Astream.Close();
}
catch (Exception)
{ throw;
}
finally
{
sqlconn.Close();
}问题:在调试的时候发现,sqlReader[0] 就是一个 byte[1]...为何??!
存入:
string StrConn=System.Configuration.ConfigurationManager.ConnectionStrings["DSN"].ToString();
SqlConnection sqlconn = new SqlConnection(StrConn);
SqlCommand sqlcomm = new SqlCommand("filesAdd",sqlconn);
sqlcomm.Parameters.Add("@filName",SqlDbType.NVarChar,32).Value=fileName;
sqlcomm.Parameters.Add("@fileData",SqlDbType.Image,fileLength).Value=fileData;
sqlcomm.Parameters.Add("@fileE", SqlDbType.NVarChar, 32).Value = fileE; try
{
sqlconn.Open();
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.ExecuteNonQuery();
}
catch (Exception)
{ throw;
}
finally {
sqlconn.Close();
}
读取:
//sqlReader[0] 就是数据,sqlReader[1]就是扩展名
try
{
sqlconn.Open();
sqlcomm.CommandType = CommandType.StoredProcedure;
SqlDataReader sqlReader = sqlcomm.ExecuteReader();
sqlReader.Read();
Stream Astream = File.Open("D:\\TTTT" + sqlReader[1].ToString(), FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
Astream.Write((byte[])sqlReader[0], 0, ((byte[])sqlReader[0]).Length);
Astream.Flush();
Astream.Close();
}
catch (Exception)
{ throw;
}
finally
{
sqlconn.Close();
}问题:在调试的时候发现,sqlReader[0] 就是一个 byte[1]...为何??!
解决方案 »
- 本人新学ASP.NET 不知该如何下手去学 希望.NET高手指点 该如何从头学起
- 菜鸟问题
- 未能映射路径“/foot.inc”。
- 能否IIS回收和在cache消失之前捕获到这个cache中的值?
- 做一个WebForm,如何实现聊天室的功能?如何能即时的在客户端显示服务器端发送的信息
- 求一生成图片缩略图的代码,谢谢拉急用,在线等
- ajax.net中客户端取得DataSet类型数据库在客户端分页
- aspnet的访问权限怎么设置啊???
- 在datagrid的模板列中绑定dropdownlist,如何在程序中对子控件的属性进行设置?
- 有没有人想过在web 上这样做报表?
- 如何把数据库查询的内容放到<table><tr><td></td></tr><table>中,不放到GridView中,自己手写放到表格里
- 给个文件上传的思路
SqlConnection conn = new SqlConnection("server=.;database=db;uid=sa;pwd=;");
conn.Open(); SqlCommand cmd = new SqlCommand("select top 1 * from [image]", conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read(); MemoryStream ms = new MemoryStream((Byte[])dr["pic"]);
Bitmap image = new Bitmap(ms);
image.Save(Server.MapPath("~/1.jpg")); this.Image1.ImageUrl = "1.jpg"; dr.Close();
conn.Close();我这样可以读出来,你看下,希望能给你有点帮助
这里用的是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();
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";
}
}
读取图片
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="显示你图片的页面";