如何将图片以二进制的形式存入数据库?

时间:2022-08-01 21:43:19
在Winform中,如果将图片以二进制的形式存入数据库?求各位大侠帮帮忙

5 个解决方案

#2


保存图片路径到数据库就行了。

#3


字段:1.id 自增长2.img ole对象详细实现代码:default.aspx  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <div>      <asp:FileUpload ID="FileUpload1" runat="server" /><br />        <br />         <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>          </form></body></html>default.aspx.csusing 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.OleDb;using System.IO;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {    }    protected void Button1_Click(object sender, EventArgs e)    {        try        {            if (FileUpload1.HasFile)            {                if (IsAllowedExtension(this.FileUpload1))                {                    //取得上传文件的大小                    int FileLen = FileUpload1.PostedFile.ContentLength;                    Byte[] FileData = new Byte[FileLen];                    //创建访问客户端上传文件的对象                    HttpPostedFile hp = FileUpload1.PostedFile;                    //创建数据流对象                    Stream sr = hp.InputStream;                    //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置                    sr.Read(FileData, 0, FileLen);                    OleDbConnection conn = new OleDbConnection("Provider = Microsoft.JET.OleDB.4.0;Data Source = " + Server.MapPath("mydata.mdb"));                    conn.Open();                    OleDbCommand cmd = new OleDbCommand("insert into table_img(img) values(?)", conn);                    //存储过程插入到数据库中                    OleDbParameter para = new OleDbParameter("?", OleDbType.Binary);                    para.Value = FileData;                    cmd.Parameters.Add(para);                    cmd.ExecuteNonQuery();                    //弹出上传成功的提示                    Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传成功‘);</script>");                    //关闭数据库连接                    conn.Close();                }                else                {                    Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传文件格式不对,只允许上传jpg和gif格式的文件‘);</script>");                }            }            else            {                Response.Write("<script language = ‘javascript‘ type = ‘text/javascript‘>alert(‘请选择上传文件‘);</script>");            }        }        catch        {        }    }    #region 实现真正意义上的文件类型判断    public static bool IsAllowedExtension(FileUpload hifile)    {        System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);        System.IO.BinaryReader r = new System.IO.BinaryReader(fs);        string fileclass = "";        byte buffer;        try        {            buffer = r.ReadByte();            fileclass = buffer.ToString();            buffer = r.ReadByte();            fileclass += buffer.ToString();        }        catch        {        }        r.Close();        fs.Close();        if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar        {            return true;        }        else        {            return false;        }    }    #endregion}

							     

#4


用FileUpload控件

System.IO.Stream fileDataStream = DocUpload.PostedFile.InputStream;
//得到文件大小 
                int fileLength = DocUpload.PostedFile.ContentLength;

                //创建数组 
                byte[] fileData = new byte[fileLength];
                //把文件流填充到数组 
                fileDataStream.Read(fileData, 0, fileLength);

#5


使用读写流 如何将图片以二进制的形式存入数据库?

#1


#2


保存图片路径到数据库就行了。

#3


字段:1.id 自增长2.img ole对象详细实现代码:default.aspx  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <div>      <asp:FileUpload ID="FileUpload1" runat="server" /><br />        <br />         <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>          </form></body></html>default.aspx.csusing 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.OleDb;using System.IO;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {    }    protected void Button1_Click(object sender, EventArgs e)    {        try        {            if (FileUpload1.HasFile)            {                if (IsAllowedExtension(this.FileUpload1))                {                    //取得上传文件的大小                    int FileLen = FileUpload1.PostedFile.ContentLength;                    Byte[] FileData = new Byte[FileLen];                    //创建访问客户端上传文件的对象                    HttpPostedFile hp = FileUpload1.PostedFile;                    //创建数据流对象                    Stream sr = hp.InputStream;                    //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置                    sr.Read(FileData, 0, FileLen);                    OleDbConnection conn = new OleDbConnection("Provider = Microsoft.JET.OleDB.4.0;Data Source = " + Server.MapPath("mydata.mdb"));                    conn.Open();                    OleDbCommand cmd = new OleDbCommand("insert into table_img(img) values(?)", conn);                    //存储过程插入到数据库中                    OleDbParameter para = new OleDbParameter("?", OleDbType.Binary);                    para.Value = FileData;                    cmd.Parameters.Add(para);                    cmd.ExecuteNonQuery();                    //弹出上传成功的提示                    Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传成功‘);</script>");                    //关闭数据库连接                    conn.Close();                }                else                {                    Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传文件格式不对,只允许上传jpg和gif格式的文件‘);</script>");                }            }            else            {                Response.Write("<script language = ‘javascript‘ type = ‘text/javascript‘>alert(‘请选择上传文件‘);</script>");            }        }        catch        {        }    }    #region 实现真正意义上的文件类型判断    public static bool IsAllowedExtension(FileUpload hifile)    {        System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);        System.IO.BinaryReader r = new System.IO.BinaryReader(fs);        string fileclass = "";        byte buffer;        try        {            buffer = r.ReadByte();            fileclass = buffer.ToString();            buffer = r.ReadByte();            fileclass += buffer.ToString();        }        catch        {        }        r.Close();        fs.Close();        if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar        {            return true;        }        else        {            return false;        }    }    #endregion}

							     

#4


用FileUpload控件

System.IO.Stream fileDataStream = DocUpload.PostedFile.InputStream;
//得到文件大小 
                int fileLength = DocUpload.PostedFile.ContentLength;

                //创建数组 
                byte[] fileData = new byte[fileLength];
                //把文件流填充到数组 
                fileDataStream.Read(fileData, 0, fileLength);

#5


使用读写流 如何将图片以二进制的形式存入数据库?