精华内容
下载资源
问答
  • 二进制数据库创建使用索引 该项目演示了使用索引、哈希等创建二进制数据库
  • asp 把二进制数据库access里的记录读出来显示成图片
  • 图片的二进制数据库存储和显示

    千次阅读 2016-01-19 10:55:36
    GridView控件 Image控件 与图片的二进制数据库存储和显示1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式显示图片 5....
    GridView控件 Image控件 与图片的二进制数据库存储和显示
    
    1.将图片以二进制存入数据库
    2.读取二进制图片在页面显示
    3.设置Image控件显示从数据库中读出的二进制图片
    4.GridView中ImageField以URL方式显示图片
    5.GridView显示读出的二进制图片
    ====================
    
    1.将图片以二进制存入数据库
    --------------------------
    //保存图片到数据库
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        //图片路径
        string strPath = "~/photo/03.JPG";
        string strPhotoPath = Server.MapPath(strPath);
    
         这里的Server.MapPath()就是将上传的图片保存到***地址/
        //读取图片
        FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs); 
        byte[] photo = br.ReadBytes((int)fs.Length); 
        br.Close(); 
        fs.Close(); 
        //存入
        SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
        string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";
        strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";
        SqlCommand myComm = new SqlCommand(strComm, myConn);
        myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);
        myComm.Parameters["@photoBinary"].Value = photo;
        myConn.Open();
        myComm.ExecuteNonQuery();
        myConn.Close();
    }
    
    2.读取二进制图片在页面显示
    --------------------------
    //读取图片
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
    SqlCommand myComm = new SqlCommand(strComm, myConn);
    myConn.Open();
    SqlDataReader dr = myComm.ExecuteReader();
    while (dr.Read())
    {
        byte[] photo = (byte[])dr["personPhoto"];
        this.Response.BinaryWrite(photo);
    }
    dr.Close();
    myConn.Close();
    
    或
    
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    //
    byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
    this.Response.BinaryWrite(photo);
    
    3.设置Image控件显示从数据库中读出的二进制图片
    ---------------------------------------------
    SqlConnection myConn = new SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    //
    byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
    //图片路径
    string strPath = "~/photo/wangwu.JPG";
    string strPhotoPath = Server.MapPath(strPath);
    //保存图片文件
    BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
    bw.Write(photo);
    bw.Close();
    //显示图片
    this.Image1.ImageUrl = strPath;
    
    4.GridView中ImageField以URL方式显示图片
    ----------------------------
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="personName" HeaderText="姓名" />
            <asp:ImageField DataImageUrlField="personPhotoPath" 
                HeaderText="图片">
            </asp:ImageField>
        </Columns>
    </asp:GridView>
    后台直接绑定即可
    
    5.GridView显示读出的二进制图片
    ------------------------------
    //样板列
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="personName" HeaderText="姓名" />
            <asp:ImageField DataImageUrlField="personPhotoPath" 
                HeaderText="图片">
            </asp:ImageField>
            <asp:TemplateField HeaderText="图片">
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    
    //绑定
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;
        // System.ComponentModel.Container
        string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");
        Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");
        if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))
        {
            //
            byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");
            //图片路径
            string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";
            string strPhotoPath = Server.MapPath(strPath);
            //保存图片文件
            BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));
            bw.Write(photo);
            bw.Close();
            //显示图片
            tmp_Image.ImageUrl = strPath;
        }    
    }
    
    ***************************
    #region 相册Grid按钮事件 
    protected void gv_PhotoInfo_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
    GridViewRow currRow = (GridViewRow)((Button)e.CommandSource).Parent.Parent; 
    string strPhotoID = currRow.Cells[0].Text.ToString(); 
    string strPhotoName = currRow.Cells[1].Text.ToString(); 
    if (e.CommandName == "btn_Edit")//照片属性修改 
    { 
    Response.Redirect("PhotoEdit.aspx?PhotoID=" + strPhotoID + "&PhotoName=" + strPhotoName); 
    } 
    else if (e.CommandName == "btn_Detail")//照片内容查看 
    { 
    this.img1.Src = "PhotoShow.aspx?PhotoID=" + strPhotoID; 
    this.img1.Visible = true; 
    } 
    else if (e.CommandName == "btn_Delete")//照片删除 
    { 
    
    } 
    } 
    #endregion
    PhotoShow.aspx页面 
    =============== 
    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Collections; 
    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; 
    
    public partial class PhotoShow : System.Web.UI.Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    try 
    { 
    #region 原图 
    //int intPhotoID = Convert.ToInt32(Request.QueryString["PhotoID"].Trim()); 
    //string strSql = " SELECT PhotoContent FROM PhotoInfo "; 
    //strSql += " WHERE PhotoID = " + intPhotoID + " "; 
    //DataTable dt = DBClass.RunSelectGetDataTable(strSql); 
    //byte[] photo = (byte[])dt.Rows[0]["PhotoContent"]; 
    //this.Response.BinaryWrite(photo); 
    #endregion 
    
    #region 规定宽高的图 
    //int intPhotoID = Convert.ToInt32(Request.QueryString["PhotoID"].Trim()); 
    int intPhotoID = 1; 
    //int intHeight = Request.QueryString["PhotoHeight"].Trim(); 
    int intHeight = 500; 
    //int intWidth = Request.QueryString["PhotoWidth"].Trim(); 
    int intWidth =500; 
    
    string strSql = " SELECT PhotoContent FROM PhotoInfo "; 
    strSql += " WHERE PhotoID = " + intPhotoID + " "; 
    DataTable dt = DBClass.RunSelectGetDataTable(strSql); 
    byte[] photo = (byte[])dt.Rows[0]["PhotoContent"]; 
    //this.Response.BinaryWrite(photo); 
    System.IO.MemoryStream ms = new MemoryStream(photo); 
    System.Drawing.Image img = System.Drawing.Image.FromStream(ms); 
    System.Drawing.Image newImg = img.GetThumbnailImage(intWidth, intHeight, null, new System.IntPtr()); 
    newImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); 
    #endregion 
    } 
    catch(Exception ex) 
    { 
    
    } 
    } 
    }
    *************************
    #region 上传 按钮 事件 
    protected void btn_Upload_Click(object sender, EventArgs e) 
    { 
    using (SqlConnection sqlConn = new SqlConnection(DBClass.DBConnString())) 
    { 
    sqlConn.Open(); 
    int intImageSize; 
    string strImageType; 
    Stream ImageStream; 
    
    intImageSize = this.FileUpload1.PostedFile.ContentLength; 
    ImageStream = this.FileUpload1.PostedFile.InputStream; 
    strImageType = this.FileUpload1.PostedFile.ContentType; 
    Byte[] ImageContent = new Byte[intImageSize]; 
    int intStatus; 
    intStatus = ImageStream.Read(ImageContent, 0, intImageSize); 
    
    String strSql = "INSERT INTO PhotoInfo( AlbumID, PhotoName, PhotoType "; 
    strSql += " , PhotoContent, PhotoByteSize, PhotoNote )"; 
    strSql += " VALUES( '" + Request.QueryString["AlbumID"].Trim() + "' "; 
    strSql += " ,'" + this.txt_PhotoName.Text.Trim() + "' "; 
    strSql += " ,'" + this.FileUpload1.PostedFile.ContentType + "' "; 
    strSql += " ,@photoBinary "; 
    strSql += " ," + this.FileUpload1.PostedFile.ContentLength + " "; 
    strSql += " ,'" + this.txt_PhotoNote.Text.Trim() + "' "; 
    strSql += " ) "; 
    
    SqlCommand sqlComm = new SqlCommand(strSql, sqlConn); 
    sqlComm.Parameters.Add("@photoBinary", SqlDbType.Image, intImageSize); 
    sqlComm.Parameters["@photoBinary"].Value = ImageContent; 
    
    sqlComm.ExecuteNonQuery(); 
    sqlConn.Close(); 
    } 
    CommonHelper.DoAlert(this.Page, "上传成功"); 
    this.txt_PhotoName.Text = ""; 
    this.txt_PhotoNote.Text = ""; 
    
    } 
    #endregion
    
    展开全文
  • .net 图片的二进制数据库存储和显示

    千次阅读 2012-04-25 15:15:29
    .net 图片的二进制数据库存储和显示  与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中...

    .net 图片的二进制数据库存储和显示  

    与图片的二进制数据库存储和显示

    1.将图片以二进制存入数据库

    2.读取二进制图片在页面显示

    3.设置Image控件显示从数据库中读出的二进制图片

    4.GridView中ImageField以URL方式显示图片

    5.GridView显示读出的二进制图片

    ====================

    用到的知识点:

    FileSteam fs=new FileSteam(fileName,FileMode.Open,FileAccess.Read);

    BinaryReader binaryReader=new BinaryReader(fs);

    byte[] myByte=new byte[fs.length];

    binaryReader.Read(myByte,0,Convet.ToInt32(fs.Length));

    fs.close()

    FileInfo fl=new FileInfo(fileName);

    string imgName=fl.name;

    Memorystream ms=new MemoryStream();

    byte[] imgByte=(byte[])rd.GetValue(0);

    Stream imgStream=new MemoryStream(imgByte);

    pb.Image=Image.FromStream(imgStream);

    DataView dv=(DataView)cmb.SelectedItem;

    string imgId=dv.Row["id"].ToString();

    ============================

    1.将图片以二进制存入数据库

    C#

    --------------------------

    //保存图片到数据库

    protected void Button1_Click(object sender, EventArgs e)

    {

        //图片路径

        string strPath = "~/photo/03.JPG";

        string strPhotoPath = Server.MapPath(strPath);

        //读取图片

        FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);

        BinaryReader br = new BinaryReader(fs);

        byte[] photo = br.ReadBytes((int)fs.Length);

        br.Close();

        fs.Close();

        //存入

        SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

        string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";

        strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";

        SqlCommand myComm = new SqlCommand(strComm, myConn);

        myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);

        myComm.Parameters["@photoBinary"].Value = photo;

        myConn.Open();

        myComm.ExecuteNonQuery();

        myConn.Close();

    }

    VB.NET

    '保存图片到数据库

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

            '图片路径

            Dim strPath As String = "~/photo/03.JPG"

            Dim strPhotoPath As String = Server.MapPath(strPath)

            '读取图片

            Dim fs As FileStream = New System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read)

            Dim br As BinaryReader = New BinaryReader(fs)

            Dim photo() As Byte = br.ReadBytes(CType(fs.Length,Integer))

            br.Close

            fs.Close

            '存入

            Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")

            Dim strComm As String = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) "

            strComm = (strComm + (" VALUES('wangwu', '" _

                        + (strPath + "', @photoBinary )")))

            Dim myComm As SqlCommand = New SqlCommand(strComm, myConn)

            myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length)

            myComm.Parameters("@photoBinary").Value = photo

            myConn.Open

            myComm.ExecuteNonQuery

            myConn.Close

        End Sub

    2.读取二进制图片在页面显示

    C#

    --------------------------

    //读取图片

    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

    string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";

    SqlCommand myComm = new SqlCommand(strComm, myConn);

    myConn.Open();

    SqlDataReader dr = myComm.ExecuteReader();

    while (dr.Read())

    {

        byte[] photo = (byte[])dr["personPhoto"];

        this.Response.BinaryWrite(photo);

    }

    dr.Close();

    myConn.Close();

    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");

    SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);

    DataSet myds = new DataSet();

    myConn.Open();

    myda.Fill(myds);

    myConn.Close();

    //

    byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];

    this.Response.BinaryWrite(photo);

    VB.NET

    Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")

    Dim strComm As String = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' "

    Dim myComm As SqlCommand = New SqlCommand(strComm, myConn)

    myConn.Open

    Dim dr As SqlDataReader = myComm.ExecuteReader

    While dr.Read

        Dim photo() As Byte = CType(dr("personPhoto"),Byte())

        Me.Response.BinaryWrite(photo)

       

    End While

    dr.Close

    myConn.Close

    Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")

    Dim myda As SqlDataAdapter = New SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn)

    Dim myds As DataSet = New DataSet

    myConn.Open

    myda.Fill(myds)

    myConn.Close

    Dim photo() As Byte = CType(myds.Tables(0).Rows(0)("personPhoto"),Byte())

    Me.Response.BinaryWrite(photo)

    3.设置Image控件显示从数据库中读出的二进制图片

    C#

    ---------------------------------------------

    SqlConnection myConn = new SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa");

    SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);

    DataSet myds = new DataSet();

    myConn.Open();

    myda.Fill(myds);

    myConn.Close();

    //

    byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];

    //图片路径

    string strPath = "~/photo/wangwu.JPG";

    string strPhotoPath = Server.MapPath(strPath);

    //保存图片文件

    BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));

    bw.Write(photo);

    bw.Close();

    //显示图片

    this.Image1.ImageUrl = strPath;

    VB.NET

    Dim myConn As SqlConnection = New SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa")

    Dim myda As SqlDataAdapter = New SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn)

    Dim myds As DataSet = New DataSet

    myConn.Open

    myda.Fill(myds)

    myConn.Close

    Dim photo() As Byte = CType(myds.Tables(0).Rows(0)("personPhoto"),Byte())

    Dim strPath As String = "~/photo/wangwu.JPG"

    Dim strPhotoPath As String = Server.MapPath(strPath)

    Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))

    bw.Write(photo)

    bw.Close

    '显示图片

    Me.Image1.ImageUrl = strPath

    4.GridView中ImageField以URL方式显示图片

    ----------------------------

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

        <Columns>

            <asp:BoundField DataField="personName" HeaderText="姓名" />

            <asp:ImageField DataImageUrlField="personPhotoPath"

                HeaderText="图片">

            </asp:ImageField>

        </Columns>

    </asp:GridView>

    后台直接绑定即可

    5.GridView显示读出的二进制图片

    ------------------------------

    //样板列

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">

        <Columns>

            <asp:BoundField DataField="personName" HeaderText="姓名" />

            <asp:ImageField DataImageUrlField="personPhotoPath"

                HeaderText="图片">

            </asp:ImageField>

            <asp:TemplateField HeaderText="图片">

                <ItemTemplate>

                    <asp:Image ID="Image1" runat="server" />

                </ItemTemplate>

            </asp:TemplateField>

        </Columns>

    </asp:GridView>

    //绑定

    C#

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowIndex < 0)

            return;

        // System.ComponentModel.Container

        string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");

        Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");

        if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))

        {

            //

            byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");

            //图片路径

            string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";

            string strPhotoPath = Server.MapPath(strPath);

            //保存图片文件

            BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));

            bw.Write(photo);

            bw.Close();

            //显示图片

            tmp_Image.ImageUrl = strPath;

        }   

    }

    VB.NET

        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

            If (e.Row.RowIndex < 0) Then

                Return

            End If

            ' System.ComponentModel.Container

            Dim strPersonName As String = CType(DataBinder.Eval(e.Row.DataItem, "personName"),String)

            Dim tmp_Image As Image = CType(e.Row.Cells(2).FindControl("Image1"),Image)

            If Not System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")) Then

                '

                Dim photo() As Byte = CType(DataBinder.Eval(e.Row.DataItem, "personPhoto"),Byte())

                'G

                Dim strPath As String = ("~/photo/" _

                            + (strPersonName.Trim + ".JPG"))

                Dim strPhotoPath As String = Server.MapPath(strPath)

                'XG

                Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))

                bw.Write(photo)

                bw.Close

                '>:G

                tmp_Image.ImageUrl = strPath

            End If

        End Sub

    展开全文
  • 使用junit 怎么测试条件判断 二进制 数据库连接 等机能好使啊
  • PG create table demo ( id int, name bytea ); Insert into demo (id,name)values(256,pg_read_binary_file('imgs/1.png'));   slq server create table img( id varchar(50), ...n...

    PG 

    create table demo
    (
    id int,
    name bytea
    );
    Insert into demo (id,name)values(256,pg_read_binary_file('imgs/1.png'));

      

    slq server

     

    create table img(
    id varchar(50),
    name varbinary(max)
    );
    
    INSERT INTO img (id,name)
    select '1' as id, * from OPENROWSET(BULK N'C:\img.jpg',SINGLE_BLOB) as name
    

      

    微信公众号:

     

    转载于:https://www.cnblogs.com/Amos-Turing/p/6400917.html

    展开全文
  • 与图片的二进制数据库存储和显示

    千次阅读 2009-11-28 10:38:00
    1.将图片以二进制存入数据库2.读取二进制图片在页面显示3.设置Image控件显示从数据库中读出的二进制图片4.GridView中ImageField以URL方式显示图片5.GridView显示读出的二进制图片====================1.将图片以...

     1.将图片以二进制存入数据库
    2.读取二进制图片在页面显示
    3.设置Image控件显示从数据库中读出的二进制图片
    4.GridView中ImageField以URL方式显示图片
    5.GridView显示读出的二进制图片
    ====================

    1.将图片以二进制存入数据库

    C#
    --------------------------
    //保存图片到数据库
    protected void Button1_Click(object sender, EventArgs e)
    {
        //图片路径
        string strPath = "~/photo/03.JPG";
        string strPhotoPath = Server.MapPath(strPath);
        //读取图片
        FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        byte[] photo = br.ReadBytes((int)fs.Length);
        br.Close();
        fs.Close();
        //存入
        SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
        string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";
        strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";
        SqlCommand myComm = new SqlCommand(strComm, myConn);
        myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);
        myComm.Parameters["@photoBinary"].Value = photo;
        myConn.Open();
        myComm.ExecuteNonQuery();
        myConn.Close();
    }

    VB.NET

    '保存图片到数据库

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            '图片路径
            Dim strPath As String = "~/photo/03.JPG"
            Dim strPhotoPath As String = Server.MapPath(strPath)
            '读取图片
            Dim fs As FileStream = New System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read)
            Dim br As BinaryReader = New BinaryReader(fs)
            Dim photo() As Byte = br.ReadBytes(CType(fs.Length,Integer))
            br.Close
            fs.Close
            '存入
            Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")
            Dim strComm As String = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) "
            strComm = (strComm + (" VALUES('wangwu', '"  _
                        + (strPath + "', @photoBinary )")))
            Dim myComm As SqlCommand = New SqlCommand(strComm, myConn)
            myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length)
            myComm.Parameters("@photoBinary").Value = photo
            myConn.Open
            myComm.ExecuteNonQuery
            myConn.Close
        End Sub

    2.读取二进制图片在页面显示

    C#
    --------------------------
    //读取图片
    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
    SqlCommand myComm = new SqlCommand(strComm, myConn);
    myConn.Open();
    SqlDataReader dr = myComm.ExecuteReader();
    while (dr.Read())
    {
        byte[] photo = (byte[])dr["personPhoto"];
        this.Response.BinaryWrite(photo);
    }
    dr.Close();
    myConn.Close();

    SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    //
    byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
    this.Response.BinaryWrite(photo);

    VB.NET

    Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")
    Dim strComm As String = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' "
    Dim myComm As SqlCommand = New SqlCommand(strComm, myConn)
    myConn.Open
    Dim dr As SqlDataReader = myComm.ExecuteReader

    While dr.Read
        Dim photo() As Byte = CType(dr("personPhoto"),Byte())
        Me.Response.BinaryWrite(photo)
       
    End While
    dr.Close
    myConn.Close


    Dim myConn As SqlConnection = New SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa")
    Dim myda As SqlDataAdapter = New SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn)
    Dim myds As DataSet = New DataSet
    myConn.Open
    myda.Fill(myds)
    myConn.Close
    Dim photo() As Byte = CType(myds.Tables(0).Rows(0)("personPhoto"),Byte())
    Me.Response.BinaryWrite(photo)


    3.设置Image控件显示从数据库中读出的二进制图片

    C#
    ---------------------------------------------
    SqlConnection myConn = new SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa");
    SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn);
    DataSet myds = new DataSet();
    myConn.Open();
    myda.Fill(myds);
    myConn.Close();
    //
    byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
    //图片路径
    string strPath = "~/photo/wangwu.JPG";
    string strPhotoPath = Server.MapPath(strPath);
    //保存图片文件
    BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
    bw.Write(photo);
    bw.Close();
    //显示图片
    this.Image1.ImageUrl = strPath;

    VB.NET

    Dim myConn As SqlConnection = New SqlConnection("Data Source=192.168.0.36;Initial Catalog=TestDB;User ID=sa;Password=sa")
    Dim myda As SqlDataAdapter = New SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ", myConn)
    Dim myds As DataSet = New DataSet
    myConn.Open
    myda.Fill(myds)
    myConn.Close
    Dim photo() As Byte = CType(myds.Tables(0).Rows(0)("personPhoto"),Byte())
    Dim strPath As String = "~/photo/wangwu.JPG"
    Dim strPhotoPath As String = Server.MapPath(strPath)
    Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))
    bw.Write(photo)
    bw.Close
    '显示图片
    Me.Image1.ImageUrl = strPath

    4.GridView中ImageField以URL方式显示图片
    ----------------------------
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="personName" HeaderText="姓名" />
            <asp:ImageField DataImageUrlField="personPhotoPath"
                HeaderText="图片">
            </asp:ImageField>
        </Columns>
    </asp:GridView>
    后台直接绑定即可

    5.GridView显示读出的二进制图片
    ------------------------------
    //样板列
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="personName" HeaderText="姓名" />
            <asp:ImageField DataImageUrlField="personPhotoPath"
                HeaderText="图片">
            </asp:ImageField>
            <asp:TemplateField HeaderText="图片">
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    //绑定

    C#
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;
        // System.ComponentModel.Container
        string strPersonName = (string)DataBinder.Eval(e.Row.DataItem, "personName");
        Image tmp_Image = (Image)e.Row.Cells[2].FindControl("Image1");
        if (!System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")))
        {
            //
            byte[] photo = (byte[])DataBinder.Eval(e.Row.DataItem, "personPhoto");
            //图片路径
            string strPath = "~/photo/" + strPersonName.Trim() + ".JPG";
            string strPhotoPath = Server.MapPath(strPath);
            //保存图片文件
            BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate));
            bw.Write(photo);
            bw.Close();
            //显示图片
            tmp_Image.ImageUrl = strPath;
        }   
    }
     VB.NET

        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If (e.Row.RowIndex < 0) Then
                Return
            End If
            ' System.ComponentModel.Container
            Dim strPersonName As String = CType(DataBinder.Eval(e.Row.DataItem, "personName"),String)
            Dim tmp_Image As Image = CType(e.Row.Cells(2).FindControl("Image1"),Image)
            If Not System.Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "personPhoto")) Then
                '
                Dim photo() As Byte = CType(DataBinder.Eval(e.Row.DataItem, "personPhoto"),Byte())
                'G
                Dim strPath As String = ("~/photo/"  _
                            + (strPersonName.Trim + ".JPG"))
                Dim strPhotoPath As String = Server.MapPath(strPath)
                'XG
                Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))
                bw.Write(photo)
                bw.Close
                '>:G
                tmp_Image.ImageUrl = strPath
            End If
        End Sub

    展开全文
  • 这样做,您可以将UUID作为二进制值存储在数据库中,并且仍然能够使用字符串表示形式进行查询,因为数据库将负责类型转换。 顾名思义,它仅支持MySQL。 如果您使用的是PostgreSQL,则可以正确使用UUID。 如果要在...
  • 与图片的二进制数据库存储和显示

    千次阅读 2007-06-08 15:22:00
    1.将图片以二进制存入数据库2.读取二进制图片在页面显示3.设置Image控件显示从数据库中读出的二进制图片4.GridView中ImageField以URL方式显示图片5.GridView显示读出的二进制图片====================1.将图片以...
  • 二进制恢复数据库

    2019-12-02 09:03:21
    利用二进制日志,还原数据库最新状态 1在恢复数据库前期准备: 1.1 完全备份,并记录备份的二进制位置 mysqldump -A -F --master-data=2 | gzip > /data/binlog/all_date +%F.sql.gz 1.2二进制日志独立存放 ...
  • 二进制流的主要编码格式是base64码。可以在网上找一些在线转base64编码的网站进行尝试转换。 例如:http://imgbase64.duoshitong.com/然后通过前端展现和下载。 一、前端查看、下载功能实现 前端显示二进制流图片...
  • 图片以二进制存取数据库
  • pygeoip:Maxmind二进制GeoIP数据库的纯 Python API
  • ueditor修改版,上传图片直接保存到想要的位置,不再是项目中,添加图文内容直接转为二进制存储数据库方法,直接在页面显示,不需要任何处理。
  • 项目需求,文件要存到数据库中,自己写的dome,上传上来,供大家参考学习。vs2017环境,C#语言,SqlServer数据库,代码逻辑其实都差不多,自我感觉参考价值还是挺高的。编译执行的时候,记得修改Sql连接字符串
  • public void SaveImage(string MID, OpenFileDialog openF)//将图片以二进制存入数据库中 { string strimg = openF.FileName.ToString(); //记录图片的所在路径 FileStream fs = new FileStream(strimg, FileMode...
  • fullAvatarEditor 头像编辑上传(存入二进制图片到数据库以及显示二进制图片)
  •  mysql二进制日志文件用来记录所有用户对数据库操作,即记录用户对数据库操作的sql语句。如果有此文件,当数据库发生意外时,可以通过此文件查看到用户在此文件记录的时间段内用户所做的操作,再和数据库备份配合...
  • 文章目录**MySQL使用二进制日志恢复数据库**一、二进制日志简介二、数据恢复实例 MySQL使用二进制日志恢复数据库 一、二进制日志简介 MySQL有不同类型的日志,其中二进制文件记录了所有对数据库的修改,如果数据库...
  • 上传二进制文件到数据库源码。
  • 1.1二进制包安装MySQL数据库  1.1.1 安装前准备(规范) 1 [root@Mysql_server ~]# mkdir -p /home/zhurui/tools ##创建指定工具包存放路径 2 [root@Mysql_server ~]# wget ...
  • 与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式显示图片 5.GridView显示读出的二...
  • 读取图片的二进制流,存到数据库中读取图片的二进制流,存到数据库中读取图片的二进制流,存到数据库中读取图片的二进制流,存到数据库
  • 本篇文章主要介绍了详解如何通过Mysql的二进制日志恢复数据库数据,具有一定的参考价值,有兴趣的可以了解一下。
  • 二进制安装mariaDB-10.21.准备二进制文件2准备数据目录,创建lvm逻辑卷,后期数据量大可以扩容3.创建数据库用户4.创建数据库文件5.准备配置文件/etc/mysql/my.cnf4.创建数据库文件6.准备服务脚本,添加PATH路径,...
  • 功能介绍:第“一”解决的是js代码实现file控件浏览图片显示到img,单击保存第“二”实现把file控件浏览到的图片转成二进制格式保存到数据库,第“三”实现从数据库读出二进制流图片显示到页面img控件;一 、 js...
  • //用来存储图片的二进制 Stream ms; byte[] picbyte; //在创建数据库链接,测试链接成功后,在高级里可自动生成链接数据库字符串,copy出来即可 string str = "Data Source=PC-20180AIHL;Initial...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 391,831
精华内容 156,732
关键字:

二进制数据库