当前位置: 代码迷 >> ASP.NET >> 请问一个图片存储到数据库以及读取的有关问题
  详细解决方案

请问一个图片存储到数据库以及读取的有关问题

热度:8863   发布时间:2013-02-25 00:00:00.0
请教一个图片存储到数据库以及读取的问题
今天无聊自己弄看看把图片存进数据库,以及从数据库读取,发现读取的时候页面就显示一个 X

附上代码,请教高手,帮忙看看是哪里出错了

代码没按照规范的3层架构写,敬请原谅~

就一个默认的Default 页面!

C# code
public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {         #region  测试读取        try        {            string sqlCmd = "select * from Image where imageID = 1";            string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;            SqlConnection sqlconn = new SqlConnection(connection);            sqlconn.Open();            SqlCommand sqc = new SqlCommand(sqlCmd, sqlconn);            SqlDataReader sdr = sqc.ExecuteReader();            if (sdr.Read())            {                Response.ContentType = sdr["imageType"].ToString();                Response.BinaryWrite((byte[])sdr["imageData"]);                sdr.Close();                sqlconn.Close();            }            else            {                Response.Write("NULL");            }        }        catch(Exception ex)        {            Response.Write(ex.StackTrace);        }        finally        {                   }        #endregion    }    protected void Button1_Click(object sender, EventArgs e)    {        #region 测试写入        if (!FileUpload1.HasFile)        {            Response.Write("请选择图片");        }            string fileEx = System.IO.Path.GetExtension(this.FileUpload1.FileName);  //获取图片扩展名            if (fileEx != ".gif" && fileEx != ".jpg" && fileEx != ".jpeg" && fileEx!= ".bmp")            {                Response.Write("只能上传图片");            }            else            {                                    // ImageType 有用,读取图片的时候需要                    int imgSize = this.FileUpload1.PostedFile.ContentLength;  //图片大小 (字节)                    string imgType = this.FileUpload1.PostedFile.ContentType;    //图片类型                    string imgPath = this.FileUpload1.PostedFile.FileName; //获取图片路径                    string imgName = this.FileUpload1.FileName; //获取图片名字                    int imgLength = this.FileUpload1.FileName.Length; //获取图片长度                    if (imgLength <= 0)//判断是否有图片                        return;                    Byte[] imgByte = new Byte[imgLength]; //用图片的长度来初始化一个字节数组存储临时的图片文件                    Stream stream = this.FileUpload1.PostedFile.InputStream; //建立文件流对象                    stream.Read(imgByte, 0, imgLength);// 读取图片数据到临时存储体imgByte,0为数据指针位置,fileLength为数据长度                    try                    {                        string connection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;                        SqlConnection sqlconn = new SqlConnection(connection);                        sqlconn.Open();                        string sqlcmd = "insert into Image values (@ImageType,@ImageData,@ImageTitle)";                        SqlCommand sqc = new SqlCommand(sqlcmd, sqlconn);                        sqc.Parameters.Add("@ImageType", SqlDbType.VarChar, 50).Value = imgType;                        sqc.Parameters.Add("@ImageData", SqlDbType.Image, imgLength).Value = imgByte;//将二进制的图片赋值给@ImageData                        sqc.Parameters.Add("@ImageTitle", SqlDbType.VarChar, 50).Value = imgName;                        int result = sqc.ExecuteNonQuery();                        sqlconn.Close();                        if (result != 0)                        {                            Response.Write("OK");                        }                        else                        {                            Response.Write("NO");                        }                    }                    catch (Exception ex)                    {                        Response.Write(ex.StackTrace);                    }                    finally                    {                                            }            }        #endregion        }}
  相关解决方案