• Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control



          原文:   Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control

    主要是介绍在VS2005中如何将图片直接存入到数据库,当然利用了FileUpload这个控件

    下面我把主要代码贴上来(已经测试过了,没什么问题):

    Save:

    protected void Button1_Click(object sender, EventArgs e)
        
    {
            
    if (FileUpload1.HasFile)
            

                
    using(BinaryReader reader = new BinaryReader(FileUpload1.PostedFile.InputStream))
                
    {
                    
    byte[] image = reader.ReadBytes(FileUpload1.PostedFile.ContentLength);

                    
    using (SqlConnection conn = new SqlConnection("server=.;database=IBatisNet;uid=sa;pwd="))
                    
    {
                        
    using (SqlCommand command = conn.CreateCommand())
                        
    {
                            command.CommandText 
    = @"INSERT INTO photo (photo) VALUES (@photo)";
                            command.Parameters.AddWithValue(
    "@photo", image);
                            conn.Open();
                            command.ExecuteNonQuery();
                        }

                    }

                }

            }

        }

    Display:

    protected void Button2_Click(object sender, EventArgs e)
        
    {
            Response.Clear();
            Response.ContentType 
    = "image/jpeg";

            
    using (SqlConnection conn = new SqlConnection("server=.;database=IBatisNet;uid=sa;pwd="))
            
    {
                
    using (SqlCommand command = conn.CreateCommand())
                
    {
                    command.CommandText 
    = "select top 1 photo from photo";
                    conn.Open();
                    
    byte[] imageData = (byte[])command.ExecuteScalar();

                    Response.BinaryWrite(imageData);
                    
                }


            }

        }
  • 相关阅读:
    Excel函数和公式——Excel基础(8)
    Excel函数和公式——Excel基础(7)
    条件格式与公式——Excel基础(6)
    日期函数——Excel基础(5)
    Excel导入邮件合并——Excel基础(4)
    VScode与ssh
    python绘图相关知识点
    Python加密成.so或dll
    Pytorch从本地获取数据集
    Pytorch_3.3_ 线性回归的简洁实现
  • 原文地址:https://www.cnblogs.com/pw/p/460879.html
Copyright © 2020-2023  润新知